Will order by preserve? The Next CEO of Stack OverflowSQLite - UPSERT *not* INSERT or REPLACEHow do I limit the number of rows returned by an Oracle query after ordering?What are the options for storing hierarchical data in a relational database?creating functional index with a order by clause in oracleView's SELECT contains a subquery in the FROM clausePreserve order with Select…Where In and lock the linesPreserve order of SQL WHERE IN() clause with nested SELECToracle 11g using index on mixed order by statementdb2 concatenated rows displaying whitespaceDoes Create Table as Select preserves columns order in Oracle?

What does "Its cash flow is deeply negative" mean?

When airplanes disconnect from a tanker during air to air refueling, why do they bank so sharply to the right?

Why didn't Khan get resurrected in the Genesis Explosion?

What's the point of interval inversion?

How to use tikz in fbox?

If the heap is initialized for security, then why is the stack uninitialized?

What is meant by a M next to a roman numeral?

Return the Closest Prime Number

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

WOW air has ceased operation, can I get my tickets refunded?

Can a caster that cast Polymorph on themselves stop concentrating at any point even if their Int is low?

Rotate a column

How do I get the green key off the shelf in the Dobby level of Lego Harry Potter 2?

Why does standard notation not preserve intervals (visually)

Visit to the USA with ESTA approved before trip to Iran

Are there languages with no euphemisms?

Is it safe to use c_str() on a temporary string?

How long to clear the 'suck zone' of a turbofan after start is initiated?

Horror movie/show or scene where a horse creature opens its mouth really wide and devours a man in a stables

Why did we only see the N-1 starfighters in one film?

Grabbing quick drinks

Why here is plural "We went to the movies last night."

Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis

What is the purpose of the Evocation wizard's Potent Cantrip feature?



Will order by preserve?



The Next CEO of Stack OverflowSQLite - UPSERT *not* INSERT or REPLACEHow do I limit the number of rows returned by an Oracle query after ordering?What are the options for storing hierarchical data in a relational database?creating functional index with a order by clause in oracleView's SELECT contains a subquery in the FROM clausePreserve order with Select…Where In and lock the linesPreserve order of SQL WHERE IN() clause with nested SELECToracle 11g using index on mixed order by statementdb2 concatenated rows displaying whitespaceDoes Create Table as Select preserves columns order in Oracle?










3















create table source_table (id number);
insert into source_table values(3);
insert into source_table values(1);
insert into source_table values(2);

create table target_table (id number, seq_val number);
create sequence example_sequence;

insert into target_table
select id, example_sequence.nextval
from

> (select id from source_table ***order by id***);


Is it officially assured that for the id's with the lower values in source_table corresponding sequence's value will also be lower when inserting into the source_table? In other words, is it guaranteed that the sorting provided by order by clause will be preserved when inserting?



EDIT
The question is not: 'Are rows ordered in a table as such?' but rather 'Can we rely on the order by clause used in the subquery when inserting?'.



To even more closely illustrate this, the contents of the target table in the above example, after running the query like select * from target_table order by id would be:



ID | SEQ_VAL
1 1
2 2
3 3



Moreover, if i specified descending ordering when inserting like this:



insert into target_table 
select id, example_sequence.nextval
from

> (select id from source_table ***order by id DESC***);


The output of the same query from above would be:
ID | SEQ_VAL
1 3
2 2
3 1



Of that I'm sure, I have tested it multiple times. My question is 'Can I always rely on this ordering?'










share|improve this question



















  • 1





    SQL is explicit in that its data-sets are unordered data-sets. Ordering in this sense is never guaranteed to be preserved. The order by must be in the same scope as the INSERT : INSERT INTO tgt SELECT id, s.nextval FROM src ORDER BY id

    – MatBailie
    Mar 21 at 16:15







  • 1





    That's what I thought at first as well. But when testing I always got correct ordering. Hence my question was raised. Is it officially confirmed to work that way every time.

    – Z.Szymon
    Mar 21 at 16:18






  • 3





    No, it will often, even normally, work, just because it's convenient for the database. But it's never a guarantee.

    – MatBailie
    Mar 21 at 16:19






  • 5





    This will definitively fail if you use a parallel insert.

    – Marmite Bomber
    Mar 21 at 16:21






  • 1





    So there is no way to assign example_sequence.nextval in the way I wanted when posting the question?

    – Z.Szymon
    Mar 21 at 16:31















3















create table source_table (id number);
insert into source_table values(3);
insert into source_table values(1);
insert into source_table values(2);

create table target_table (id number, seq_val number);
create sequence example_sequence;

insert into target_table
select id, example_sequence.nextval
from

> (select id from source_table ***order by id***);


Is it officially assured that for the id's with the lower values in source_table corresponding sequence's value will also be lower when inserting into the source_table? In other words, is it guaranteed that the sorting provided by order by clause will be preserved when inserting?



EDIT
The question is not: 'Are rows ordered in a table as such?' but rather 'Can we rely on the order by clause used in the subquery when inserting?'.



To even more closely illustrate this, the contents of the target table in the above example, after running the query like select * from target_table order by id would be:



ID | SEQ_VAL
1 1
2 2
3 3



Moreover, if i specified descending ordering when inserting like this:



insert into target_table 
select id, example_sequence.nextval
from

> (select id from source_table ***order by id DESC***);


The output of the same query from above would be:
ID | SEQ_VAL
1 3
2 2
3 1



Of that I'm sure, I have tested it multiple times. My question is 'Can I always rely on this ordering?'










share|improve this question



















  • 1





    SQL is explicit in that its data-sets are unordered data-sets. Ordering in this sense is never guaranteed to be preserved. The order by must be in the same scope as the INSERT : INSERT INTO tgt SELECT id, s.nextval FROM src ORDER BY id

    – MatBailie
    Mar 21 at 16:15







  • 1





    That's what I thought at first as well. But when testing I always got correct ordering. Hence my question was raised. Is it officially confirmed to work that way every time.

    – Z.Szymon
    Mar 21 at 16:18






  • 3





    No, it will often, even normally, work, just because it's convenient for the database. But it's never a guarantee.

    – MatBailie
    Mar 21 at 16:19






  • 5





    This will definitively fail if you use a parallel insert.

    – Marmite Bomber
    Mar 21 at 16:21






  • 1





    So there is no way to assign example_sequence.nextval in the way I wanted when posting the question?

    – Z.Szymon
    Mar 21 at 16:31













3












3








3








create table source_table (id number);
insert into source_table values(3);
insert into source_table values(1);
insert into source_table values(2);

create table target_table (id number, seq_val number);
create sequence example_sequence;

insert into target_table
select id, example_sequence.nextval
from

> (select id from source_table ***order by id***);


Is it officially assured that for the id's with the lower values in source_table corresponding sequence's value will also be lower when inserting into the source_table? In other words, is it guaranteed that the sorting provided by order by clause will be preserved when inserting?



EDIT
The question is not: 'Are rows ordered in a table as such?' but rather 'Can we rely on the order by clause used in the subquery when inserting?'.



To even more closely illustrate this, the contents of the target table in the above example, after running the query like select * from target_table order by id would be:



ID | SEQ_VAL
1 1
2 2
3 3



Moreover, if i specified descending ordering when inserting like this:



insert into target_table 
select id, example_sequence.nextval
from

> (select id from source_table ***order by id DESC***);


The output of the same query from above would be:
ID | SEQ_VAL
1 3
2 2
3 1



Of that I'm sure, I have tested it multiple times. My question is 'Can I always rely on this ordering?'










share|improve this question
















create table source_table (id number);
insert into source_table values(3);
insert into source_table values(1);
insert into source_table values(2);

create table target_table (id number, seq_val number);
create sequence example_sequence;

insert into target_table
select id, example_sequence.nextval
from

> (select id from source_table ***order by id***);


Is it officially assured that for the id's with the lower values in source_table corresponding sequence's value will also be lower when inserting into the source_table? In other words, is it guaranteed that the sorting provided by order by clause will be preserved when inserting?



EDIT
The question is not: 'Are rows ordered in a table as such?' but rather 'Can we rely on the order by clause used in the subquery when inserting?'.



To even more closely illustrate this, the contents of the target table in the above example, after running the query like select * from target_table order by id would be:



ID | SEQ_VAL
1 1
2 2
3 3



Moreover, if i specified descending ordering when inserting like this:



insert into target_table 
select id, example_sequence.nextval
from

> (select id from source_table ***order by id DESC***);


The output of the same query from above would be:
ID | SEQ_VAL
1 3
2 2
3 1



Of that I'm sure, I have tested it multiple times. My question is 'Can I always rely on this ordering?'







sql oracle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 21 at 22:47







Z.Szymon

















asked Mar 21 at 16:12









Z.SzymonZ.Szymon

537




537







  • 1





    SQL is explicit in that its data-sets are unordered data-sets. Ordering in this sense is never guaranteed to be preserved. The order by must be in the same scope as the INSERT : INSERT INTO tgt SELECT id, s.nextval FROM src ORDER BY id

    – MatBailie
    Mar 21 at 16:15







  • 1





    That's what I thought at first as well. But when testing I always got correct ordering. Hence my question was raised. Is it officially confirmed to work that way every time.

    – Z.Szymon
    Mar 21 at 16:18






  • 3





    No, it will often, even normally, work, just because it's convenient for the database. But it's never a guarantee.

    – MatBailie
    Mar 21 at 16:19






  • 5





    This will definitively fail if you use a parallel insert.

    – Marmite Bomber
    Mar 21 at 16:21






  • 1





    So there is no way to assign example_sequence.nextval in the way I wanted when posting the question?

    – Z.Szymon
    Mar 21 at 16:31












  • 1





    SQL is explicit in that its data-sets are unordered data-sets. Ordering in this sense is never guaranteed to be preserved. The order by must be in the same scope as the INSERT : INSERT INTO tgt SELECT id, s.nextval FROM src ORDER BY id

    – MatBailie
    Mar 21 at 16:15







  • 1





    That's what I thought at first as well. But when testing I always got correct ordering. Hence my question was raised. Is it officially confirmed to work that way every time.

    – Z.Szymon
    Mar 21 at 16:18






  • 3





    No, it will often, even normally, work, just because it's convenient for the database. But it's never a guarantee.

    – MatBailie
    Mar 21 at 16:19






  • 5





    This will definitively fail if you use a parallel insert.

    – Marmite Bomber
    Mar 21 at 16:21






  • 1





    So there is no way to assign example_sequence.nextval in the way I wanted when posting the question?

    – Z.Szymon
    Mar 21 at 16:31







1




1





SQL is explicit in that its data-sets are unordered data-sets. Ordering in this sense is never guaranteed to be preserved. The order by must be in the same scope as the INSERT : INSERT INTO tgt SELECT id, s.nextval FROM src ORDER BY id

– MatBailie
Mar 21 at 16:15






SQL is explicit in that its data-sets are unordered data-sets. Ordering in this sense is never guaranteed to be preserved. The order by must be in the same scope as the INSERT : INSERT INTO tgt SELECT id, s.nextval FROM src ORDER BY id

– MatBailie
Mar 21 at 16:15





1




1





That's what I thought at first as well. But when testing I always got correct ordering. Hence my question was raised. Is it officially confirmed to work that way every time.

– Z.Szymon
Mar 21 at 16:18





That's what I thought at first as well. But when testing I always got correct ordering. Hence my question was raised. Is it officially confirmed to work that way every time.

– Z.Szymon
Mar 21 at 16:18




3




3





No, it will often, even normally, work, just because it's convenient for the database. But it's never a guarantee.

– MatBailie
Mar 21 at 16:19





No, it will often, even normally, work, just because it's convenient for the database. But it's never a guarantee.

– MatBailie
Mar 21 at 16:19




5




5





This will definitively fail if you use a parallel insert.

– Marmite Bomber
Mar 21 at 16:21





This will definitively fail if you use a parallel insert.

– Marmite Bomber
Mar 21 at 16:21




1




1





So there is no way to assign example_sequence.nextval in the way I wanted when posting the question?

– Z.Szymon
Mar 21 at 16:31





So there is no way to assign example_sequence.nextval in the way I wanted when posting the question?

– Z.Szymon
Mar 21 at 16:31












1 Answer
1






active

oldest

votes


















3














Tables in a relational database are not ordered, and any apparent ordering in the result set of a cursor which lacks an ORDER BY is an artifact of data storage, is not guaranteed, and later actions on the table may cause this apparent ordering to change. If you want the results of a cursor to be ordered in a particular manner you MUST use an ORDER BY.






share|improve this answer


















  • 1





    Oh but I do! Just in the subquery... .

    – Z.Szymon
    Mar 21 at 16:53







  • 1





    Once it's stored in the table there is no ordering to the data. When you SELECT data from the table you created you must use an ORDER BY if you want the results in a particular order. Think of a table as if it's a bag, into which you toss your data - from across the room. It lands in the bag with considerable force, and the impact of the new data may cause old data to move - or perhaps the new data will end up on the top of the old data - or next to it. You have no guarantee of ordering. Use an ORDER BY whenever you SELECT data if you care about the order of the results.

    – Bob Jarvis
    Mar 21 at 17:09











  • The question as I undestod it is about assigning sequence ID in the same order as one column in the source table.

    – Marmite Bomber
    Mar 21 at 19:42











  • @MarmiteBomber that exactly what the question was about!.

    – Z.Szymon
    Mar 21 at 22:34











  • Nevertheless thank you @BobJarvis for touching on very close related topic which is chaotic row placement in the table as such.

    – Z.Szymon
    Mar 21 at 22:36











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55284803%2fwill-order-by-preserve%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Tables in a relational database are not ordered, and any apparent ordering in the result set of a cursor which lacks an ORDER BY is an artifact of data storage, is not guaranteed, and later actions on the table may cause this apparent ordering to change. If you want the results of a cursor to be ordered in a particular manner you MUST use an ORDER BY.






share|improve this answer


















  • 1





    Oh but I do! Just in the subquery... .

    – Z.Szymon
    Mar 21 at 16:53







  • 1





    Once it's stored in the table there is no ordering to the data. When you SELECT data from the table you created you must use an ORDER BY if you want the results in a particular order. Think of a table as if it's a bag, into which you toss your data - from across the room. It lands in the bag with considerable force, and the impact of the new data may cause old data to move - or perhaps the new data will end up on the top of the old data - or next to it. You have no guarantee of ordering. Use an ORDER BY whenever you SELECT data if you care about the order of the results.

    – Bob Jarvis
    Mar 21 at 17:09











  • The question as I undestod it is about assigning sequence ID in the same order as one column in the source table.

    – Marmite Bomber
    Mar 21 at 19:42











  • @MarmiteBomber that exactly what the question was about!.

    – Z.Szymon
    Mar 21 at 22:34











  • Nevertheless thank you @BobJarvis for touching on very close related topic which is chaotic row placement in the table as such.

    – Z.Szymon
    Mar 21 at 22:36















3














Tables in a relational database are not ordered, and any apparent ordering in the result set of a cursor which lacks an ORDER BY is an artifact of data storage, is not guaranteed, and later actions on the table may cause this apparent ordering to change. If you want the results of a cursor to be ordered in a particular manner you MUST use an ORDER BY.






share|improve this answer


















  • 1





    Oh but I do! Just in the subquery... .

    – Z.Szymon
    Mar 21 at 16:53







  • 1





    Once it's stored in the table there is no ordering to the data. When you SELECT data from the table you created you must use an ORDER BY if you want the results in a particular order. Think of a table as if it's a bag, into which you toss your data - from across the room. It lands in the bag with considerable force, and the impact of the new data may cause old data to move - or perhaps the new data will end up on the top of the old data - or next to it. You have no guarantee of ordering. Use an ORDER BY whenever you SELECT data if you care about the order of the results.

    – Bob Jarvis
    Mar 21 at 17:09











  • The question as I undestod it is about assigning sequence ID in the same order as one column in the source table.

    – Marmite Bomber
    Mar 21 at 19:42











  • @MarmiteBomber that exactly what the question was about!.

    – Z.Szymon
    Mar 21 at 22:34











  • Nevertheless thank you @BobJarvis for touching on very close related topic which is chaotic row placement in the table as such.

    – Z.Szymon
    Mar 21 at 22:36













3












3








3







Tables in a relational database are not ordered, and any apparent ordering in the result set of a cursor which lacks an ORDER BY is an artifact of data storage, is not guaranteed, and later actions on the table may cause this apparent ordering to change. If you want the results of a cursor to be ordered in a particular manner you MUST use an ORDER BY.






share|improve this answer













Tables in a relational database are not ordered, and any apparent ordering in the result set of a cursor which lacks an ORDER BY is an artifact of data storage, is not guaranteed, and later actions on the table may cause this apparent ordering to change. If you want the results of a cursor to be ordered in a particular manner you MUST use an ORDER BY.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 21 at 16:49









Bob JarvisBob Jarvis

34.7k55987




34.7k55987







  • 1





    Oh but I do! Just in the subquery... .

    – Z.Szymon
    Mar 21 at 16:53







  • 1





    Once it's stored in the table there is no ordering to the data. When you SELECT data from the table you created you must use an ORDER BY if you want the results in a particular order. Think of a table as if it's a bag, into which you toss your data - from across the room. It lands in the bag with considerable force, and the impact of the new data may cause old data to move - or perhaps the new data will end up on the top of the old data - or next to it. You have no guarantee of ordering. Use an ORDER BY whenever you SELECT data if you care about the order of the results.

    – Bob Jarvis
    Mar 21 at 17:09











  • The question as I undestod it is about assigning sequence ID in the same order as one column in the source table.

    – Marmite Bomber
    Mar 21 at 19:42











  • @MarmiteBomber that exactly what the question was about!.

    – Z.Szymon
    Mar 21 at 22:34











  • Nevertheless thank you @BobJarvis for touching on very close related topic which is chaotic row placement in the table as such.

    – Z.Szymon
    Mar 21 at 22:36












  • 1





    Oh but I do! Just in the subquery... .

    – Z.Szymon
    Mar 21 at 16:53







  • 1





    Once it's stored in the table there is no ordering to the data. When you SELECT data from the table you created you must use an ORDER BY if you want the results in a particular order. Think of a table as if it's a bag, into which you toss your data - from across the room. It lands in the bag with considerable force, and the impact of the new data may cause old data to move - or perhaps the new data will end up on the top of the old data - or next to it. You have no guarantee of ordering. Use an ORDER BY whenever you SELECT data if you care about the order of the results.

    – Bob Jarvis
    Mar 21 at 17:09











  • The question as I undestod it is about assigning sequence ID in the same order as one column in the source table.

    – Marmite Bomber
    Mar 21 at 19:42











  • @MarmiteBomber that exactly what the question was about!.

    – Z.Szymon
    Mar 21 at 22:34











  • Nevertheless thank you @BobJarvis for touching on very close related topic which is chaotic row placement in the table as such.

    – Z.Szymon
    Mar 21 at 22:36







1




1





Oh but I do! Just in the subquery... .

– Z.Szymon
Mar 21 at 16:53






Oh but I do! Just in the subquery... .

– Z.Szymon
Mar 21 at 16:53





1




1





Once it's stored in the table there is no ordering to the data. When you SELECT data from the table you created you must use an ORDER BY if you want the results in a particular order. Think of a table as if it's a bag, into which you toss your data - from across the room. It lands in the bag with considerable force, and the impact of the new data may cause old data to move - or perhaps the new data will end up on the top of the old data - or next to it. You have no guarantee of ordering. Use an ORDER BY whenever you SELECT data if you care about the order of the results.

– Bob Jarvis
Mar 21 at 17:09





Once it's stored in the table there is no ordering to the data. When you SELECT data from the table you created you must use an ORDER BY if you want the results in a particular order. Think of a table as if it's a bag, into which you toss your data - from across the room. It lands in the bag with considerable force, and the impact of the new data may cause old data to move - or perhaps the new data will end up on the top of the old data - or next to it. You have no guarantee of ordering. Use an ORDER BY whenever you SELECT data if you care about the order of the results.

– Bob Jarvis
Mar 21 at 17:09













The question as I undestod it is about assigning sequence ID in the same order as one column in the source table.

– Marmite Bomber
Mar 21 at 19:42





The question as I undestod it is about assigning sequence ID in the same order as one column in the source table.

– Marmite Bomber
Mar 21 at 19:42













@MarmiteBomber that exactly what the question was about!.

– Z.Szymon
Mar 21 at 22:34





@MarmiteBomber that exactly what the question was about!.

– Z.Szymon
Mar 21 at 22:34













Nevertheless thank you @BobJarvis for touching on very close related topic which is chaotic row placement in the table as such.

– Z.Szymon
Mar 21 at 22:36





Nevertheless thank you @BobJarvis for touching on very close related topic which is chaotic row placement in the table as such.

– Z.Szymon
Mar 21 at 22:36



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55284803%2fwill-order-by-preserve%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현