# Keep the rows with the largest or smallest values

Shape / rows

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

Sort by one or more columns and keep the N rows with the largest or smallest values.

Page: <https://meridian.online/operations/take-the-first-or-last-rows/keep-the-top-or-bottom-n-rows>

## Status

Runs as a SQL step.

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

## The DuckDB step

keep the N rows with the largest or smallest values, sorting on two columns

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
-- largest three amounts; the two 510.00 orders tie, so the later order date comes first
CREATE TEMP TABLE largest AS
SELECT order_id, amount, order_date FROM orders ORDER BY amount DESC NULLS LAST, order_date DESC LIMIT 3;
-- smallest two amounts, leaving out the order with no amount
CREATE TEMP TABLE smallest AS
SELECT order_id, amount FROM orders WHERE amount IS NOT NULL ORDER BY amount ASC LIMIT 2;
-- the single largest: the tie at 510.00 goes to the later order date
CREATE TEMP TABLE winner AS
SELECT order_id FROM orders ORDER BY amount DESC NULLS LAST, order_date DESC LIMIT 1;
SELECT * FROM largest;
SELECT * FROM smallest;
SELECT CASE WHEN ((SELECT list(order_id ORDER BY amount DESC, order_date DESC) FROM largest) = [8, 6, 3] AND (SELECT order_id FROM winner) = 8 AND (SELECT list(order_id ORDER BY amount) FROM smallest) = [9, 5]) THEN 'as expected' ELSE error('the largest three were not orders 8, 6 and 3 with 8 first, or the smallest two were not orders 9 and 5') 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).

- EasyMorph: [`Keep min/max`](https://help.easymorph.com/doku.php?id=transformations:filterminmax) (read 2026-09-27)
- KNIME Analytics Platform: [`Top k Row Filter`](https://api.hub.knime.com/nodes/*lKYIEI7DlbmMxvBj?details=full) (read 2026-09-27)
- Polars: [`DataFrame.bottom_k`](https://docs.pola.rs/api/python/stable/reference/dataframe/modify_select.html) (read 2026-09-27)
- Polars: [`DataFrame.top_k`](https://docs.pola.rs/api/python/stable/reference/dataframe/modify_select.html) (read 2026-09-27)
- Power Query M: [`Table.Max`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#ordering) (read 2026-09-27)
- Power Query M: [`Table.MaxN`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#ordering) (read 2026-09-27)
- Power Query M: [`Table.Min`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#ordering) (read 2026-09-27)
- Power Query M: [`Table.MinN`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#ordering) (read 2026-09-27)
- Talend: [`tTop`](https://help.qlik.com/talend/en-US/components/8.0/processing/processing-component) (read 2026-09-27)
