SQL error · GROUP BY and calculations

ROW_NUMBER does not work in WHERE

Problem: The window is computed after that level's WHERE filter.

Solution: Compute the window in a subquery or CTE and filter on the position from outside.

Why it happens: ROW_NUMBER does not work in WHERE

Problem query
SELECT customer_id,
       date,
       ROW_NUMBER() OVER (
         PARTITION BY customer_id
         ORDER BY date DESC, id DESC
       ) AS position
FROM orders
WHERE ROW_NUMBER() OVER (
        PARTITION BY customer_id
        ORDER BY date DESC, id DESC
      ) = 1;

The intent is reasonable, but the WHERE stage does not have the window's result yet.

“ROW_NUMBER does not work in WHERE” error messages by engine

The exact wording depends on the database. Compare these common messages with the one in your console.

EngineTypical message
PostgreSQLERROR: window functions are not allowed in WHERE
MySQLERROR 3593 (HY000): You cannot use the window function 'row_number' in this context.
SQL ServerMsg 4108, Level 15: Windowed functions can only appear in the SELECT or ORDER BY clauses.
OracleORA-30483: window functions are not allowed here
SQLiteError: misuse of window function ROW_NUMBER()

How to fix it in SQL

Compute the window in an inner layer

Fixed subquery
SELECT customer_id, date
FROM (
  SELECT customer_id,
         date,
         ROW_NUMBER() OVER (
           PARTITION BY customer_id
           ORDER BY date DESC, id DESC
         ) AS position
  FROM orders
) AS sorted
WHERE position = 1;

The inner query produces a virtual table that already contains position. In the outer query, that name is an ordinary column and WHERE can filter it.

The same approach with a CTE

CTE
WITH sorted AS (
  SELECT customer_id,
         date,
         ROW_NUMBER() OVER (
           PARTITION BY customer_id
           ORDER BY date DESC, id DESC
         ) AS position
  FROM orders
)
SELECT customer_id, date
FROM sorted
WHERE position = 1;

The CTE does not change the logic: it just makes the split between calculating and filtering more visible.

How to check it

Keep the position column at first, and check the order and any ties within each group.

How to avoid it next time

HAVING cannot stand in for the outer query when filtering a window.

Related cases worth checking

If several rows can share the same date, add a stable tiebreaker such as id DESC. Otherwise, which of the tied rows gets ROW_NUMBER() = 1 may not be a deterministic choice.

Filtering before or after numbering answers different questions

Splitting the query in two is not just a trick to avoid an error message: it decides which rows take part in the numbering. If you want each customer's latest paid order, the status filter goes inside the query that computes the window. If you want customers whose latest order, whatever its status, is paid, number every order and filter on status in the outer query together with the position.

Take a customer with an order paid on the 10th and another cancelled on the 12th. The first report shows the order from the 10th. In the second report, that customer does not appear at all, because their latest order is cancelled. Both queries run without errors and give different results; checking only that the error message has gone does not validate the business question.

Keep the order key in the diagnostic output so you can see which order won, not just its date. The ordering inside OVER decides the position, but on its own it does not sort the final report. Add an outer ORDER BY if you need a stable presentation. If every order tied on the most recent date should survive, consider a ranking function instead of picking a single row.

Self-contained case to check in SQLite

The data contains an older payment and a later cancellation from the same customer. Here we ask for their latest paid order.

Data and query · SQLite
WITH orders(id, customer_id, date, status) AS (
  VALUES (10, 1, '2026-10-10', 'paid'),
         (12, 1, '2026-10-12', 'cancelled')
), sorted AS (
  SELECT id, customer_id, status,
         ROW_NUMBER() OVER (
           PARTITION BY customer_id ORDER BY date DESC, id DESC
         ) AS position
  FROM orders WHERE status = 'paid'
)
SELECT id, customer_id, status FROM sorted WHERE position = 1;
Verified result of the self-contained case
idcustomer_idstatus
101paid

The cancellation does not take part in the window because the filter is inside. Numbering both orders first and then filtering on status and position would return zero rows.

Check your interpretation: Which order gets position 1 if you remove the filter from the inner query?

Show answer

Order 12, the cancellation, because it is the customer's most recent order. If you then filter status = 'paid' outside, the customer disappears from the result.

Technical questions: SQLite: window functions (opens in a new tab).