How do database servers decide which order to return rows without any "order by" statements? How do database servers decide which order to return rows without any "order by" statements? sql sql

How do database servers decide which order to return rows without any "order by" statements?


If you do not supply an ORDER BY clause on a SELECT statement you will get rows back in arbitrary order.

The actual order is undefined, and depends on which blocks/records are already cached in memory, what order I/O is performed in, when threads in the database server are scheduled to run, and so on.

There's no rhyme or reason to the order and you should never base any expectations on what order rows will be in unless you supply an ORDER BY.


If they're not ordered by the calling query, I believe they're just returned in the order they were read off disk. This may vary because of the types of joins used or the indexes that looked up the values.

You can see this if the table has a clustered index on it (and you're just selecting - a JOIN can re-order things) - a SELECT will return the rows in clustered-index-order, even without an ORDER BY clause.