sqlstreak

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_idorder_idordered_onstatus
1162026-03-22preparing
2132026-03-15delivered
342026-03-04cancelled
4112026-03-12cancelled
5142026-03-18delivered
6152026-03-20delivered
7102026-03-11delivered
8172026-03-22preparing

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 1
    PARTITION BY customer_id

    One numbering per customer.

  2. 2
    ORDER BY ordered_on DESC, id DESC

    Newest first; the id decides between two orders on the same day.

  3. 3
    WHERE rn = 1

    Keep 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_idorder_idordered_onstatus
1162026-03-22preparing
2132026-03-15delivered
342026-03-04cancelled
4112026-03-12cancelled
5142026-03-18delivered
6152026-03-20delivered
7102026-03-11delivered
8172026-03-22preparing

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_idorder_idordered_onstatus
1162026-03-22preparing
2132026-03-15delivered
342026-03-04cancelled
4112026-03-12cancelled
5142026-03-18delivered
6152026-03-20delivered
7102026-03-11delivered
8172026-03-22preparing

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_id with MAX(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 ON in 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

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.

Start free