SAMPLE / TABLESAMPLE

constructsupportedQueries & DML

docs.snowflake.com ↗

Returns a subset of rows sampled randomly from the specified table. You can specify different types of sampling methods, and you can sample a fraction of a table or a fixed number of rows: - When you sample a fraction of a table, with a specified probability for including a given row, the number of rows returned depends on the size of the table and the requested probability. You can specify a seed to make the sampling deterministic. - When you sample a fixed, specified number of rows, the query returns the exact number of specified rows unless the table contains fewer rows. SAMPLE and TABLESAMPLE are synonymous and can be used interchangeably.

Syntax

supportedpartialnot supportednot scored

SELECT ...
FROM ...
  { SAMPLE | TABLESAMPLE } [ samplingMethod ]
[ ... ]

Where

samplingMethod ::= { { BERNOULLI | ROW } ( { <probability> | <num> ROWS } ) |
                     { SYSTEM | BLOCK } ( <probability> ) [ { REPEATABLE | SEED } ( <seed> ) ] }

Parameters

NameRequiredDescription
{ BERNOULLI | ROW } or { SYSTEM | BLOCK }Specifies the sampling method to use: BERNOULLI (or ROW): Includes each row with a probability of p/100. This method is similar to flipping a weighted coin for each row. SYSTEM (or BLOCK): Includes each block of rows with a probability of p/100. This method is similar to flipping a weighted coin for each block of rows. This method doesn’t support fixed-size sampling. The sampling method is optional. If no method is specified, the default is BERNOULLI.
probability or num ROWSSpecifies whether to sample based on a fraction of the table or a fixed number of rows in the table, where: probability specifies the percentage probability to use for selecting the sample. Can be any decimal number between 0 (no rows selected) and 100 (all rows selected) inclusive. num specifies the number of rows (up to 1,000,000) to sample from the table. Can be any integer between 0 (no rows selected) and 1000000 inclusive. In addition to using literals to specify probability or num ROWS, you can also use session or bind variables.
{ REPEATABLE | SEED ( seed ) }Specifies a seed value to make the sampling deterministic. Can be any integer between 0 and 2147483647 inclusive. This parameter only applies to SYSTEM and BLOCK sampling. In addition to using literals to specify seed, you can also use session or bind variables.

Usage notes

Parameters

{ BERNOULLI | ROW } or { SYSTEM | BLOCK }: Specifies the sampling method to use: BERNOULLI (or ROW): Includes each row with a probability of p/100. This method is similar to flipping a weighted coin for each row. SYSTEM (or BLOCK): Includes each block of rows with a probability of p/100. This method is similar to flipping a weighted coin for each block of rows. This method doesn’t support fixed-size sampling. The sampling method is optional. If no method is specified, the default is BERNOULLI. probability or num ROWS: Specifies whether to sample based on a fraction of the table or a fixed number of rows in the table, where: probability specifies the percentage probability to use for selecting the sample. Can be any decimal number between 0 (no rows selected) and 100 (all rows selected) inclusive. num specifies the number of rows (up to 1,000,000) to sample from the table. Can be any integer between 0 (no rows selected) and 1000000 inclusive. In addition to using literals to specify probability or num ROWS, you can also use session or bind variables. { REPEATABLE | SEED ( seed ) }: Specifies a seed value to make the sampling deterministic. Can be any integer between 0 and 2147483647 inclusive. This parameter only applies to SYSTEM and BLOCK sampling. In addition to using literals to specify seed, you can also use session or bind variables.

Performance considerations

- SYSTEM or BLOCK sampling is often faster than BERNOULLI or ROW sampling. - Sampling without a seed is often faster than sampling with a seed. - Fixed-size sampling might be slower than equivalent fraction-based sampling because fixed-size sampling prevents some query optimization.

Referenced by