sqlstreak

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.

Broken
SELECT name, price, ROW_NUMBER() AS position
FROM menu_items;
Fixed
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

namepriceposition
BBQ Meteor15.51
Four Cheese Orbit142
Pepperoni Rocket13.53
Veggie Nebula134
Margherita115
Wings8.56

The fix returns, SQLite: first 6 of 11 rows

namepriceposition
BBQ Meteor15.51
Four Cheese Orbit142
Pepperoni Rocket13.53
Veggie Nebula134
Margherita115
Wings8.56

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

categorynamepricerank_in_category
dessertTiramisu6.51
drinkLemon Soda31
drinkCola2.52
pizzaBBQ Meteor15.51
pizzaFour Cheese Orbit142
pizzaPepperoni Rocket13.53

Returns, SQLite: first 6 of 11 rows

categorynamepricerank_in_category
dessertTiramisu6.51
drinkLemon Soda31
drinkCola2.52
pizzaBBQ Meteor15.51
pizzaFour Cheese Orbit142
pizzaPepperoni Rocket13.53

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.