USING SAMPLE clause
Use USING SAMPLE to run a query on a random part of its rows instead of all of them. Write USING SAMPLE after the WHERE, GROUP BY, HAVING, WINDOW, and QUALIFY clauses, and before ORDER BY and LIMIT. Even though it comes late in the query text, DuckDB takes the sample from the joined rows of the FROM clause. As a result, the WHERE clause, grouping, and aggregates see only the sampled rows.
Give the size as a number of rows, with or without ROWS, or as a percentage with PERCENT or %. A number without PERCENT or % is a row count.
Choose a sampling method by name. The reservoir method returns exactly the requested number of rows, or every row if there are fewer. The bernoulli method keeps each row with the given probability, so the number of rows varies from run to run. The system method keeps or leaves out whole blocks of rows, so on a small table it returns either every row or none. Without a method, a row count uses reservoir and a percentage uses system. To set the random seed, add REPEATABLE (seed) or pass the seed after the method.
Because the rows in a sample vary from run to run, the examples show the number of rows or a property of the result instead of the rows.
Syntax
USING SAMPLE size [ ROWS | PERCENT | % ] [ ( method
[, seed ] ) ]
USING SAMPLE method ( size [ ROWS | PERCENT ] )
[ REPEATABLE ( seed ) ]
method: { reservoir | bernoulli | system }
Examples
For a description of the sample tables that the examples on this page use, see SELECT statement in DuckDB.
The following SQL query takes a sample of three rows. A row count uses the reservoir method, so the sample always has three rows.
SELECT count(*) AS sampled_rows FROM sales_db.sales_parquet USING SAMPLE 3 ROWS
The query returns the following result.
| sampled_rows |
|---|
|
The following SQL query keeps each row with a probability of 50 percent. Because the number of rows varies, the query checks only that it is between 0 and 6.
SELECT count(*) BETWEEN 0 AND 6 AS within_table_size FROM sales_db.sales_parquet USING SAMPLE 50 PERCENT (bernoulli)
The query returns the following result.
| within_table_size |
|---|
|
The following SQL query samples 50 percent of a six-row table with the default method for percentages, system. On a table this small, the sample has either every row or none.
SELECT count(*) IN (0, 6) AS all_or_none FROM sales_db.sales_parquet USING SAMPLE 50 PERCENT
The query returns the following result.
| all_or_none |
|---|
|
Considerations
bernoulli takes only a percentage. A bernoulli sample with a row count makes the query fail.
Differences from Athena engine version 3
In Athena engine version 3, TABLESAMPLE SYSTEM (50) returns about 50 percent of the rows. In DuckDB, a sample size without PERCENT or % is a row count. To sample about 50 percent of the rows, write USING SAMPLE 50 PERCENT.
In Athena engine version 3, TABLESAMPLE BERNOULLI (50) keeps each row with a probability of 50 percent. In DuckDB, a bernoulli sample needs a percentage. Write USING SAMPLE 50 PERCENT (bernoulli) instead.