Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>If you care what order the records are returned, you MUST use an order by.</strong></p> <p>If you leave it out, it may <em>appear</em> organized (based on the indexes chosen by the query plan), but the results you see today may NOT be the results you expect, and it could even change when the same query is run tomorrow.</p> <p>Edit: Some good, specific examples: (all examples are MS SQL server)</p> <ul> <li><p><a href="http://blog.sqlauthority.com/2008/11/24/sql-server-interesting-observation-about-order-of-resultset-without-order-by/" rel="noreferrer">Dave Pinal's blog</a> describes how two very similar queries can show a different apparent order, because different indexes are used:</p> <pre><code>SELECT ContactID FROM Person.Contact SELECT * FROM Person.Contact </code></pre></li> <li><p><a href="http://blogs.msdn.com/conor_cunningham_msft/archive/2008/08/27/no-seatbelt-expecting-order-without-order-by.aspx" rel="noreferrer">Conor Cunningham</a> shows how the apparent order can change when the table gets larger (if the query optimizer decides to use a parallel execution plan).</p></li> <li><p><a href="http://sqlblog.com/blogs/hugo_kornelis/archive/2006/12/31/Beatles-vs-Stones.aspx" rel="noreferrer">Hugo Kornelis</a> proves that the apparent order is <strong>not</strong> always based on primary key. <a href="http://sqlblog.com/blogs/hugo_kornelis/archive/2007/01/16/Beatles_2D00_vs_2D00_Stones_2D00_explanation.aspx" rel="noreferrer">Here is his follow-up post</a> with explanation.</p></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload