Q&A-1Walk through SQL's logical processing order and explain why it matters in practice.
Answer
The order. FROM and its joins → WHERE → GROUP BY → HAVING → window functions → SELECT → DISTINCT → ORDER BY → LIMIT/OFFSET. It is a semantic model, not an execution plan: the planner reorders physical work freely as long as the result matches. What the model buys you is a prediction of which names and which values exist at each step, and therefore which errors you will hit.
| step | what it can reference |
|---|---|
| FROM / JOIN | base tables, join predicates |
| WHERE | base columns only: no select alias, no aggregate, no window function |
| GROUP BY | base columns; output aliases allowed in PostgreSQL and MySQL |
| HAVING | aggregates; output aliases only in MySQL |
| window functions | aggregates and base columns, but not other window functions |
| SELECT | aggregates, window functions; aliases are born here |
| ORDER BY | output aliases in PostgreSQL and MySQL |
| LIMIT / OFFSET | a row count, nothing else |
A trace. PostgreSQL 16:
SELECT region,
count(*) AS orders,
rank() OVER (ORDER BY count(*) DESC) AS rnk
FROM sales
WHERE status = 'paid'
GROUP BY region
HAVING count(*) > 100
ORDER BY orders DESC
LIMIT 3;FROM sales: the row set exists.WHERE status = 'paid': unpaid rows dropped.count(*)is not defined yet, soWHERE count(*) > 100is an error, not a slow query.GROUP BY region: one row per region;count(*)becomes computable.HAVING count(*) > 100: regions with 100 or fewer paid orders dropped.rank() OVER (...): computed over the surviving groups.SELECT: projectsregion,orders,rnk.ORDER BY orders DESC: the alias resolves because SELECT already ran.LIMIT 3: three rows.
Failure modes this predicts.
- Alias in WHERE.
SELECT price * 1.2 AS price_with_tax FROM items WHERE price_with_tax > 100fails withERROR: column "price_with_tax" does not existin PostgreSQL andERROR 1054 (42S22): Unknown column 'price_with_tax' in 'where clause'in MySQL. Repeat the expression or wrap the query in a subquery. - Alias in HAVING. PostgreSQL rejects
HAVING orders > 100withERROR: column "orders" does not exist; MySQL accepts it as an extension. Portable SQL repeats the aggregate. - Filtering a window function.
rnkcannot appear in WHERE or HAVING: window functions run after HAVING. You need a CTE or subquery, which is the usual reason a "simple" ranking query grows a wrapper. SELECT DISTINCTwith an ORDER BY expression outside the select list. PostgreSQL:ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list. DISTINCT runs before ORDER BY, so the sort key must survive the deduplication.LIMITwithoutORDER BY. LIMIT runs last, so it returns an arbitrary ten rows from a set whose order the engine never promised. It is stable in practice on a small, unchanging table and not stable in general.
Optimisation. A predicate that does not involve an aggregate belongs in WHERE, not HAVING: WHERE filters before grouping, so fewer rows reach the aggregate. In HAVING it is semantically after grouping, and while some planners push it down, you should not depend on that. The same reasoning explains why WHERE on a partitioned or indexed column is where the planner can actually use the index: by the time HAVING runs, the grouping work is already paid for.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.