# window function row_number requires an OVER clause / misuse of window function

Source: https://sqlstreak.com/sql/errors/window-function-requires-an-over-clause · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**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

- PostgreSQL: `ERROR: window function row_number requires an OVER clause`
- SQLite: `misuse of window function ROW_NUMBER()`

## How PostgreSQL prints it

```
ERROR:  window function row_number requires an OVER clause
LINE 1: SELECT name, price, ROW_NUMBER() AS position
                            ^
```

## 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.

## The broken query

```sql
SELECT name, price, ROW_NUMBER() AS position
FROM menu_items;
```

## The fix

```sql
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.

| 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 |

(6 of 11 rows)
