beginnerSELECT Queries — WHERE, GROUP BY, HAVING, ORDER BY

What is the logical execution order of a SELECT statement?

FROM → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT/OFFSET. This matters practically: 1) You can't use column aliases defined in SELECT inside WHERE (it hasn't run yet). 2) You can't use aggregate functions in WHERE (GROUP BY hasn't happened). 3) You CAN use aggregate functions in HAVING (runs after GROUP BY). 4) You CAN use SELECT aliases in ORDER BY (runs after SELECT). 5) LI

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to What is the difference between WHERE and HAVING?← Back to all SQL questions