Learn SQL
-- fundamentals.execution_order

Logical Execution Order in SQL

Written SELECT-first, executed FROM-first — why aliases fail in WHERE

--concept

SQL is written SELECT … FROM … WHERE … but executed FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT. WHERE runs before SELECT, so it cannot see column aliases; ORDER BY runs after, so it can. GROUP BY also precedes SELECT, which is why only grouped or aggregated expressions are valid there.

--how it works
1

FROM loads first

The engine starts from the table (or JOIN result) — not from SELECT. Everything downstream sees those rows.

2

WHERE filters early

Rows are trimmed before grouping or projection. No aliases exist yet, no aggregates — only base columns and expressions over them.

3

GROUP BY + HAVING bucket

Survivors are bucketed, aggregates computed per bucket, then HAVING trims buckets. Aggregates are born here.

4

SELECT, ORDER BY, LIMIT finish

Projection and aliases appear, then ORDER BY sorts (aliases visible) and LIMIT caps. Alias-in-WHERE errors are execution order made visible.

--visualization
FROM1→WHERE2→GROUP BY3→HAVING4→SELECT5→ORDER BY6→LIMIT7WHERE cannot see SELECT aliases
Stage 1 / 9
Written SELECT-first, executed FROM-first. Press play to walk the pipeline.
--example
query.sql
1SELECT salary * 12 AS annual FROM employees WHERE annual > 500000; -- INVALID
2SELECT salary * 12 AS annual FROM employees ORDER BY annual DESC; -- VALID
Result
nameannual
Cynthia1092000
Alice1020000
Farid876000

The first query fails because WHERE runs before the alias annual exists — repeat the expression or filter outside. The second works because ORDER BY runs after SELECT.

--variants

Alias invisible in WHERE

WHERE runs before SELECT, so the alias does not exist yet.

query.sql
1-- INVALID: annual unknown in WHERE
2-- SELECT salary * 12 AS annual FROM employees WHERE annual > 500000;
3-- VALID: repeat expression
4SELECT name, salary * 12 AS annual FROM employees WHERE salary * 12 > 500000 ORDER BY annual DESC;
Result
nameannual
Cynthia1092000
Alice1020000
Farid876000
1FROM loads all six employees.
2WHERE evaluates salary * 12 per row — aliases not consulted.
3SELECT projects the alias; ORDER BY sorts by it legally.

HAVING sits after grouping

Aggregates can only be filtered after they are computed.

query.sql
1SELECT department, AVG(salary) AS avg_pay FROM employees GROUP BY department HAVING AVG(salary) > 60000;
Result
departmentavg_pay
Engineering83000
1WHERE would fail here — no averages exist before grouping.
2GROUP BY builds three buckets; AVG computed per bucket.
3HAVING keeps only Engineering above 60000.
--key facts
OrderFROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT
Alias ruleWHERE/GROUP BY cannot use SELECT aliases; ORDER BY can
Aggregate ruleaggregates are illegal in WHERE — no groups exist yet
--use cases
Debugging alias/aggregate errors — half of all SQL error messages are ordering
Interview recital: seven-step order plus alias visibility rule
--under the hood
Parsers resolve names in execution order — that is why the error points at WHERE, not SELECT.
Planners push selective WHERE before JOIN width; ORDER BY + LIMIT enables top-N short-circuit.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.