It's simple: if you need ordered data, use ORDER BY. It's not hard!
This may not cause you problems today or next week, or even next month, but one day.
I was in a project where we needed to rewrite dozens (or maybe hundreds) of queries after upgrading to Oracle 10g so that GROUP BY was evaluated differently than in Oracle 9i, which means that the queries were not necessarily ordered by grouped columns . Not fun and just avoided.
Remember that SQL is a declarative language, so you tell the DBMS what you want, and then the DBMS develops how to get it. Each time it will return the same results, but each time it can evaluate differently: there are no guarantees.
Just one simple example of where this can cause problems is that new rows appear at the end of the table if you select from the table ... until they do, because you deleted several rows and the DBMS decides to fill the empty space.
There are an unrecognizable number of ways this might go wrong if you are not using ORDER BY.
Why does water boil at 100 Β° C? Because the way it was defined.
Why are there no guarantees regarding ordering the result without ORDER BY? Because the way it was defined.
The next time, the DBMS is likely to use the same query plan, and this query plan is likely to return data in the same order: but this is not a guarantee, not even close to the guarantee.
Mike meyers
source share