SQL how-to
How to get the first or latest row per group in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
Number each group's rows newest first with ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY ordered_on DESC, id DESC) and keep row 1. PostgreSQL can do the same with DISTINCT ON (customer_id).
The task: Keep one row per group: the first or the most recent, such as each customer's latest order.
Each customer's latest order
SELECT customer_id, id AS order_id, ordered_on, status
FROM (SELECT o.*,
ROW_NUMBER() OVER (PARTITION BY customer_id
ORDER BY ordered_on DESC, id DESC) AS rn
FROM orders o) t
WHERE rn = 1
ORDER BY customer_id;Result (PostgreSQL and SQLite, identical): 8 rows
| customer_id | order_id | ordered_on | status |
|---|---|---|---|
| 1 | 16 | 2026-03-22 | preparing |
| 2 | 13 | 2026-03-15 | delivered |
| 3 | 4 | 2026-03-04 | cancelled |
| 4 | 11 | 2026-03-12 | cancelled |
| 5 | 14 | 2026-03-18 | delivered |
| 6 | 15 | 2026-03-20 | delivered |
| 7 | 10 | 2026-03-11 | delivered |
| 8 | 17 | 2026-03-22 | preparing |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
PARTITION BY customer_idOne numbering per customer.
- 2
ORDER BY ordered_on DESC, id DESCNewest first; the id decides between two orders on the same day.
- 3
WHERE rn = 1Keep each customer's newest order and drop the rest.
More examples
PostgreSQL's shortcut: DISTINCT ON
Keeps the first row of each customer_id in ORDER BY order. SQLite has no DISTINCT ON, so it needs the ROW_NUMBER form.
PostgreSQL
SELECT DISTINCT ON (customer_id)
customer_id, id AS order_id, ordered_on, status
FROM orders
ORDER BY customer_id, ordered_on DESC, id DESC;SQLite
SELECT customer_id, id AS order_id, ordered_on, status
FROM (SELECT o.*,
ROW_NUMBER() OVER (PARTITION BY customer_id
ORDER BY ordered_on DESC, id DESC) AS rn
FROM orders o) t
WHERE rn = 1
ORDER BY customer_id;Result (PostgreSQL and SQLite, identical): 8 rows
| customer_id | order_id | ordered_on | status |
|---|---|---|---|
| 1 | 16 | 2026-03-22 | preparing |
| 2 | 13 | 2026-03-15 | delivered |
| 3 | 4 | 2026-03-04 | cancelled |
| 4 | 11 | 2026-03-12 | cancelled |
| 5 | 14 | 2026-03-18 | delivered |
| 6 | 15 | 2026-03-20 | delivered |
| 7 | 10 | 2026-03-11 | delivered |
| 8 | 17 | 2026-03-22 | preparing |
Try it: PostgreSQL playground · SQLite playground
Without window functions: join to the MAX
Find each customer's latest date, then join back. If two orders share that date, both come back.
SELECT o.customer_id, o.id AS order_id, o.ordered_on, o.status
FROM orders o
JOIN (SELECT customer_id, MAX(ordered_on) AS last_on
FROM orders
GROUP BY customer_id) m
ON m.customer_id = o.customer_id AND m.last_on = o.ordered_on
ORDER BY o.customer_id, o.id;Result (PostgreSQL and SQLite, identical): 8 rows
| customer_id | order_id | ordered_on | status |
|---|---|---|---|
| 1 | 16 | 2026-03-22 | preparing |
| 2 | 13 | 2026-03-15 | delivered |
| 3 | 4 | 2026-03-04 | cancelled |
| 4 | 11 | 2026-03-12 | cancelled |
| 5 | 14 | 2026-03-18 | delivered |
| 6 | 15 | 2026-03-20 | delivered |
| 7 | 10 | 2026-03-11 | delivered |
| 8 | 17 | 2026-03-22 | preparing |
Try it: PostgreSQL playground · SQLite playground
Variations
The first row instead
Sort ascending: ORDER BY ordered_on, id makes row 1 the earliest order.
Only the date, not the whole row
Then a plain GROUP BY is enough: SELECT customer_id, MAX(ordered_on) FROM orders GROUP BY customer_id.
Common mistakes
GROUP BY customer_idwithMAX(ordered_on)and other columns: PostgreSQL rejects the other columns, SQLite fills them from an arbitrary row.No tie-breaker: with two orders on the same day, which one is “latest” changes from run to run.
DISTINCT ONin SQLite: it's PostgreSQL-only.
PostgreSQL and SQLite
ROW_NUMBER and the MAX join run the same in both. DISTINCT ON is PostgreSQL-only, so that example shows each engine's form; their results match.
Official documentation
Go deeper
- Guide: SQL window functions
- Practice: SQL window functions practice: 8 exercises with answers
- Lesson: Ranking with OVER (Pro)
Make it stick in five minutes a day
“Ranking with OVER” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.