Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17
Window functions (row_number(), rank(), sum() OVER …) are computed after WHERE, GROUP BY and HAVING have done their work, so PostgreSQL does not allow them in those clauses. The message names the clause: in WHERE, in HAVING or in UPDATE, and LINE 2: ... ^ points at the window function.
The fix is always the same idea: compute the window function in a subquery or CTE first, give it an alias, and filter or join on that alias outside.
01row_number() in WHERE to get the latest row per group
To get each customer's latest order, WHERE row_number() OVER (PARTITION BY customer_id ORDER BY ordered_at DESC) = 1 failed with 42P20: window functions are not allowed in WHERE.
In the SELECT list the same expression works and gave rn 1 to orders 2 and 4. Wrapping it in a subquery (SELECT ... FROM (SELECT o.*, row_number() OVER (...) AS rn FROM orders o) t WHERE rn = 1) returned order 2 for customer 10 and order 4 for customer 20.
Setup
CREATE TABLE orders (id int PRIMARY KEY, customer_id int, amount numeric, ordered_at date);
INSERT INTO orders VALUES (1, 10, 500, '2024-05-01'), (2, 10, 300, '2024-05-03'), (3, 20, 700, '2024-05-02'), (4, 20, 200, '2024-05-04');CREATE TABLE
INSERT 0 4SELECT id, customer_id, ordered_at FROM orders
WHERE row_number() OVER (PARTITION BY customer_id ORDER BY ordered_at DESC) = 1;ERROR: 42P20: window functions are not allowed in WHERE
LINE 2: WHERE row_number() OVER (PARTITION BY customer_id ORDER BY o...
^SELECT id, customer_id, row_number() OVER (PARTITION BY customer_id ORDER BY ordered_at DESC) AS rn FROM orders ORDER BY customer_id, rn; id | customer_id | rn
----+-------------+----
2 | 10 | 1
1 | 10 | 2
4 | 20 | 1
3 | 20 | 2
(4 rows)SELECT id, customer_id, ordered_at FROM (
SELECT o.*, row_number() OVER (PARTITION BY customer_id ORDER BY ordered_at DESC) AS rn FROM orders o
) t
WHERE rn = 1
ORDER BY customer_id; id | customer_id | ordered_at
----+-------------+------------
2 | 10 | 2024-05-03
4 | 20 | 2024-05-04
(2 rows)02rank() in HAVING to keep the top group
GROUP BY customer_id HAVING rank() OVER (ORDER BY sum(amount) DESC) = 1 failed with window functions are not allowed in HAVING.
In the SELECT list rank() ranked customer 20 (900) first and customer 10 (800) second. Putting that query in a CTE and filtering WHERE rnk = 1 outside returned customer 20 with 900.
Setup
CREATE TABLE orders (id int PRIMARY KEY, customer_id int, amount numeric, ordered_at date);
INSERT INTO orders VALUES (1, 10, 500, '2024-05-01'), (2, 10, 300, '2024-05-03'), (3, 20, 700, '2024-05-02'), (4, 20, 200, '2024-05-04');CREATE TABLE
INSERT 0 4SELECT customer_id, sum(amount) AS total FROM orders
GROUP BY customer_id
HAVING rank() OVER (ORDER BY sum(amount) DESC) = 1;ERROR: 42P20: window functions are not allowed in HAVING
LINE 3: HAVING rank() OVER (ORDER BY sum(amount) DESC) = 1;
^SELECT customer_id, sum(amount) AS total, rank() OVER (ORDER BY sum(amount) DESC) AS rnk FROM orders GROUP BY customer_id; customer_id | total | rnk
-------------+-------+-----
20 | 900 | 1
10 | 800 | 2
(2 rows)WITH t AS (
SELECT customer_id, sum(amount) AS total, rank() OVER (ORDER BY sum(amount) DESC) AS rnk
FROM orders GROUP BY customer_id
)
SELECT customer_id, total FROM t WHERE rnk = 1; customer_id | total
-------------+-------
20 | 900
(1 row)03row_number() in UPDATE SET to number rows
UPDATE orders SET seq = row_number() OVER (PARTITION BY customer_id ORDER BY ordered_at) failed with window functions are not allowed in UPDATE.
Computing the numbers in a subquery and joining it with UPDATE orders o SET seq = t.rn FROM (SELECT id, row_number() OVER (...) AS rn FROM orders) t WHERE o.id = t.id updated 4 rows. Each customer's orders got seq 1 and 2 by date.
Setup
CREATE TABLE orders (id int PRIMARY KEY, customer_id int, amount numeric, ordered_at date);
INSERT INTO orders VALUES (1, 10, 500, '2024-05-01'), (2, 10, 300, '2024-05-03'), (3, 20, 700, '2024-05-02'), (4, 20, 200, '2024-05-04');
ALTER TABLE orders ADD COLUMN seq int;CREATE TABLE
INSERT 0 4
ALTER TABLEUPDATE orders SET seq = row_number() OVER (PARTITION BY customer_id ORDER BY ordered_at);ERROR: 42P20: window functions are not allowed in UPDATE
LINE 1: UPDATE orders SET seq = row_number() OVER (PARTITION BY cust...
^UPDATE orders o SET seq = t.rn
FROM (SELECT id, row_number() OVER (PARTITION BY customer_id ORDER BY ordered_at) AS rn FROM orders) t
WHERE o.id = t.id;UPDATE 4SELECT id, customer_id, ordered_at, seq FROM orders ORDER BY customer_id, seq; id | customer_id | ordered_at | seq
----+-------------+------------+-----
1 | 10 | 2024-05-01 | 1
2 | 10 | 2024-05-03 | 2
3 | 20 | 2024-05-02 | 1
4 | 20 | 2024-05-04 | 2
(4 rows)FAQ
How do I filter on row_number() in PostgreSQL?
Compute row_number() in a subquery or CTE with an alias, then filter on the alias outside, such as WHERE rn = 1. That is how the latest order per customer was returned above.
Can I use a window function in HAVING?
No. HAVING runs before window functions are computed. Put the GROUP BY query with rank() in a CTE and filter in the outer WHERE, as in the HAVING case above.
How do I UPDATE a column with row_number()?
Use UPDATE ... FROM with a subquery that computes row_number() and join on the primary key. In the run above this updated 4 rows.
Can I use the column alias of a window function in WHERE?
Not in the same query level. The alias rn worked only in the outer query that selects from the subquery.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12