Shape / rows · All operations
Sample rows
Random or stratified sample, train and test split.
Status
- In arcform
Runs as a SQL step
arcform carries it as a
sql:step today.- Shape of the result
- fewer rows, same columns
- Level
- one clause of SQL
The DuckDB step
A repeatable sample of four rows
It reads the orders table the proofs share. Create it first:
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:
CREATE TEMP TABLE result AS
SELECT (SELECT list(order_id ORDER BY order_id) FROM (SELECT * FROM orders USING SAMPLE reservoir(4 ROWS) REPEATABLE (42))) AS first_draw,
(SELECT list(order_id ORDER BY order_id) FROM (SELECT * FROM orders USING SAMPLE reservoir(4 ROWS) REPEATABLE (42))) AS second_draw;
SELECT * FROM result;
SELECT CASE WHEN ((SELECT len(first_draw) = 4 AND first_draw = second_draw FROM result)) THEN 'as expected' ELSE error('the two draws with one seed differ, or a draw is not four rows') END AS verdict;- It needs nothing installed beyond DuckDB itself.
- The statement checks its own result and raises an error when it is wrong: “the two draws with one seed differ, or a draw is not four rows”.
- It passed on DuckDB
v2.0.0 (Cyanoptera) d54cbbe3df.
Variants
- Oversample or draw with replacement
Draw rows with replacement, or synthesise new minority-class rows, so rows can appear more than in the input.
- Take a stratified sample
Sample within each class of a column so the sample keeps, or evens out, the class shares.
- Split rows into training and test sets
Partition every row at random into two or more sets, such as training, validation and holdout.
Take every nth row
Keep rows at a fixed interval from an offset.
Other sampling methods
Sample by design or model, sample collections, or write samples aside for checking.
Take a random sample
Keep a random number or share of rows, repeatable with a seed, by Bernoulli, system or reservoir sampling.
Shuffle values at random
Reorder rows, or permute one column's values, at random.
What other tools call it
Meridian's classification; an independent blind rating agreed on 86.2% of 312 rows (exact class, version 2).
Alteryx Designer (3)
Create Samplesread 2026-09-27Split records at random into estimation, validation and holdout samples by percentage
Oversample Fieldread 2026-09-27Resample so a chosen value of a field reaches a target share, for imbalanced binary targets
Random % Sampleread 2026-09-27Return a random sample of N records or N percent of the stream
Apache Hop (1)
Reservoir Samplingread 2026-09-27Draw a fixed-size uniform random sample from a stream of unknown length
Apache NiFi (1)
SampleRecordread 2026-09-27Sample the records of a FlowFile by a chosen strategy, such as reservoir sampling
AWS Glue (2)
DuckDB (2)
SAMPLE (clause)read 2026-09-27Keep a random sample by rows or percent, repeatable with a seed
duckdb_table_sampleread 2026-09-27duckdb_table_sample table function
Duckle (2)
Sampleread 2026-09-25random row sample with USING SAMPLE
Sample (Reproducible %)read 2026-09-25percentage sample, reservoir or Bernoulli, with a seed
Ibis (1)
Sampleread 2026-09-27Randomly sample a fraction of rows, by row or by block, with an optional seed
KNIME Analytics Platform (8)
Bootstrap Samplingread 2026-09-27Draw rows at random with replacement
DB Row Samplerread 2026-09-27In-database: sample rows of a DB table
DB Table Partitionerread 2026-09-27In-database: split a DB table row-wise into two partitions such as train and test
Equal Size Samplingread 2026-09-27Downsample so every value of a categorical column occurs equally often
Row Samplerread 2026-09-27Extract a sample of rows of a given size
SMOTEread 2026-09-27Oversample by synthesising artificial minority-class rows (SMOTE)
Table Partitionerread 2026-09-27Split rows into two partitions, for instance train and test
Target Shufflingread 2026-09-27Randomly permute the values of one column to break its link with the other columns
mlpack (1)
Dataset splittingread 2026-09-27Split a dataset, and its labels, into a training set and a test set
Pentaho Data Integration (1)
Reservoir Samplingread 2026-09-27Draw a fixed-size random sample of rows from the stream
Polars (3)
DataFrame.gather_everyread 2026-09-27Keep every nth row, starting from an offset
DataFrame.sampleread 2026-09-27Random sample by count or fraction, with or without replacement, optionally seeded
Expr manipulation/selection: sampling (group)read 2026-09-272 expressions: sample, shuffle; draw from or reorder the values of a column at random
RapidMiner Studio (9)
SMOTE Upsamplingread 2026-09-27Oversample the minority class by synthesising rows between nearest neighbours (SMOTE)
Sampleread 2026-09-27Random sample of rows by absolute count, fraction or probability, optionally per class
Sample (Bootstrapping)read 2026-09-27Sample rows with replacement (bootstrap)
Sample (Collection)read 2026-09-27Sample a collection of objects down to a given number of items
Sample (In Database)read 2026-09-27Sample rows by absolute count or probability, executed in the database
Sample (Kennard-Stone)read 2026-09-27Pick rows by the Kennard-Stone algorithm, each time adding the row farthest from those chosen
Sample (Model-Based)read 2026-09-27Sample rows weighted towards those a given model does not yet explain
Sample (Stratified)read 2026-09-27Random sample that preserves the label's class distribution
Split Dataread 2026-09-27Partition rows into several subsets by given ratios, such as train and test
Tableau Prep (1)
Data sampleread 2026-09-27Work on a sample while authoring: Quick select, Random or Stratified rows, up to a row cap
Talend (2)
tReservoirSamplingread 2026-09-27Draw a random sample from a large data set, keeping profiling results close to the full set
tSampleread 2026-09-27Return a sample subset of the rows
VisiData (1)
random-rowsread 2026-09-23Open duplicate sheet with a random sample of N rows
Background
Sampling is the selection of a subset of individuals from within a statistical population to estimate characteristics of the whole population.[1] In DuckDB, samples randomly select a subset of a dataset, which is useful for exploring it faster when rough indications of what it holds are enough.[2] In DuckDB, a sample can be different between runs unless its seed is specified.[2]
When to use it
Sampling has lower costs and faster data collection than a census recording data from the entire population.[1] In DuckDB, samples give approximate answers to queries faster, because they reduce the amount of data that needs to pass through the query engine.[2] Reach for a sample to look at a large table or to try out a query, and go back to the full table for any figure that will be reported. (Meridian's view)
A simple random sample can be vulnerable to sampling error, because the randomness of the selection may give a sample that does not reflect the makeup of the population.[1] Systematic and stratified sampling attempt to overcome the risk of an unrepresentative sample by using information about the population to choose a more representative one.[1]
In DuckDB, fixing the seed only guarantees a reproducible sample when DuckDB runs single-threaded (SET threads = 1).[2] Record the seed and the thread setting alongside any sample a later step depends on, or store the sampled rows themselves. (Meridian's view) In DuckDB, the USING SAMPLE clause samples the result of the entire FROM clause after all joins have been resolved, while TABLESAMPLE samples only the table expression it follows.[2] Sample a table before a join when the aim is to cut the work, and sample the joined result when the sample has to describe the join. (Meridian's view)
The method
In a simple random sample (SRS) of a given size, all subsets of a sampling frame have an equal probability of being selected.[1] In small populations and often in large ones, simple random sampling is typically done without replacement, so that no member of the population is chosen more than once.[3] Systematic sampling arranges the population by some ordering scheme, makes a random start, and then selects every kth element, where k is the population size divided by the sample size.[1]
Reservoir sampling is a family of randomised online algorithms for choosing a simple random sample, without replacement, of k items from a population of unknown size in a single pass.[4] Reservoir sampling was introduced as early as 1962 by Fan, Muller and Rezucha, and a simpler algorithm, known as Algorithm R, was discovered independently by Alan G. Waterman and by McLeod and Bellhouse.[4] Algorithm R fills a reservoir with the first k items, then for each new item i draws a random number j from 1 to i and puts the item in slot j of the reservoir if j is at most k, otherwise discarding it.[4] Algorithm R can also be viewed as a variant of the Fisher–Yates shuffle.[4]
DuckDB supports three sampling methods, reservoir, bernoulli and system, and by default uses reservoir sampling when an exact number of rows is sampled and system sampling when a percentage is given.[2] In DuckDB, bernoulli sampling includes every row with a chance equal to the given percentage, so it can return a different number of rows even for the same percentage.[2] In DuckDB, reservoir sampling always outputs the same number of rows, unlike system and bernoulli sampling.[2] In DuckDB, reservoir sampling requires the entire sample to be materialised in memory, and is recommended only for small sample sizes.[2]
Compared with the alternatives
Systematic sampling gives every element the same probability of selection, but it is not simple random sampling, because different subsets of the same size have different selection probabilities.[1] Cluster sampling generally increases the variability of sample estimates above that of simple random sampling, so it needs a larger sample for the same accuracy.[1]
Sources
- [1]Sampling (statistics) - Wikipediasecondary source
- [2]Samples – DuckDBdocumentation
- [3]Simple random sample - Wikipediasecondary source
- [4]Reservoir sampling - Wikipediasecondary source