Skip to main content
Intermediatehow it works Verified ~7 min

Why can't you use a SELECT alias in the WHERE clause, but you can in ORDER BY?

Source: PostgreSQL: SELECT (clause processing)

Quick answer

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:

  1. FROM / JOIN (build the row source)
  2. WHERE (filter rows)
  3. GROUP BY (form groups)
  4. HAVING (filter groups)
  5. Window functions (OVER)
  6. SELECT (compute expressions and create aliases)
  7. DISTINCT
  8. QUALIFY (in engines that have it: Snowflake, BigQuery, Teradata, Databricks; filters on window results)
  9. ORDER BY
  10. LIMIT / 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

query.sql
-- 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 BY and HAVING, but not in WHERE.
  • Snowflake, BigQuery and Databricks let you reuse a select alias in WHERE, GROUP BY and HAVING (the engine rewrites it). QUALIFY exists there to filter window results.
  • PostgreSQL allows aliases in GROUP BY and ORDER BY, but not in WHERE or HAVING. 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 after WHERE); use a subquery or QUALIFY.
  • It explains why HAVING can use aggregates but WHERE cannot (groups do not exist at step 2).
  • It explains why ORDER BY can 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.

Animated explanation
  1. 1FROM / JOIN
  2. 2WHEREalias does not exist yet
  3. 3GROUP BY
  4. 4HAVING
  5. 5SELECTalias created here
  6. 6DISTINCT
  7. 7ORDER BYalias visible
  8. 8LIMIT
Logical evaluation order: an alias exists only from SELECT onwards.
See the concept: Join fan-outWhy a join to a one-to-many table multiplies sums
Join fan-out
order_lines: one row per line
O101line 1₹300
payments: one row per attempt
O101P101FAILED
O101P102SUCCESS
lines JOIN payments ON order_id
line 1P101₹300
line 1P102₹300
SUM(line_revenue)₹600

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

One order line, two payment attempts: a direct join doubles the revenue. Aggregating payments to order grain first keeps one row per line.

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?

References

Discussion