This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Originally posted by Ray Marsh: Do I have to specify an "order by" clause?
If you want results in a specific order, you have to ask for it. Full stop.
If you don't specify an "order by", the database optimiser is free to execute the query in whatever way it likes, and return the result in whatever order it happens to read the rows in. Most of the time this will be some "obvious" order, but don't ever rely on it. A change in the amount or nature of your data may prompt the optimiser to use a different execution plan and return results in a different order. Similarly, you might add a new index to the table as part of an application change and find the result order changing. An upgrade to the database may include optimiser tweaks that have the same effect. And, last but certainly not least, when read by human eyes your SQL code is not a clear expression of your intent.
All in all this is a recipe for maintenance nightmares. If you want results in a specific order, always use an "order by".
- Peter [ July 31, 2004: Message edited by: Peter den Haan ]
Joined: Jul 14, 2004
Thanks for the info.
I'm used to an environment where the DB file accessed always produces the data in key order, unless otherwise specified. This is a change of perspective for me.