IntermediateWindow functions

Can you filter on a window function in WHERE?

Scenario

You need the latest order of each customer, using ROW_NUMBER, and try to filter rn = 1 in WHERE.

Interview answerTry answering first, then reveal

No. WHERE is evaluated before window functions are computed, so the column does not exist yet. Compute the window in a CTE or subquery and filter outside it, or use QUALIFY where it is supported (Databricks, Snowflake, BigQuery, DuckDB, Teradata).

Explanation

SQL runs clauses in a logical order: FROM and joins, WHERE, GROUP BY, HAVING, window functions, SELECT, DISTINCT, ORDER BY, LIMIT. Window functions run after WHERE and HAVING, so neither can reference them.

The portable fix is a CTE or derived table: compute the row number inside, filter outside. QUALIFY is a filter that runs after window functions, so it does the same thing in one statement.

Filtering in WHERE first and then numbering gives a different answer: WHERE status = 'paid' before ROW_NUMBER finds the latest paid order, which is not the same question as "is the latest order paid?".

Worked example

Run on DuckDB when this page was built; the results shown are the real output.

Sample data
SQL
CREATE TABLE orders (order_id INT, customer_id INT, order_ts TIMESTAMP, status VARCHAR);
INSERT INTO orders VALUES
  (1, 10, '2026-01-05 09:00', 'paid'),   (2, 10, '2026-01-09 14:30', 'refunded'),
  (3, 20, '2026-01-03 11:15', 'paid'),   (4, 20, '2026-01-07 16:45', 'paid'),
  (5, 30, '2026-01-08 10:00', 'paid');

Filtering in WHERE fails

SQL
SELECT order_id, customer_id,
       ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_ts DESC) AS rn
FROM orders
WHERE rn = 1;
Error
Binder Error: WHERE clause cannot contain window functions!

Portable fix: a CTE

SQL
WITH ranked AS (
  SELECT *, ROW_NUMBER() OVER (PARTITION BY customer_id
                               ORDER BY order_ts DESC, order_id DESC) AS rn
  FROM orders
)
SELECT order_id, customer_id, order_ts, status
FROM ranked
WHERE rn = 1
ORDER BY customer_id;
Result
order_idcustomer_idorder_tsstatus
2102026-01-09 14:30refunded
4202026-01-07 16:45paid
5302026-01-08 10:00paid

Same result with QUALIFY

SQL
SELECT order_id, customer_id, order_ts, status
FROM orders
QUALIFY ROW_NUMBER() OVER (PARTITION BY customer_id
                           ORDER BY order_ts DESC, order_id DESC) = 1
ORDER BY customer_id;
Result
order_idcustomer_idorder_tsstatus
2102026-01-09 14:30refunded
4202026-01-07 16:45paid
5302026-01-08 10:00paid

In production

  • In Databricks and Snowflake QUALIFY is idiomatic and easy to read in latest-record and deduplication logic.
  • In PostgreSQL, SELECT DISTINCT ON (customer_id) ... ORDER BY customer_id, order_ts DESC is a shortcut for "first row per group".
  • For huge tables, check whether you can filter the input first (for example to the last 30 days) before the window, as long as that does not change the meaning.

Common mistakes

  • Trying HAVING rn = 1: HAVING also runs before window functions.
  • Moving a condition from outside the CTE to inside it, which changes the question being answered.
  • Ordering by a timestamp with ties and no tie-breaker, so "latest" changes between runs.

More on this topic in the SQL Interview Questions guide, or browse all SQL questions.