Sample Rows

Pull a small random slice out of a big table, in the order the grid is showing.

What it does

Sample Rows picks rows at random and drops the rest. Nothing is sorted, and no column is involved. Use it on a large file to test an idea fast. You can then remove the step and run the same work on every row.

Take a sample of rows

  1. Type sample in the Search transforms box, in the Pipeline panel.
  2. Select Sample Rows in the results list.
  3. Look at Sample by. It is already set to rows.
  4. Type a row count in Sample size. For example, type 500.
  5. Click Apply.

Example: a web server log has two million rows. A sample of 500 rows loads at once. You can then try a date split or a filter on it.

Sample a percentage instead

Open Sample by and select percent. Now Sample size means a percentage, not a row count. Type 5 to keep 5 percent of the rows, rounded to a whole row count. The value must stay at 100 or below.

Percent mode grows with your table. Rows mode gives you the same count every time.

Get the same sample twice

A sample is different on each run by default. To lock it, use the third field:

  1. Type any whole number in Random seed (optional, for reproducibility).
  2. Click Apply.

The same seed and the same size always return the same rows. Share the seed with a colleague to compare notes on the same slice. Leave the field empty for a fresh sample on each run.

The rows keep their order

The rows that survive come back in the order the grid was showing them. The step decides which rows to keep; it never decides where they sit.

That matters most at the edge case: ask for 1000 rows from a table of 300 and you get all 300, unchanged. It used to hand them back shuffled, because the sampler returns rows in the order it drew them, so a step you expected to do nothing quietly reordered the table, and every later "first N rows" moved with it.

For SQL users

The step always names reservoir sampling in DuckDB, seed or no seed. A sample of 500 rows runs as:

SELECT * EXCLUDE ("__row_order__") FROM (
  SELECT * FROM (SELECT *, ROW_NUMBER() OVER () AS "__row_order__" FROM data)
  USING SAMPLE 500 ROWS (RESERVOIR)
) ORDER BY "__row_order__"

Add a seed and only the second argument of the sample clause changes:

USING SAMPLE 500 ROWS (RESERVOIR, 42)

The method matters. DuckDB's other sampler works a block at a time, so a percentage of a small table can come back as every row or as none.

The numbering is what keeps the order. It is added before the sample, used to sort afterwards, and dropped again, so nothing downstream ever sees it.

Try Sample Rows with sample data →

Related Operations