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 3 July 2026

  • Logical order: FROM/JOIN, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT.

  • SELECT runs late because filtering and grouping must first fix which rows exist before projecting columns.

  • So SELECT aliases can't be used in WHERE or GROUP BY, but most engines allow them in ORDER 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
All SQL interview questions