SQL query, execution sequence
Ordering usually happens last.
If you are using SQL Server, run the query parser and the execution plan will give you a nice graphical view of your query.
source to share
The execution sequence is not required by any SQL statement. The result is said to correspond to the result to be obtained by the "canonical" evaluation. In canonical evaluation, ORDER BY is applied last (even after evaluating SELECT statements), but that does not mean that sorting is carried over to that point when the query is actually executed on the real system.
source to share
I also suggest using a query parser for a specific database engine. Below is an example in Postgres explaining that ORDER BY is executed first and a WHERE filter after that:
EXPLAIN
SELECT * FROM table WHERE id=x AND date<='yyyy-mm-dd' ORDER BY date DESC;
So, if I change DESC to ASC, the result set contains different records!
source to share