The execution order SQL actually follows — not the order you write it in
The single most common SQL mistake — using an alias defined in SELECT inside WHERE — comes from not knowing the logical execution order. SQL doesn't run top-to-bottom like Python. WHERE runs before SELECT, which means column aliases from SELECT don't exist yet when WHERE is evaluated. Knowing the order explains every "column does not exist" error.
SQL's logical execution order: FROM → JOIN → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT. You write SELECT first, but it runs near last. This explains why WHERE can't reference SELECT aliases, why HAVING filters after aggregation, and why ORDER BY can use SELECT aliases (it runs after SELECT).
NULL propagates through arithmetic and comparisons — any expression involving NULL produces NULL. COALESCE is the standard way to substitute a default value for NULLs.
Conditional aggregation (COUNT(CASE WHEN ...)) is a key pattern for pivot-style reports — counting or summing only rows that meet a condition, without a subquery.