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?
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
add a comment |
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
1
SQL is explicit in that its data-sets are unordered data-sets. Ordering in this sense is never guaranteed to be preserved. Theorder by
must be in the same scope as theINSERT
: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 assignexample_sequence.nextval
in the way I wanted when posting the question?
– Z.Szymon
Mar 21 at 16:31
add a comment |
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
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
sql oracle
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. Theorder by
must be in the same scope as theINSERT
: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 assignexample_sequence.nextval
in the way I wanted when posting the question?
– Z.Szymon
Mar 21 at 16:31
add a comment |
1
SQL is explicit in that its data-sets are unordered data-sets. Ordering in this sense is never guaranteed to be preserved. Theorder by
must be in the same scope as theINSERT
: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 assignexample_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
add a comment |
1 Answer
1
active
oldest
votes
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
.
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
|
show 2 more comments
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
.
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
|
show 2 more comments
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
.
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
|
show 2 more comments
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
.
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
.
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
|
show 2 more comments
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
|
show 2 more comments
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 theINSERT
: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