PostgreSQL 17.12Lab-verified · 2026-11-29

PostgreSQL 42P20: window functions are not allowed in WHERE

A window function such as row_number() or rank() was used in WHERE, HAVING or UPDATE SET, where it cannot run.

PostgreSQL 17.12
42P20: window functions are not allowed in WHERE

Jump to a cause

  1. 01row_number() in WHERE to get the latest row per group
  2. 02rank() in HAVING to keep the top group
  3. 03row_number() in UPDATE SET to number rows

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
Setuprun as lab_a
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');
OUTPUT
CREATE TABLE
INSERT 0 4
Reproducerun as lab_a
SELECT id, customer_id, ordered_at FROM orders
WHERE row_number() OVER (PARTITION BY customer_id ORDER BY ordered_at DESC) = 1;
✕ ERROR
ERROR:  42P20: window functions are not allowed in WHERE
LINE 2: WHERE row_number() OVER (PARTITION BY customer_id ORDER BY o...
              ^
Checkrun as lab_a
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;
OUTPUT
 id | customer_id | rn
----+-------------+----
  2 |          10 |  1
  1 |          10 |  2
  4 |          20 |  1
  3 |          20 |  2
(4 rows)
After the fixrun as lab_a
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;
✓ SUCCESS
 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
Setuprun as lab_a
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');
OUTPUT
CREATE TABLE
INSERT 0 4
Reproducerun as lab_a
SELECT customer_id, sum(amount) AS total FROM orders
GROUP BY customer_id
HAVING rank() OVER (ORDER BY sum(amount) DESC) = 1;
✕ ERROR
ERROR:  42P20: window functions are not allowed in HAVING
LINE 3: HAVING rank() OVER (ORDER BY sum(amount) DESC) = 1;
               ^
Checkrun as lab_a
SELECT customer_id, sum(amount) AS total, rank() OVER (ORDER BY sum(amount) DESC) AS rnk FROM orders GROUP BY customer_id;
OUTPUT
 customer_id | total | rnk
-------------+-------+-----
          20 |   900 |   1
          10 |   800 |   2
(2 rows)
After the fixrun as lab_a
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;
✓ SUCCESS
 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
Setuprun as lab_a
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;
OUTPUT
CREATE TABLE
INSERT 0 4
ALTER TABLE
Reproducerun as lab_a
UPDATE orders SET seq = row_number() OVER (PARTITION BY customer_id ORDER BY ordered_at);
✕ ERROR
ERROR:  42P20: window functions are not allowed in UPDATE
LINE 1: UPDATE orders SET seq = row_number() OVER (PARTITION BY cust...
                                ^
Fixrun as lab_a
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;
OUTPUT
UPDATE 4
After the fixrun as lab_a
SELECT id, customer_id, ordered_at, seq FROM orders ORDER BY customer_id, seq;
✓ SUCCESS
 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