# Take the first or last rows

Shape / rows

first N, last N, top N, row range, skip rows

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

## Status

Runs as a SQL step.

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

## The DuckDB step

top two orders per region

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
CREATE TEMP TABLE result AS
SELECT * FROM orders QUALIFY row_number() OVER (PARTITION BY region ORDER BY amount DESC NULLS LAST) <= 2;
SELECT * FROM result;
SELECT CASE WHEN ((SELECT count(*) = 9 AND count(*) FILTER (WHERE order_id = 2) = 0 FROM result) AND (SELECT max(n) <= 2 FROM (SELECT count(*) AS n FROM result GROUP BY region))) THEN 'as expected' ELSE error('a region kept more than two orders, or order 2 survived') END AS verdict;
```

It needs nothing installed beyond DuckDB itself.

## Variants

- [Keep rows by their positions](https://meridian.online/operations/take-the-first-or-last-rows/keep-rows-by-their-positions): Keep rows at listed line numbers or in several separate ranges of positions.
- Keep or skip the first or last rows: Keep the first or last N rows, skip some, or keep one contiguous run by offset and count.
- Keep every nth row: Keep or drop rows at a regular interval, such as every third row.
- Trim binary content: Discard a byte range at the start or end of binary content.
- [Keep the first rows of each group](https://meridian.online/operations/take-the-first-or-last-rows/keep-the-first-rows-of-each-group): Keep the first, last or top N rows within every group rather than across the table.
- Take the first row as a value: Return one row as a record, or one cell as a value, instead of a table.
- [Keep the rows with the largest or smallest values](https://meridian.online/operations/take-the-first-or-last-rows/keep-the-top-or-bottom-n-rows): Sort by one or more columns and keep the N rows with the largest or smallest values.
- Keep rows up to a condition: Keep or drop the rows before or after the first row that meets a condition.

## What other tools call it

Meridian's classification; an independent blind rating agreed on 86.2% of 312 rows (exact class, version 2).

- Azure Data Factory: [`Reducing Rows`](https://learn.microsoft.com/en-us/azure/data-factory/wrangling-functions) (read 2026-09-27)
- Alteryx Designer: [`Sample`](https://help.alteryx.com/current/en/designer/tools/preparation/sample-tool.html) (read 2026-09-27)
- Alteryx Designer: [`Sample In-DB`](https://help.alteryx.com/current/en/designer/tools/in-database/sample-in-db-tool.html) (read 2026-09-27)
- Alteryx Designer: [`Select Records`](https://help.alteryx.com/current/en/designer/tools/preparation/select-records-tool.html) (read 2026-09-27)
- Apache DataFusion: [`LIMIT`](https://datafusion.apache.org/user-guide/sql/select.html) (read 2026-09-27)
- Apache DataFusion: [`Limit (LIMIT)`](https://datafusion.apache.org/user-guide/features.html) (read 2026-09-27)
- Apache DataFusion: [`OFFSET`](https://datafusion.apache.org/user-guide/sql/select.html) (read 2026-09-27)
- Apache DataFusion: [`limit`](https://docs.rs/datafusion/latest/datafusion/dataframe/struct.DataFrame.html) (read 2026-09-27)
- DuckDB: [`LIMIT and OFFSET (clause)`](https://duckdb.org/docs/current/sql/query_syntax/limit) (read 2026-09-27)
- Duckle: [`Skip / Offset`](https://github.com/slothflowlabs/duckle/blob/f85f8960edfb6efbf4cb89d58a7b32e16df0e867/website/docs/components.html) (read 2026-09-25)
- Duckle: [`Top N / Limit`](https://github.com/slothflowlabs/duckle/blob/f85f8960edfb6efbf4cb89d58a7b32e16df0e867/website/docs/components.html) (read 2026-09-25)
- Duckle: [`Top N per Group`](https://github.com/slothflowlabs/duckle/blob/f85f8960edfb6efbf4cb89d58a7b32e16df0e867/website/docs/components.html) (read 2026-09-25)
- EasyMorph: [`Keep min/max`](https://help.easymorph.com/doku.php?id=transformations:filterminmax) (read 2026-09-27)
- EasyMorph: [`Trim table`](https://help.easymorph.com/doku.php?id=transformations:trimtable) (read 2026-09-27)
- EasyMorph: [`Trim table by condition`](https://help.easymorph.com/doku.php?id=transformations:trimbycondition) (read 2026-09-27)
- Apache Hop: [`Sample Rows`](https://hop.apache.org/manual/latest/pipeline/transforms/samplerows.html) (read 2026-09-27)
- Ibis: [`Limit`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/relations.py) (read 2026-09-27)
- KNIME Analytics Platform: [`Cell Extractor`](https://api.hub.knime.com/nodes/*VhUck8shpAE4-LvI?details=full) (read 2026-09-27)
- KNIME Analytics Platform: [`Table Cropper`](https://api.hub.knime.com/nodes/*bOdbxQQ7JyiSlkjQ?details=full) (read 2026-09-27)
- KNIME Analytics Platform: [`Table Splitter`](https://api.hub.knime.com/nodes/*uqoOrCkNcxp1Qc6t?details=full) (read 2026-09-27)
- KNIME Analytics Platform: [`Top k Row Filter`](https://api.hub.knime.com/nodes/*lKYIEI7DlbmMxvBj?details=full) (read 2026-09-27)
- Apache NiFi: [`ModifyBytes`](https://nifi.apache.org/components/org.apache.nifi.processors.standard.ModifyBytes/) (read 2026-09-27)
- Pentaho Data Integration: [`Sample rows`](https://docs.pentaho.com/pdia-data-integration/9.3-data-integration/pdi-transformation-steps-reference-overview) (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.gather`](https://docs.pola.rs/api/python/stable/reference/dataframe/modify_select.html) (read 2026-09-27)
- Polars: [`DataFrame.head`](https://docs.pola.rs/api/python/stable/reference/dataframe/modify_select.html) (read 2026-09-27)
- Polars: [`DataFrame.limit`](https://docs.pola.rs/api/python/stable/reference/dataframe/modify_select.html) (read 2026-09-27)
- Polars: [`DataFrame.slice`](https://docs.pola.rs/api/python/stable/reference/dataframe/modify_select.html) (read 2026-09-27)
- Polars: [`DataFrame.tail`](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)
- Polars: [`Expr manipulation/selection: positional selection (group)`](https://docs.pola.rs/api/python/stable/reference/expressions/modify_select.html) (read 2026-09-27)
- 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)
- Polars: [`LazyFrame.first`](https://docs.pola.rs/api/python/stable/reference/lazyframe/modify_select.html) (read 2026-09-27)
- Polars: [`LazyFrame.last`](https://docs.pola.rs/api/python/stable/reference/lazyframe/modify_select.html) (read 2026-09-27)
- Power Query M: [`Table.AlternateRows`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#row-operations) (read 2026-09-27)
- Power Query M: [`Table.First`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#row-operations) (read 2026-09-27)
- Power Query M: [`Table.FirstN`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#row-operations) (read 2026-09-27)
- Power Query M: [`Table.FirstValue`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#row-operations) (read 2026-09-27)
- Power Query M: [`Table.Last`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#row-operations) (read 2026-09-27)
- Power Query M: [`Table.LastN`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#row-operations) (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)
- Power Query M: [`Table.Range`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#row-operations) (read 2026-09-27)
- Power Query M: [`Table.RemoveFirstN`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#row-operations) (read 2026-09-27)
- Power Query M: [`Table.RemoveLastN`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#row-operations) (read 2026-09-27)
- Power Query M: [`Table.RemoveRows`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#row-operations) (read 2026-09-27)
- Power Query M: [`Table.SingleRow`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#row-operations) (read 2026-09-27)
- Power Query M: [`Table.Skip`](https://learn.microsoft.com/en-us/powerquery-m/table-functions#row-operations) (read 2026-09-27)
- RapidMiner Studio: [`Filter Example Range`](https://docs.rapidminer.com/latest/studio/operators/blending/examples/filter/filter_example_range.html) (read 2026-09-27)
- RapidMiner Studio: [`Filter Example Range (In Database)`](https://docs.rapidminer.com/latest/studio/operators/extensions/In-Database%20Processing/indb_blending/indb_sample.html) (read 2026-09-27)
- SQLMesh: [`@LIMIT`](https://github.com/SQLMesh/sqlmesh/blob/23b237cea7218cc8f338b89c14f0957394a0003b/docs/concepts/macros/sqlmesh_macros.md) (read 2026-09-27)
- Tableau Prep: [`Set header and data start row`](https://help.tableau.com/current/prep/en-us/prep_configure_dastaset.htm) (read 2026-09-27)
- Talend: [`tSampleRow`](https://help.qlik.com/talend/en-US/components/8.0/processing/processing-component) (read 2026-09-27)
- Talend: [`tTop`](https://help.qlik.com/talend/en-US/components/8.0/processing/processing-component) (read 2026-09-27)
- Talend: [`tTopBy`](https://help.qlik.com/talend/en-US/components/8.0/processing/processing-component) (read 2026-09-27)
- Trifacta: [`Filter at interval`](https://help.alteryx.com/aac/en/trifacta-classic/reference/transformation-reference.html) (read 2026-09-27)
- Trifacta: [`Filter from top`](https://help.alteryx.com/aac/en/trifacta-classic/reference/transformation-reference.html) (read 2026-09-27)
- Trifacta: [`Filter range`](https://help.alteryx.com/aac/en/trifacta-classic/reference/transformation-reference.html) (read 2026-09-27)
- VisiData: [`dup-limit`](https://github.com/saulpw/visidata/blob/7609212ec2febafa9caba7183cbebfaf49627909/visidata/apps/vdsql/_ibis.py#L763) (read 2026-09-23)
- VisiData: [`dup-nolimit`](https://github.com/saulpw/visidata/blob/7609212ec2febafa9caba7183cbebfaf49627909/visidata/apps/vdsql/_ibis.py#L764) (read 2026-09-23)
