Function or operator · Advanced SQL

ROW_NUMBER

ROW_NUMBER numbers the rows from 1 following the window's ORDER BY. PARTITION BY restarts the numbering for each group.

Before continuing: This entry is part of the window functions guide. If you are new to the topic, start with OVER.

ROW_NUMBER example

Numbering products within each category
SELECT category, name, price,
       ROW_NUMBER() OVER (
         PARTITION BY category
         ORDER BY price DESC, id
       ) AS position
FROM products
WHERE category IN ('Accessories', 'Displays')
ORDER BY category, position;

The sequence restarts whenever the category changes. Within each category, the most expensive product is number 1.

Check the result with small data

Each group needs its own numbering.

Self-contained query · SQLite
WITH notes(student, group_label, note) AS (
  VALUES ('Amy','A',9),('Liam','A',7),('Eva','B',8)
)
SELECT student, group_label,
       ROW_NUMBER() OVER (PARTITION BY group_label ORDER BY note DESC) AS position
FROM notes ORDER BY group_label, position;
Query result
studentgroup_labelposition
AmyA1
LiamA2
EvaB1

ROW_NUMBER restarts the sequence when the group changes. Ties need a second, stable sort key if it matters which row comes first.

Check your understanding: What number does Eva get within her group?

Show answer

1: Eva is the only one in group B and ROW_NUMBER restarts the numbering in each partition.

How ROW_NUMBER is used

PARTITION BY is optional. For reproducible numbering, define ORDER BY within OVER: SQL Server requires it; SQLite lets you leave it out but then numbers the rows in an arbitrary order, and PostgreSQL does not guarantee a stable order either unless you specify one. Adding a stable tiebreaker, such as the primary key, stops two tied rows from swapping places between runs.

Common mix-ups with ROW_NUMBER

Sorting only by a column that can have ties

If two products have the same price and the ORDER BY clause only uses price DESC, the engine has no complete rule for deciding which comes first.

Add id as a second key does not change the order by price; it only breaks ties in a stable way.

Picking a row with a reproducible rule

Write down the selection rule before numbering: “the most recent order; if two share the same date, the one with the highest id”. That second part turns a date tie into a reproducible choice, provided the id is unique. If you sort by date alone, tied rows can swap places, and keeping position 1 no longer reflects a complete preference.

The partition sets where the numbering starts again. Without PARTITION BY customer_id, you get one global sequence, and keeping number 1 leaves a single order from the whole set. With that partition, you get one per customer who has orders. Customers without orders do not appear automatically: if they also belong in the report, you need to decide how to bring them in.

The numbering does not tell you how many distinct values there are: two equal amounts get different numbers. If tied rows should share a position, compare with the ranking functions and decide which policy you need. Also review the filters applied first: numbering only paid orders picks the latest payment, whereas numbering every order and filtering afterwards can exclude a customer whose latest order was cancelled. The window's sort order and the final presentation are separate decisions.

Technical questions: SQLite: window functions (opens in a new tab).