# Keep the first rows of each group

Shape / rows

A variant of [Take the first or last rows](https://meridian.online/operations/take-the-first-or-last-rows).

Keep the first, last or top N rows within every group rather than across the table.

Page: <https://meridian.online/operations/take-the-first-or-last-rows/keep-the-first-rows-of-each-group>

## Status

Runs as a SQL step.

arcform carries it as a `sql:` step today.

## The DuckDB step

keep the first, last or top N rows of each group

It reads the `orders` table the proofs share. Create it first:

```sql
CREATE TABLE orders AS SELECT * FROM (VALUES
  (1,  'Alice Ng',      'north', DATE '2026-01-03', 120.00, 'widget', 'a|b',   '{"channel":"web","items":[1,2]}'),
  (2,  'alice ng',      'north', DATE '2026-01-05',  80.50, 'gadget', 'b',     '{"channel":"shop","items":[3]}'),
  (3,  'Bob  Okafor',   'south', DATE '2026-01-05', 300.00, 'widget', 'a|c',   '{"channel":"web","items":[4,5,6]}'),
  (4,  'Carla Díaz',    'east',  DATE '2026-01-09',  NULL,  'gizmo',  NULL,    '{"channel":"web","items":[]}'),
  (5,  'Bob Okafor',    'south', DATE '2026-01-12',  45.25, 'gadget', 'c',     '{"channel":"phone","items":[7]}'),
  (6,  'Dev Patel',     'west',  DATE '2026-01-12', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (7,  'Carla Diaz',    'east',  DATE '2026-01-20',  99.99, 'gizmo',  'b|c',   '{"channel":"shop","items":[10]}'),
  (8,  'Dev Patel',     'west',  DATE '2026-02-02', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (9,  'Eve Larsen',    NULL,    DATE '2026-02-03',  15.00, 'gadget', NULL,    '{"channel":"web","items":[11]}'),
  (10, 'Alice Ng',      'north', DATE '2026-02-10', 220.00, 'gizmo',  'a|b|c', '{"channel":"phone","items":[12,13]}')
) t(order_id, customer, region, order_date, amount, product, tags, meta);
```

Then run the statement:

```sql
-- the first and the last order of each product, by date
CREATE TEMP TABLE first_order AS
SELECT product, order_id FROM orders
QUALIFY row_number() OVER (PARTITION BY product ORDER BY order_date, order_id) = 1;
CREATE TEMP TABLE last_order AS
SELECT product, order_id FROM orders
QUALIFY row_number() OVER (PARTITION BY product ORDER BY order_date DESC, order_id DESC) = 1;
-- the two largest orders of each product
CREATE TEMP TABLE top_two AS
SELECT product, order_id, amount FROM orders
QUALIFY row_number() OVER (PARTITION BY product ORDER BY amount DESC NULLS LAST, order_id) <= 2;
SELECT * FROM top_two ORDER BY product, amount DESC;
SELECT CASE WHEN ((SELECT map_from_entries(list((product, order_id) ORDER BY product)) FROM first_order) = MAP {'gadget': 2, 'gizmo': 4, 'widget': 1} AND (SELECT map_from_entries(list((product, order_id) ORDER BY product)) FROM last_order) = MAP {'gadget': 9, 'gizmo': 10, 'widget': 8} AND (SELECT list(order_id ORDER BY order_id) FROM top_two) = [2, 5, 6, 7, 8, 10] AND (SELECT max(n) FROM (SELECT count(*) AS n FROM top_two GROUP BY product)) = 2) THEN 'as expected' ELSE error('the first, last and two largest orders of each product were not the expected orders') END AS verdict;
```

It needs nothing installed beyond DuckDB itself.

## What other tools call it

Meridian's classification; an independent blind rating agreed on 95.7% of 324 rows (variant, version 3).

- Duckle: [`Top N per Group`](https://github.com/slothflowlabs/duckle/blob/f85f8960edfb6efbf4cb89d58a7b32e16df0e867/website/docs/components.html) (read 2026-09-25)
- Polars: [`GroupBy.head`](https://docs.pola.rs/api/python/stable/reference/dataframe/group_by.html) (read 2026-09-27)
- Polars: [`GroupBy.tail`](https://docs.pola.rs/api/python/stable/reference/dataframe/group_by.html) (read 2026-09-27)
- Talend: [`tTopBy`](https://help.qlik.com/talend/en-US/components/8.0/processing/processing-component) (read 2026-09-27)
