SQL Order of Execution
You write SELECT first, but the engine reads FROM first. Know the order and most errors explain themselves.

What the sheet covers
A compact reference for how a SQL engine reads a query. The written order starts with SELECT, but logical execution starts at FROM, then WHERE, GROUP BY, HAVING, SELECT (columns, aliases and window functions), QUALIFY, DISTINCT, ORDER BY and LIMIT. SELECT aliases are safe in QUALIFY and ORDER BY, and not portable in GROUP BY. DuckDB and Snowflake also accept aliases in WHERE and HAVING; BigQuery and MySQL accept them in HAVING; PostgreSQL and SQL Server do not. WHERE filters rows, HAVING filters groups, and QUALIFY filters on a window result (DuckDB, Snowflake, BigQuery, Databricks; elsewhere use an outer query). ORDER BY in a CTE does not fix the final order; LIMIT without ORDER BY returns any N rows. SQL Server OFFSET/FETCH requires ORDER BY.
Topics on the sheet
- Clause skeleton
- Logical execution order
- Where can I use a SELECT alias?
- WHERE vs HAVING vs QUALIFY
- No QUALIFY? Filter in an outer query
- Sorting and limiting
Download the PDF
SQL syntax and logical execution order as a one-page PDF in Letter and A4, plus all six sheets in one SQL cheat sheet PDF. Free PDF, download it right after you sign up. Signing up joins the dataengineers.club newsletter and unlocks the PDF bundle in Letter and A4. Unsubscribe anytime.
Clause skeleton
The comment on each clause is the step in which it runs.
WITH recent AS ( -- 0
SELECT * FROM orders
WHERE ts >= DATE '2025-01-01'
)
SELECT customer_id, -- 5
COUNT(*) AS n,
SUM(amount) AS revenue
FROM recent -- 1
WHERE status = 'paid' -- 2
GROUP BY customer_id -- 3
HAVING COUNT(*) >= 2 -- 4
ORDER BY revenue DESC -- 8
LIMIT 10; -- 9Step 0: a CTE is a named subquery, read where FROM uses it.
Logical execution order
| Step | Clause | What it does |
|---|---|---|
| 1 | FROM | tables, CTEs, joins |
| 2 | WHERE | keep rows |
| 3 | GROUP BY | one row per group |
| 4 | HAVING | keep groups |
| 5 | SELECT | columns, aliases, windows |
| 6 | QUALIFY | keep rows by a window result |
| 7 | DISTINCT | drop duplicate rows |
| 8 | ORDER BY | sort (aliases OK) |
| 9 | LIMIT | keep the first N rows |
The optimizer may run steps in another physical order, but the result is always as if it followed this one.
Where can I use a SELECT alias?
| Clause | SELECT alias allowed? |
|---|---|
| WHERE | No: it runs before SELECT. Repeat the expression or use a CTE. |
| GROUP BY | Not portable: PostgreSQL and DuckDB yes, SQL Server no. |
| HAVING | No: repeat the aggregate. |
| QUALIFY | Yes: it runs after SELECT. |
| ORDER BY | Yes, in every engine. |
DuckDB and Snowflake also accept aliases in WHERE and HAVING. BigQuery and MySQL accept them in HAVING. PostgreSQL and SQL Server do not. Portable fix: compute the alias in a CTE, then filter on it in the outer query.
WHERE vs HAVING vs QUALIFY
Top 3 customers by paid revenue, if over 100:
SELECT customer_id,
SUM(amount) AS revenue
FROM orders
WHERE status = 'paid' -- 2
GROUP BY customer_id
HAVING SUM(amount) > 100 -- 4
QUALIFY RANK() OVER (
ORDER BY SUM(amount) DESC
) <= 3; -- 6- WHERE filters rows, before grouping.
- HAVING filters groups, after GROUP BY.
- QUALIFY filters on a window result.
QUALIFY: DuckDB, Snowflake, BigQuery, Databricks. Not PostgreSQL, SQL Server or MySQL.
No QUALIFY? Filter in an outer query
SELECT *
FROM (
SELECT o.*,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY ts DESC, order_id
) AS rn
FROM orders o
) t
WHERE rn = 1; -- latest orderThe window runs in the inner query, so the outer WHERE can see rn. Same fix for "window function not allowed in WHERE".
Sorting and limiting
- ORDER BY in a CTE or subquery does not fix the final order. Sort in the outer query.
- LIMIT without ORDER BY returns any N rows.
- Ties at the cut: add a tiebreak column.
- SQL Server: SELECT TOP (10), or ORDER BY col OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY.
Two mistakes the order explains
From chapter 2 of the book. Window functions are computed with the SELECT list, which is why QUALIFY (where it exists) can use a SELECT alias. Two consequences come up again and again:
- You cannot filter on a window function in
WHERE, becauseWHEREruns before the window is computed. UseQUALIFYwhere it exists, or compute the window in a CTE and filter in the next step. HAVINGfilters groups andWHEREfilters rows. Putting a row filter inHAVING(or an aggregate inWHERE) is a common slip under pressure.
The real engine is free to execute things in a different physical order as long as the result is the same. The logical order is what decides what your query means.
Engines named on the sheet
DuckDB, PostgreSQL, Snowflake, BigQuery, SQL Server, Databricks, MySQL. DuckDB is the book's runtime. QUALIFY is noted for DuckDB, Snowflake, BigQuery and Databricks (not PostgreSQL, SQL Server or MySQL). Alias notes mention DuckDB, Snowflake, BigQuery, MySQL, PostgreSQL and SQL Server. SQL Server TOP and OFFSET/FETCH (with ORDER BY) are also noted. DuckDB 2.0.1 is the runtime used in the book. Do not read this list as a claim that every engine was executed for every example.
FAQ
What is the SQL order of execution?
The logical order is FROM (tables, CTEs, joins), WHERE, GROUP BY, HAVING, SELECT (columns, aliases and window functions), QUALIFY, DISTINCT, ORDER BY, then LIMIT. The optimizer may run steps in another physical order, but the result is always as if it followed this one.
Why can I not use a SELECT alias in WHERE?
WHERE runs before SELECT, so in most engines the alias does not exist yet. Repeat the expression, or compute the alias in a CTE and filter on it in the outer query. DuckDB and Snowflake also accept aliases in WHERE and HAVING, BigQuery and MySQL accept them in HAVING, and PostgreSQL and SQL Server do not.
What is the difference between WHERE, HAVING and QUALIFY?
WHERE filters rows before grouping. HAVING filters groups after GROUP BY. QUALIFY filters on a window result, after SELECT.
Which databases support QUALIFY?
QUALIFY works in DuckDB, Snowflake, BigQuery and Databricks. PostgreSQL, SQL Server and MySQL do not have it: compute the window in an inner query, then filter on it with WHERE in the outer query.
Does ORDER BY in a CTE sort the final result?
No. ORDER BY in a CTE or subquery does not fix the final order, so sort in the outer query. LIMIT without ORDER BY returns any N rows. On SQL Server, OFFSET and FETCH require ORDER BY.