Why can't you use a SELECT alias in the WHERE clause, but you can in ORDER BY?
In standard SQL (and PostgreSQL, SQL Server) the logical order is FROM, WHERE, GROUP BY, HAVING, window functions, SELECT, DISTINCT, ORDER BY, LIMIT. WHERE runs before SELECT creates the alias, so the alias is unknown there. ORDER BY runs after SELECT, so it can use it.
The logical processing order of a SQL query
You write SELECT first, but the database logically evaluates the clauses in a different order:
FROM/JOIN(build the row source)WHERE(filter rows)GROUP BY(form groups)HAVING(filter groups)- Window functions (
OVER) SELECT(compute expressions and create aliases)DISTINCTQUALIFY(in engines that have it: Snowflake, BigQuery, Teradata, Databricks; filters on window results)ORDER BYLIMIT/OFFSET/FETCH
An alias is defined in step 6. WHERE is step 2, so when it runs, the alias does not exist yet. ORDER BY is step 9, after the alias exists, so it is allowed.
Example
-- Fails in PostgreSQL / SQL Server: column "total" does not exist
SELECT price * qty AS total
FROM order_items
WHERE total > 1000;
-- Fix 1: repeat the expression
SELECT price * qty AS total
FROM order_items
WHERE price * qty > 1000;
-- Fix 2: compute it in a subquery or CTE, then filter
WITH t AS (SELECT price * qty AS total FROM order_items)
SELECT total FROM t WHERE total > 1000;
-- Works everywhere: ORDER BY can use the alias
SELECT price * qty AS total FROM order_items ORDER BY total DESC;
The rule is engine dependent
The logical order is the standard model, and PostgreSQL and SQL Server follow it strictly for WHERE. Some engines add extra convenience:
- MySQL allows aliases in
GROUP BYandHAVING, but not inWHERE. - Snowflake, BigQuery and Databricks let you reuse a select alias in
WHERE,GROUP BYandHAVING(the engine rewrites it).QUALIFYexists there to filter window results. - PostgreSQL allows aliases in
GROUP BYandORDER BY, but not inWHEREorHAVING. Check each engine's documentation, and do not depend on this for portable code.
Why it matters
- It explains why you cannot filter on a window function in
WHERE(windows run afterWHERE); use a subquery orQUALIFY. - It explains why
HAVINGcan use aggregates butWHEREcannot (groups do not exist at step 2). - It explains why
ORDER BYcan sort by columns you did not select (for non-DISTINCT queries).
Note that this is the logical order. The optimizer may physically reorder operations (push filters down, for example) as long as the result is identical.
- 1FROM / JOIN
- 2WHEREalias does not exist yet
- 3GROUP BY
- 4HAVING
- 5SELECTalias created here
- 6DISTINCT
- 7ORDER BYalias visible
- 8LIMIT
See the concept: Join fan-outWhy a join to a one-to-many table multiplies sums
| O101 | line 1 | ₹300 |
| O101 | P101 | FAILED |
| O101 | P102 | SUCCESS |
| line 1 | P101 | ₹300 |
| line 1 | P102 | ₹300 |
Wrong: the single ₹300 line is counted once per payment attempt.
Fix: aggregate payments to one row per order first
line 1, paid, ₹300, so SUM = ₹300
Key points
- Logical order: FROM, WHERE, GROUP BY, HAVING, window functions, SELECT, DISTINCT, ORDER BY, LIMIT
- Aliases are created in the SELECT step, so WHERE (earlier) cannot see them
- ORDER BY runs after SELECT, so it can use aliases
- MySQL, Snowflake, BigQuery and Databricks relax the rule in some clauses
- Window functions cannot be filtered in WHERE; use a subquery or QUALIFY
Common mistakes
- Assuming the written order of clauses is the execution order
- Using an alias in WHERE on PostgreSQL or SQL Server
- Filtering on a window function in WHERE
- Using an aggregate in WHERE instead of HAVING
- Assuming the alias rules are identical on every engine
Prerequisites
- Basic SELECT, WHERE and GROUP BY
Best practices
- Use a CTE or subquery when a filter needs a computed value
- Avoid relying on engine-specific alias reuse in portable SQL
- Use HAVING for conditions on aggregates and WHERE for row conditions
Likely follow-ups
- Why can't you use a window function in the WHERE clause?
- What is the difference between WHERE and HAVING?
- What does QUALIFY do and which engines support it?
- Does the optimizer have to follow the logical processing order?