SQL error
window function row_number requires an OVER clause / misuse of window function
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
ROW_NUMBER(), RANK(), LAG() and the other window functions only work with OVER (…), which says how to order (and optionally group) the rows they look at: ROW_NUMBER() OVER (ORDER BY price DESC).
The exact messages
- PostgreSQLERROR: window function row_number requires an OVER clause
- SQLitemisuse of window function ROW_NUMBER()
How to read this message
This is what PostgreSQL 18.6 prints for the broken query below, the way psql shows it. LINE gives the line it was reading when it gave up, and the ^ marks the exact spot.
ERROR: window function row_number requires an OVER clause
LINE 1: SELECT name, price, ROW_NUMBER() AS position
^SQLite 3.54.1 reports the same mistake as misuse of window function ROW_NUMBER(), with no line number: look for the word or column it names.
Why it happens
OVER left out
ROW_NUMBER() on its own has no order to number in. Even an empty OVER () is accepted, but for numbering you almost always want an ORDER BY inside it.
Treating it like COUNT
COUNT and SUM work without OVER because they're aggregates. Ranking and offset functions exist only as window functions.
Reproduce it, then fix it
Both queries run on a real database in your browser. Switch dialects to compare.
SELECT name, price, ROW_NUMBER() AS position
FROM menu_items;SELECT name, price,
ROW_NUMBER() OVER (ORDER BY price DESC, name) AS position
FROM menu_items;Numbered from the most expensive item down; name breaks ties so the numbering is the same every time.
The fix returns, PostgreSQL: first 6 of 11 rows
| name | price | position |
|---|---|---|
| BBQ Meteor | 15.5 | 1 |
| Four Cheese Orbit | 14 | 2 |
| Pepperoni Rocket | 13.5 | 3 |
| Veggie Nebula | 13 | 4 |
| Margherita | 11 | 5 |
| Wings | 8.5 | 6 |
The fix returns, SQLite: first 6 of 11 rows
| name | price | position |
|---|---|---|
| BBQ Meteor | 15.5 | 1 |
| Four Cheese Orbit | 14 | 2 |
| Pepperoni Rocket | 13.5 | 3 |
| Veggie Nebula | 13 | 4 |
| Margherita | 11 | 5 |
| Wings | 8.5 | 6 |
Experiment with it: PostgreSQL playground·SQLite playground
Another fix
Add PARTITION BY to restart the numbering for each group.
SELECT category, name, price,
ROW_NUMBER() OVER (PARTITION BY category ORDER BY price DESC, name) AS rank_in_category
FROM menu_items;Returns, PostgreSQL: first 6 of 11 rows
| category | name | price | rank_in_category |
|---|---|---|---|
| dessert | Tiramisu | 6.5 | 1 |
| drink | Lemon Soda | 3 | 1 |
| drink | Cola | 2.5 | 2 |
| pizza | BBQ Meteor | 15.5 | 1 |
| pizza | Four Cheese Orbit | 14 | 2 |
| pizza | Pepperoni Rocket | 13.5 | 3 |
Returns, SQLite: first 6 of 11 rows
| category | name | price | rank_in_category |
|---|---|---|---|
| dessert | Tiramisu | 6.5 | 1 |
| drink | Lemon Soda | 3 | 1 |
| drink | Cola | 2.5 | 2 |
| pizza | BBQ Meteor | 15.5 | 1 |
| pizza | Four Cheese Orbit | 14 | 2 |
| pizza | Pepperoni Rocket | 13.5 | 3 |
Other forms of this mistake
Each one was run when this page was built; the result is what the engine really does.
SELECT ordered_on, LAG(ordered_on) AS previous FROM orders;PostgreSQLERROR: window function lag requires an OVER clause
The same for LAG, LEAD, RANK and friends.
Official documentation
Stop hitting this error
Practise the concept behind it in a five-minute lesson, or read the full guide: SQL window functions.