BigQuery · Query syntax
BigQuery TABLESAMPLE SYSTEM
TABLESAMPLE SYSTEM (n PERCENT) reads a random subset of a BigQuery table's storage blocks instead of the whole table, so exploratory queries on large tables scan and bill only about n percent of the bytes. Results are approximate and change on every run.
TABLESAMPLE SYSTEM (10 PERCENT) goes right after a table name in the FROM clause. BigQuery picks roughly that share of the table's data blocks at random and reads every row in them. Because whole blocks are skipped, the bytes scanned, and the on-demand bill, shrink with the sample. That is the difference from LIMIT, which on an unclustered table still reads every referenced column in full, and from WHERE RAND() < 0.1, which samples rows but has to scan the whole table to do it.
Sampling is by block, not by row. BigQuery typically splits a table or partition into blocks once it is larger than about 1 GB, so a small table may be a single block and is then read completely. Block sizes vary, so the fraction of rows returned is only approximately the percentage you asked for. In partitioned or clustered tables, a block holds rows with the same partition or similar cluster values, which makes the sample more biased than on an unsorted table. For a row-level sample at lower cost, combine both: TABLESAMPLE SYSTEM (20 PERCENT) plus WHERE RAND() < 0.1 reads about 20% of the blocks and keeps about 10% of their rows.
The percentage must be a literal or a query parameter between 0 and 100, not a script variable. Results are never cached, so every run is billed again. A sampled table can appear only once in a statement, and you cannot sample views, subqueries, table-valued functions, UNNEST results, tables with row-level security, or a table inside an IN subquery. The Table sampling guide still marks the feature as Preview.
Syntax
SELECT ...
FROM table_name [AS alias] TABLESAMPLE SYSTEM (percent PERCENT)
[WHERE ...]Arguments and return value
| Argument | Description |
|---|---|
table_name | A table, including an external table backed by files. Not a view, subquery, table function or UNNEST. |
percent | Share of the table's data blocks to read, from 0 to 100. A literal or a query parameter such as @pct; script variables are not allowed. |
Returns: The rows of the randomly selected blocks, filtered by the rest of the query. Each run can return a different sample.
Examples
Profile a large table cheaply
Reads about 1% of the blocks of a large events table, enough to see value distributions while billing a fraction of a full scan.
SELECT
event_name,
COUNT(*) AS sampled_events,
COUNT(*) * 100 AS estimated_events
FROM `project.dataset.events` TABLESAMPLE SYSTEM (1 PERCENT)
GROUP BY event_name
ORDER BY sampled_events DESC;Row-level sample without a full scan
RAND() alone would read the whole table. Sampling 20% of the blocks first and then 10% of their rows gives about 2% of the rows at roughly a fifth of the full-scan cost.
SELECT user_id, event_name, event_ts
FROM `project.dataset.events` TABLESAMPLE SYSTEM (20 PERCENT)
WHERE RAND() < 0.1;Sample both sides of a join
Joining two samples on a key keeps only pairs whose blocks were both picked, so the result is much smaller than either sample, and on small tables can even be empty.
SELECT o.order_id, o.amount, c.country
FROM `project.dataset.orders` AS o TABLESAMPLE SYSTEM (10 PERCENT)
JOIN `project.dataset.customers` AS c TABLESAMPLE SYSTEM (50 PERCENT)
USING (customer_id);Common mistakes
- Using
LIMITto look at a big table cheaply. On an unclustered table,SELECT * ... LIMIT 10bills every byte of every column. UseTABLESAMPLE, the table preview in the console, orbq head, which read table data without running a billed query. - Expecting exactly n percent of the rows. Sampling is by storage block, so a table under about 1 GB may come back whole and a large one returns only approximately n percent.
- Passing the percentage in a script variable.
TABLESAMPLE SYSTEM (pct PERCENT)with a DECLAREd variable fails; use a literal or a query parameter. - Treating a sample of a partitioned or clustered table as uniform. Blocks group similar rows, so a 1% sample can over-represent a few days or customers. Sample within a partition filter, or add
WHERE RAND() < xfor row-level randomness. - Sampling a view. Views (and subqueries) cannot be sampled; sample the underlying table in the view's query instead.
Cost and performance
- On-demand, the bytes billed scale with the sample: a 1% sample of a 5 TiB table bills about 51 GiB instead of 5 TiB, roughly $0.31 instead of $31.25 at $6.25 per TiB, minus whatever the column selection already saves.
- Results of a query with
TABLESAMPLEare never cached, so a dashboard built on a sample pays for every refresh. Materialize the sample into a table once if many queries need it. - Partition filters still apply: sampling within
WHERE DATE(event_ts) = '2026-11-22'reads a share of one partition, not of the whole table.
FAQ
How do I select a random sample of rows in BigQuery?
For speed and low cost use FROM t TABLESAMPLE SYSTEM (10 PERCENT), which samples storage blocks. For a true row-level sample use WHERE RAND() < 0.1, which scans the whole table, or combine both.
Does TABLESAMPLE reduce query cost in BigQuery?
Yes. Only the sampled blocks are read, so on-demand bytes billed drop roughly in proportion to the percentage, unlike LIMIT. Small single-block tables are the exception: they are read in full.
Why does TABLESAMPLE return the whole table?
The table is stored as a single block, which is typical for tables smaller than about 1 GB. Sampling cannot go below one block.
Related
More query syntax
All BigQuery query syntax · All statements and clauses · BigQuery error messages
