# How to get the first or latest row per group in SQL

Source: https://sqlstreak.com/sql/how-to/first-row-per-group · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


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

```sql
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 (both engines):

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

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

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

```sql
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 (both engines):

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

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

```sql
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 (both engines):

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

## How it works

- `PARTITION BY customer_id`: One numbering per customer.
- `ORDER BY ordered_on DESC, id DESC`: Newest first; the id decides between two orders on the same day.
- `WHERE rn = 1`: Keep each customer's newest order and drop the rest.

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