← Back to SQL, page 6

sql-0120 · Medium mid

Why does WHERE ROW_NUMBER() OVER (...) = 1 fail, and how do you filter on a window function result?

Window functions are computed after WHERE, GROUP BY and HAVING, so they do not exist yet when WHERE runs, and the engine rejects the query. Compute the window in a subquery or CTE and filter in the outer query. Some engines, such as Snowflake, BigQuery and DuckDB, add a QUALIFY clause for this; SQLite and PostgreSQL do not.

Full answer
sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
SELECT name FROM employees
WHERE ROW_NUMBER() OVER (ORDER BY salary DESC) = 1;
-- error on purpose
text
Error: misuse of window function ROW_NUMBER()

PostgreSQL gives a clearer message:

sql
SELECT name FROM employees
WHERE ROW_NUMBER() OVER (ORDER BY salary DESC) = 1;
-- error on purpose
text
Error: window functions are not allowed in WHERE

The logical order is FROM, WHERE, GROUP BY, HAVING, then window functions, then SELECT, ORDER BY and LIMIT. WHERE cannot filter on something that is computed later.

Wrap the window in a CTE (or a derived table) and filter outside:

sql
WITH ranked AS (
  SELECT name, salary, ROW_NUMBER() OVER (ORDER BY salary DESC NULLS LAST, name) AS rn
  FROM employees
)
SELECT name, salary FROM ranked WHERE rn = 1;
namesalary
Asha120000

This also lets you filter on several window results at once and keeps the ranking logic readable. QUALIFY, where it exists, is shorthand for exactly this pattern (... QUALIFY ROW_NUMBER() OVER (...) = 1); it is not standard SQL and was not run here.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

-- error on purpose
SELECT name FROM employees
WHERE ROW_NUMBER() OVER (ORDER BY salary DESC) = 1;

WITH ranked AS (
  SELECT name, salary, ROW_NUMBER() OVER (ORDER BY salary DESC NULLS LAST, name) AS rn
  FROM employees
)
SELECT name, salary FROM ranked WHERE rn = 1;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • Window functions are evaluated after WHERE and HAVING
  • They cannot appear in WHERE, GROUP BY or HAVING
  • Filter them in an outer query or CTE
  • QUALIFY exists in some warehouses but not in SQLite or PostgreSQL

Common mistakes

  • Moving the window function into HAVING, which fails for the same reason
  • Writing the filter as a correlated subquery when a CTE is clearer

Follow-ups

  • Can ORDER BY use a window function?
  • How would you keep only the latest order per customer?

Tips

  • Quote the logical order to explain the error