Explain the logical order of a SQL query's execution. Why does SELECT happen after WHERE and GROUP BY, and how does this affect using aliases?
Last updated
Logical order:
FROM/JOIN,WHERE,GROUP BY,HAVING,SELECT,ORDER BY,LIMIT.SELECTruns late because filtering and grouping must first fix which rows exist before projecting columns.So
SELECTaliases can't be used inWHEREorGROUP BY, but most engines allow them inORDER BY.
- https://dev.to/kalkwst/sql-execution-order-write-queries-that-think-like-the-database-13lf
- https://dev.to/neha_christina_1ac8651819/sql-query-execution-order-why-your-query-doesnt-run-top-to-bottom-34pk
- https://www.stratascratch.com/blog/sql-execution-order-explained
- https://cwoodruff.medium.com/sql-order-of-operations-the-hidden-logic-behind-query-execution-cb21caec554f
- https://dev.to/kanani_nirav/secret-to-optimizing-sql-queries-understand-the-sql-execution-order-28m1
- https://learn.microsoft.com/en-us/answers/questions/963859/how-do-i-query-a-newly-formed-column-in-sql
- https://learn.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql?view=sql-server-ver17
- https://dev.to/t/dataengineering