Stop 08 · Window FunctionsTopics · 3/9

ROW_NUMBER

Part of the SQL Roadmap

In short

"Find each customer's first order" or "keep only the latest address" are everyday asks. ROW_NUMBER numbers the rows in each group so you can pick exactly the one you want.

ROW_NUMBER gives each row a unique sequential number within its partition, based on the ORDER BY inside OVER. Ties still get different numbers.

The jaadu stands at a clinic reception tearing numbered paper tokens from two separate rolls and handing them to patients lined up in two queues.

Think of it like this

Clinic tokens for each doctor

At a busy Lucknow clinic, each doctor has their own token roll. Dr Mehra's patients get 1, 2, 3 in order of arrival, and Dr Khan's patients start again at 1. If two people walk in at the same minute, they still get different tokens. ROW_NUMBER works the same way: unique numbers per partition, no sharing.

Watch it happen

ROW_NUMBER() OVER (PARTITION BY customer ORDER BY order_date)

Ananya 3 Jan
0
Ananya 9 Feb
1
Ananya 9 Feb
2
Vikram 5 Jan
3
Vikram 20 Mar
4

1/5Five orders, already split by customer and sorted by date inside each customer.

The big ideas

What you need to know

Always unique

ROW_NUMBER gives 1, 2, 3 with no repeats inside a partition, even when the sort values are tied.

ORDER BY decides who is 1

ORDER BY order_date gives the first order. ORDER BY order_date DESC gives the latest one.

Add a tie-breaker

If dates can repeat, add a unique column like order_id to the ORDER BY so the result is the same every run.

Filter outside

You can't use a window function in WHERE. Number the rows in a CTE, then filter WHERE rn = 1 outside it.

See it in code

Walk through the code

sql
1WITH numbered AS (2  SELECT3    customer_id,4    order_id,5    order_date,6    amount,7    ROW_NUMBER() OVER (8      PARTITION BY customer_id9      ORDER BY order_date, order_id10    ) AS rn11  FROM orders12)13SELECT customer_id, order_id, order_date, amount14FROM numbered15WHERE rn = 1;

1/3A CTE holds the numbered rows so we can filter them afterwards.

Each customer's first ever order. Runs on PostgreSQL, MySQL 8+ and SQL Server. In PostgreSQL alone, SELECT DISTINCT ON (customer_id) is a shortcut for the rn = 1 case.

Save yourself the pain

Common mistakes

Oops

Writing WHERE ROW_NUMBER() OVER (...) = 1 in the same query.

Do this instead

WHERE runs before window functions exist. Compute rn in a CTE or subquery, then filter it outside.

Oops

Ordering only by a date that can repeat.

Do this instead

Add a unique tie-breaker like order_id, or the 'first' row can change between runs.

Oops

Using ROW_NUMBER when ties should share a place.

Do this instead

If two products with equal sales both deserve rank 1, use RANK or DENSE_RANK instead.

Your mission

Latest address per customer

~20 min

Quick check

Why does WHERE ROW_NUMBER() OVER (ORDER BY amount) = 1 fail?

Go deeper

  1. 1Find each customer's first order with ROW_NUMBER and a filter on 1.
  2. 2Use it to remove duplicates by keeping only row number 1.
  3. 3Remember you must filter it in an outer query or CTE, not in WHERE directly.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge