SQLSTATE 42P20 ERROR Class 42: Syntax Error or Access Rule Violation

windowing_error window functions are not allowed in WHERE — 42P20

PostgreSQL error "window functions are not allowed in WHERE" (SQLSTATE 42P20): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

Diagnostic Queries

Symptoms

A window function (using OVER) was used in a WHERE clause. Window functions run after WHERE, so PostgreSQL raises SQLSTATE 42P20 (windowing_error).

  • A function with OVER appears in WHERE/GROUP BY/HAVING.
  • Window functions execute after rows are selected and grouped.
  • Common when filtering on a row_number()/rank().

What the server log shows

ERROR:  window functions are not allowed in WHERE
LINE 1: ...SELECT * FROM t WHERE row_number() OVER (ORDER BY id) <= 10

Why PostgreSQL raises this — what the manual says

Section 3.5 Window Functions:

“They are forbidden elsewhere, such as in GROUP BY, HAVING and WHERE clauses.”

Window functions compute over the result set after WHERE, GROUP BY, and HAVING have been applied. Their values do not exist when WHERE runs, so PostgreSQL rejects them there with 42P20.

Common causes

  • Filtering directly on a window function result in WHERE.
  • Trying to keep top-N rows with a window function in WHERE.
  • Confusing window evaluation order with row filtering.

How to fix it

  1. Wrap the window function in a subquery/CTE, then filter in the outer query.
  2. Example: SELECT * FROM (SELECT *, row_number() OVER (ORDER BY id) rn FROM t) s WHERE rn <= 10;.
  3. Use LIMIT/FETCH for simple top-N without window functions.

Related & next steps

Reference: PostgreSQL 18 Section 3.5 “Window Functions”.

Was this helpful?