As I know, from the relational database theory, a select
statement without an order by
clause should be considered to have no particular order. But actually in SQL Server and Oracle (I've tested on those 2 platforms), if I query from a table without an order by
clause multiple times, I always get the results in the same order. Does this behavior can be relied on? Anyone can help to explain a little?

- 11,507
- 3
- 43
- 82

- 26,690
- 50
- 155
- 234
-
4No it can't be relied upon. In SQL Server if you don't specify an order then you may get either an index scan or an allocation ordered scan for example. Plus also you might encounter the "advanced scanning" / merry-go-round scanning feature. – Martin Smith Apr 08 '12 at 16:52
-
6Add an ORDER BY clause. End of story. – Remus Rusanu Apr 08 '12 at 17:05
-
10Yes, it can be relied on. It can be relied on to not work the way you relied upon it working previously at the most inopportune time :-) – DCookie Apr 08 '12 at 17:16
-
2If you know the order that you want and expect, what is the point of leaving out the `ORDER BY`? Are those keystrokes really worth trading in predictability? – Aaron Bertrand Apr 10 '12 at 16:11
4 Answers
No, that behavior cannot be relied on. The order is determined by the way the query planner has decided to build up the result set. simple queries like select * from foo_table
are likely to be returned in the order they are stored on disk, which may be in primary key order or the order they were created, or some other random order. more complex queries, such as select * from foo where bar < 10
may instead be returned in order of a different column, based on an index read, or by the table order, for a table scan. even more elaborate queries, with multipe where
conditions, group by
clauses, union
s, will be in whatever order the planner decides is most efficient to generate.
The order could even change between two identical queries just because of data that has changed between those queries. a "where" clause may be satisfied with an index scan in one query, but later inserts could make that condition less selective, and the planner could decide to perform a subsequent query using a table scan.
To put a finer point on it. RDBMS systems have the mandate to give you exactly what you asked for, as efficiently as possible. That efficiency can take many forms, including minimizing IO (both to disk as well as over the network to send data to you), minimizing CPU and keeping the size of its working set small (using methods that require minimal temporary storage).
without an ORDER BY
clause, you will have not asked exactly for a particular order, and so the RDBMS will give you those rows in some order that (maybe) corresponds with some coincidental aspect of the query, based on whichever algorithm the RDBMS expects to produce the data the fastest.
If you care about efficiency, but not order, skip the ORDER BY
clause. If you care about the order but not efficiency, use the ORDER BY
clause.
Since you actually care about BOTH use ORDER BY
and then carefully tune your query and database so that it is efficient.

- 9,720
- 3
- 42
- 67

- 151,563
- 33
- 264
- 304
-
Another reason for always including `ORDER BY` is the feature called SQL Server Enterprise Advanced Scan aka Merry-Go-Round Scan. (mentioned in excellent Pro SQL Server Internals by Dmitri Korotkevitch) – Grzegorz Smulko Jul 19 '20 at 21:25
No, you can't rely on getting the results back in the same order every time. I discovered that when working on a web page with a paged grid. When I went to the next page, and then back to the previous page, the previous page contained different records! I was totally mystified.
For predictable results, then, you should include an ORDER BY
. Even then, if there are identical values in the specified columns there, you can get different results. You may have to ORDER BY
fields that you didn't really think you needed, just to get a predictable result.

- 32,337
- 7
- 60
- 92
Tom Kyte has a pet peeve about this topic. For whatever reason, people are fascinated by this, and keep trying to come up with cases where you can rely upon a specific order without specifying ORDER BY. As others have stated, you can't. Here's another amusing thread on the topic on the AskTom website.
The Right Answer
This is a new answer added to correct the old one. I've got answer from Tom Kyte and I post it here:
If you want rows sorted YOU HAVE TO USE AN ORDER. No if, and, or buts about it. period. http://tkyte.blogspot.ru/2005/08/order-in-court.html You need order by on that IOT. Rows are sorted in leaf blocks, but leaf blocks are not stored sorted. fast full scan=unsorted rows.
https://twitter.com/oracleasktom/status/625318150590980097
https://twitter.com/oracleasktom/status/625316875338149888
The Wrong Answer
(Attention! The original answer on the question was placed below here only for the sake of the history. It's wrong answer. The right answer is placed above)
As Tom Kyte wrote in the article mentioned before:
You should think of a heap organized table as a big unordered collection of rows. These rows will come out in a seemingly random order, and depending on other options being used (parallel query, different optimizer modes and so on), they may come out in a different order with the same query. Do not ever count on the order of rows from a query unless you have an ORDER BY statement on your query!
But note he only talks about heap-organized tables. But there is also index-orgainzed tables. In that case you can rely on order of the select without ORDER BY
because order implicitly defined by primary key. It is true for Oracle.
For SQL Server clustered indexes (index-organized tables) created by default. There is also possibility for PostgreSQL store information aligning by index. More information can be found here
UPDATE: I see, that there is voting down on my answer. So I would try to explain my point a little bit. In the section Overview of Index-Organized Tables there is a phrase:
In an index-organized table, rows are stored in an index defined on the primary key for the table... Index-organized tables are useful when related pieces of data must be stored together or data must be physically stored in a specific order.
http://docs.oracle.com/cd/E25054_01/server.1111/e25789/indexiot.htm#CBBJEBIH
Because of index, all data is stored in specific order, I believe same is true for Pg. http://www.postgresql.org/docs/9.2/static/sql-cluster.html
If you don't agree with me please give me a link on the documenation. I'll be happy to know that there is something to learn for me.

- 19,824
- 17
- 99
- 186

- 2,881
- 2
- 20
- 31
-
1Can you point to documentation in oracle or postgres documentation that indicates that the order for selects is determined for those storage types even in the absence of an `ORDER BY` clause? I'm fairly sure that no such promise exists. – SingleNegationElimination Apr 23 '15 at 20:14
-
@SingleNegationElimination I've placed quote from the oracle docs. It's a pity to see that my answer is voted down... – Alexander Myshov Jul 26 '15 at 04:28
-
Also I've asked about this issue Tom Kyte in twitter, hope he would post answer and after I would post link on it. – Alexander Myshov Jul 26 '15 at 04:30
-
Don't forget about secondary indexes. There's more than one way to access an IOT. – Jon Heller Jul 26 '15 at 05:06
-
I think you confuse order of stored rows with a guaranteed order of rows returned from a query. Not the same thing. – Mikael Eriksson Jul 26 '15 at 11:32
-
3You all were right. I was confused a little bit :| Tom Kyte had answered on my question. I've placed Tom's answer in the top of the my answer. But I don't want to delete my original answer for the sake of the history. So I've leaved it here too, with caption The Wrong Answer. Maybe my mistake will help someone sometime. – Alexander Myshov Jul 26 '15 at 15:25