ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.2 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://clickhouse.com/docs/sql-reference/statements/select/sample |
| Last Crawled | 2026-04-07 23:03:27 (6 days ago) |
| First Indexed | 2025-02-21 00:12:15 (1 year ago) |
| HTTP Status Code | 200 |
| Meta Title | SAMPLE Clause | ClickHouse Docs |
| Meta Description | Documentation for SAMPLE Clause |
| Meta Canonical | null |
| Boilerpipe Text | The
SAMPLE
clause allows for approximated
SELECT
query processing.
When data sampling is enabled, the query is not performed on all the data, but only on a certain fraction of data (sample). For example, if you need to calculate statistics for all the visits, it is enough to execute the query on the 1/10 fraction of all the visits and then multiply the result by 10.
Approximated query processing can be useful in the following cases:
When you have strict latency requirements (like below 100ms) but you can't justify the cost of additional hardware resources to meet them.
When your raw data is not accurate, so approximation does not noticeably degrade the quality.
Business requirements target approximate results (for cost-effectiveness, or to market exact results to premium users).
Note
You can only use sampling with the tables in the
MergeTree
family, and only if the sampling expression was specified during table creation (see
MergeTree engine
).
The features of data sampling are listed below:
Data sampling is a deterministic mechanism. The result of the same
SELECT .. SAMPLE
query is always the same.
Sampling works consistently for different tables. For tables with a single sampling key, a sample with the same coefficient always selects the same subset of possible data. For example, a sample of user IDs takes rows with the same subset of all the possible user IDs from different tables. This means that you can use the sample in subqueries in the
IN
clause. Also, you can join samples using the
JOIN
clause.
Sampling allows reading less data from a disk. Note that you must specify the sampling key correctly. For more information, see
Creating a MergeTree Table
.
For the
SAMPLE
clause the following syntax is supported:
SAMPLE Clause Syntax
Description
SAMPLE k
Here
k
is the number from 0 to 1. The query is executed on
k
fraction of data. For example,
SAMPLE 0.1
runs the query on 10% of data.
Read more
SAMPLE n
Here
n
is a sufficiently large integer. The query is executed on a sample of at least
n
rows (but not significantly more than this). For example,
SAMPLE 10000000
runs the query on a minimum of 10,000,000 rows.
Read more
SAMPLE k OFFSET m
Here
k
and
m
are the numbers from 0 to 1. The query is executed on a sample of
k
fraction of the data. The data used for the sample is offset by
m
fraction.
Read more
SAMPLE K
Here
k
is the number from 0 to 1 (both fractional and decimal notations are supported). For example,
SAMPLE 1/2
or
SAMPLE 0.5
.
In a
SAMPLE k
clause, the sample is taken from the
k
fraction of data. The example is shown below:
SELECT
Title
,
count
(
)
*
10
AS
PageViews
FROM
hits_distributed
SAMPLE
0.1
WHERE
CounterID
=
34
GROUP
BY
Title
ORDER
BY
PageViews
DESC
LIMIT
1000
In this example, the query is executed on a sample from 0.1 (10%) of data. Values of aggregate functions are not corrected automatically, so to get an approximate result, the value
count()
is manually multiplied by 10.
SAMPLE N
Here
n
is a sufficiently large integer. For example,
SAMPLE 10000000
.
In this case, the query is executed on a sample of at least
n
rows (but not significantly more than this). For example,
SAMPLE 10000000
runs the query on a minimum of 10,000,000 rows.
Since the minimum unit for data reading is one granule (its size is set by the
index_granularity
setting), it makes sense to set a sample that is much larger than the size of the granule.
When using the
SAMPLE n
clause, you do not know which relative percent of data was processed. So you do not know the coefficient the aggregate functions should be multiplied by. Use the
_sample_factor
virtual column to get the approximate result.
The
_sample_factor
column contains relative coefficients that are calculated dynamically. This column is created automatically when you
create
a table with the specified sampling key. The usage examples of the
_sample_factor
column are shown below.
Let's consider the table
visits
, which contains the statistics about site visits. The first example shows how to calculate the number of page views:
SELECT
sum
(
PageViews
*
_sample_factor
)
FROM
visits
SAMPLE
10000000
The next example shows how to calculate the total number of visits:
SELECT
sum
(
_sample_factor
)
FROM
visits
SAMPLE
10000000
The example below shows how to calculate the average session duration. Note that you do not need to use the relative coefficient to calculate the average values.
SELECT
avg
(
Duration
)
FROM
visits
SAMPLE
10000000
SAMPLE K OFFSET M
Here
k
and
m
are numbers from 0 to 1. Examples are shown below.
Example 1
SAMPLE
1
/
10
In this example, the sample is 1/10th of all data:
[++------------]
Example 2
SAMPLE
1
/
10
OFFSET
1
/
2
Here, a sample of 10% is taken from the second half of the data.
[------++------] |
| Markdown | [Skip to main content](https://clickhouse.com/docs/sql-reference/statements/select/sample#__docusaurus_skipToContent_fallback)
[](https://clickhouse.com/)
- [Products](https://clickhouse.com/docs/sql-reference/statements/select/sample)
- [ClickHouse Cloud Best way to use ClickHouse. Available on AWS, GCP, and Azure.](https://clickhouse.com/cloud)
- [BYOC (Bring Your Own Cloud) The fully managed ClickHouse Cloud service, Can be deployed in your AWS account.](https://clickhouse.com/cloud/bring-your-own-cloud)
- [ClickHouse Set up a database with open-source ClickHouse. ClickHouse](https://clickhouse.com/clickhouse)
- [Discover more than 100 integrations.](https://clickhouse.com/integrations)
[Discover more than 100 integrations.](https://clickhouse.com/integrations)
- [Use cases](https://clickhouse.com/docs/sql-reference/statements/select/sample)
- [Real-time analytics](https://clickhouse.com/use-cases/real-time-analytics)
- [Machine Learning & Generative AI](https://clickhouse.com/use-cases/machine-learning-and-data-science)
- [Business Intelligence](https://clickhouse.com/use-cases/data-warehousing)
- [Logs, Events, Traces](https://clickhouse.com/use-cases/observability)
- [All use cases](https://clickhouse.com/use-cases)
[All use cases](https://clickhouse.com/use-cases)
- [Documentation](https://clickhouse.com/docs)
- [Resources](https://clickhouse.com/docs/sql-reference/statements/select/sample)
- [User stories](https://clickhouse.com/user-stories)
- [Blog](https://clickhouse.com/blog)
- [Events](https://clickhouse.com/company/events)
- [Learning and certification](https://clickhouse.com/learn)
- [Comparison](https://clickhouse.com/docs/sql-reference/statements/select/sample)
- [BigQuery](https://clickhouse.com/comparison/bigquery)
- [PostgreSQL](https://clickhouse.com/comparison/postgresql)
- [Redshift](https://clickhouse.com/comparison/redshift)
- [Rockset](https://clickhouse.com/comparison/rockset)
- [Snowflake](https://clickhouse.com/comparison/snowflake)
- [Video](https://clickhouse.com/videos)
- [Demo](https://clickhouse.com/demos)
- [Pricing](https://clickhouse.com/pricing)
- [Contact](https://clickhouse.com/company/contact?loc=nav)
[46\.7k](https://github.com/ClickHouse/ClickHouse?utm_source=clickhouse&utm_medium=website&utm_campaign=website-nav)
[Search`Ctrl``K`](https://clickhouse.com/docs/search)
[Sign in](https://console.clickhouse.cloud/signIn?loc=docs-nav-signIn-cta&glxid=d9c1c271-d847-4188-bc4c-06053c07f641&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fsample&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fsample&utm_ga=GA1.1.1296615451.1775603010)
[Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=d9c1c271-d847-4188-bc4c-06053c07f641&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fsample&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fsample&utm_ga=GA1.1.1296615451.1775603010)
[Get started](https://clickhouse.com/docs/introduction-clickhouse)
[Cloud](https://clickhouse.com/docs/cloud/overview)
[Manage data](https://clickhouse.com/docs/updating-data)
[Server admin](https://clickhouse.com/docs/guides/manage-and-deploy-index)
[Reference](https://clickhouse.com/docs/sql-reference)
[Integrations](https://clickhouse.com/docs/integrations)
[ClickStack](https://clickhouse.com/docs/use-cases/observability/clickstack/overview)
[chDB](https://clickhouse.com/docs/chdb)
[About](https://clickhouse.com/docs/about)
[Knowledge Base](https://clickhouse.com/docs/knowledgebase)
[English](https://clickhouse.com/docs/sql-reference/statements/select/sample)
- [English](https://clickhouse.com/docs/sql-reference/statements/select/sample)
- [日本語](https://clickhouse.com/docs/jp/sql-reference/statements/select/sample)
- [中文](https://clickhouse.com/docs/zh/sql-reference/statements/select/sample)
- [Русский](https://clickhouse.com/docs/ru/sql-reference/statements/select/sample)
- [한국어](https://clickhouse.com/docs/ko/sql-reference/statements/select/sample)
[Skip to main content](https://clickhouse.com/docs/sql-reference/statements/select/sample#__docusaurus_skipToContent_fallback)
[](https://clickhouse.com/)
- [Products](https://clickhouse.com/docs/sql-reference/statements/select/sample)
- [ClickHouse Cloud Best way to use ClickHouse. Available on AWS, GCP, and Azure.](https://clickhouse.com/cloud)
- [BYOC (Bring Your Own Cloud) The fully managed ClickHouse Cloud service, Can be deployed in your AWS account.](https://clickhouse.com/cloud/bring-your-own-cloud)
- [ClickHouse Set up a database with open-source ClickHouse. ClickHouse](https://clickhouse.com/clickhouse)
- [Discover more than 100 integrations.](https://clickhouse.com/integrations)
[Discover more than 100 integrations.](https://clickhouse.com/integrations)
- [Use cases](https://clickhouse.com/docs/sql-reference/statements/select/sample)
- [Real-time analytics](https://clickhouse.com/use-cases/real-time-analytics)
- [Machine Learning & Generative AI](https://clickhouse.com/use-cases/machine-learning-and-data-science)
- [Business Intelligence](https://clickhouse.com/use-cases/data-warehousing)
- [Logs, Events, Traces](https://clickhouse.com/use-cases/observability)
- [All use cases](https://clickhouse.com/use-cases)
[All use cases](https://clickhouse.com/use-cases)
- [Documentation](https://clickhouse.com/docs)
- [Resources](https://clickhouse.com/docs/sql-reference/statements/select/sample)
- [User stories](https://clickhouse.com/user-stories)
- [Blog](https://clickhouse.com/blog)
- [Events](https://clickhouse.com/company/events)
- [Learning and certification](https://clickhouse.com/learn)
- [Comparison](https://clickhouse.com/docs/sql-reference/statements/select/sample)
- [BigQuery](https://clickhouse.com/comparison/bigquery)
- [PostgreSQL](https://clickhouse.com/comparison/postgresql)
- [Redshift](https://clickhouse.com/comparison/redshift)
- [Rockset](https://clickhouse.com/comparison/rockset)
- [Snowflake](https://clickhouse.com/comparison/snowflake)
- [Video](https://clickhouse.com/videos)
- [Demo](https://clickhouse.com/demos)
- [Pricing](https://clickhouse.com/pricing)
- [Contact](https://clickhouse.com/company/contact?loc=nav)
[46\.7k](https://github.com/ClickHouse/ClickHouse?utm_source=clickhouse&utm_medium=website&utm_campaign=website-nav)
[Search`Ctrl``K`](https://clickhouse.com/docs/search)
[Sign in](https://console.clickhouse.cloud/signIn?loc=docs-nav-signIn-cta&glxid=d9c1c271-d847-4188-bc4c-06053c07f641&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fsample&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fsample&utm_ga=GA1.1.1296615451.1775603010)
[Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=d9c1c271-d847-4188-bc4c-06053c07f641&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fsample&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fsample&utm_ga=GA1.1.1296615451.1775603010)
[Get started](https://clickhouse.com/docs/introduction-clickhouse)
[Cloud](https://clickhouse.com/docs/cloud/overview)
[Manage data](https://clickhouse.com/docs/updating-data)
[Server admin](https://clickhouse.com/docs/guides/manage-and-deploy-index)
[Reference](https://clickhouse.com/docs/sql-reference)
[Integrations](https://clickhouse.com/docs/integrations)
[ClickStack](https://clickhouse.com/docs/use-cases/observability/clickstack/overview)
[chDB](https://clickhouse.com/docs/chdb)
[About](https://clickhouse.com/docs/about)
[Knowledge Base](https://clickhouse.com/docs/knowledgebase)
[English](https://clickhouse.com/docs/sql-reference/statements/select/sample)
- [English](https://clickhouse.com/docs/sql-reference/statements/select/sample)
- [日本語](https://clickhouse.com/docs/jp/sql-reference/statements/select/sample)
- [中文](https://clickhouse.com/docs/zh/sql-reference/statements/select/sample)
- [Русский](https://clickhouse.com/docs/ru/sql-reference/statements/select/sample)
- [한국어](https://clickhouse.com/docs/ko/sql-reference/statements/select/sample)
[Search`Ctrl``K`](https://clickhouse.com/docs/search)
- [Introduction](https://clickhouse.com/docs/sql-reference)
- [Syntax](https://clickhouse.com/docs/sql-reference/syntax)
- [Input and Output Formats](https://clickhouse.com/docs/sql-reference/formats)
- [Data types](https://clickhouse.com/docs/sql-reference/data-types)
- [Statements](https://clickhouse.com/docs/sql-reference/statements)
- [SELECT](https://clickhouse.com/docs/sql-reference/statements/select)
- [ALL](https://clickhouse.com/docs/sql-reference/statements/select/all)
- [APPLY](https://clickhouse.com/docs/sql-reference/statements/select/apply-modifier)
- [ARRAY JOIN](https://clickhouse.com/docs/sql-reference/statements/select/array-join)
- [DISTINCT](https://clickhouse.com/docs/sql-reference/statements/select/distinct)
- [EXCEPT](https://clickhouse.com/docs/sql-reference/statements/select/except)
- [EXCEPT](https://clickhouse.com/docs/sql-reference/statements/select/except-modifier)
- [FORMAT](https://clickhouse.com/docs/sql-reference/statements/select/format)
- [FROM](https://clickhouse.com/docs/sql-reference/statements/select/from)
- [GROUP BY](https://clickhouse.com/docs/sql-reference/statements/select/group-by)
- [HAVING](https://clickhouse.com/docs/sql-reference/statements/select/having)
- [INTERSECT](https://clickhouse.com/docs/sql-reference/statements/select/intersect)
- [INTO OUTFILE](https://clickhouse.com/docs/sql-reference/statements/select/into-outfile)
- [JOIN](https://clickhouse.com/docs/sql-reference/statements/select/join)
- [LIMIT BY](https://clickhouse.com/docs/sql-reference/statements/select/limit-by)
- [LIMIT](https://clickhouse.com/docs/sql-reference/statements/select/limit)
- [OFFSET](https://clickhouse.com/docs/sql-reference/statements/select/offset)
- [ORDER BY](https://clickhouse.com/docs/sql-reference/statements/select/order-by)
- [PREWHERE](https://clickhouse.com/docs/sql-reference/statements/select/prewhere)
- [QUALIFY](https://clickhouse.com/docs/sql-reference/statements/select/qualify)
- [REPLACE](https://clickhouse.com/docs/sql-reference/statements/select/replace-modifier)
- [SAMPLE](https://clickhouse.com/docs/sql-reference/statements/select/sample)
- [UNION](https://clickhouse.com/docs/sql-reference/statements/select/union)
- [WHERE](https://clickhouse.com/docs/sql-reference/statements/select/where)
- [WITH](https://clickhouse.com/docs/sql-reference/statements/select/with)
- [INSERT INTO](https://clickhouse.com/docs/sql-reference/statements/insert-into)
- [CREATE](https://clickhouse.com/docs/sql-reference/statements/create)
- [ALTER](https://clickhouse.com/docs/sql-reference/statements/alter)
- [DELETE](https://clickhouse.com/docs/sql-reference/statements/delete)
- [SYSTEM](https://clickhouse.com/docs/sql-reference/statements/system)
- [SHOW](https://clickhouse.com/docs/sql-reference/statements/show)
- [GRANT](https://clickhouse.com/docs/sql-reference/statements/grant)
- [EXPLAIN](https://clickhouse.com/docs/sql-reference/statements/explain)
- [REVOKE](https://clickhouse.com/docs/sql-reference/statements/revoke)
- [UPDATE](https://clickhouse.com/docs/sql-reference/statements/update)
- [ATTACH](https://clickhouse.com/docs/sql-reference/statements/attach)
- [CHECK TABLE](https://clickhouse.com/docs/sql-reference/statements/check-table)
- [DESCRIBE TABLE](https://clickhouse.com/docs/sql-reference/statements/describe-table)
- [DETACH](https://clickhouse.com/docs/sql-reference/statements/detach)
- [DROP](https://clickhouse.com/docs/sql-reference/statements/drop)
- [EXISTS](https://clickhouse.com/docs/sql-reference/statements/exists)
- [KILL](https://clickhouse.com/docs/sql-reference/statements/kill)
- [OPTIMIZE](https://clickhouse.com/docs/sql-reference/statements/optimize)
- [RENAME](https://clickhouse.com/docs/sql-reference/statements/rename)
- [EXCHANGE](https://clickhouse.com/docs/sql-reference/statements/exchange)
- [SET](https://clickhouse.com/docs/sql-reference/statements/set)
- [SET ROLE](https://clickhouse.com/docs/sql-reference/statements/set-role)
- [TRUNCATE](https://clickhouse.com/docs/sql-reference/statements/truncate)
- [EXECUTE AS](https://clickhouse.com/docs/sql-reference/statements/execute_as)
- [PARALLEL WITH](https://clickhouse.com/docs/sql-reference/statements/parallel_with)
- [USE](https://clickhouse.com/docs/sql-reference/statements/use)
- [WATCH](https://clickhouse.com/docs/sql-reference/statements/watch)
- [MOVE](https://clickhouse.com/docs/sql-reference/statements/move)
- [CHECK GRANT](https://clickhouse.com/docs/sql-reference/statements/check-grant)
- [UNDROP](https://clickhouse.com/docs/sql-reference/statements/undrop)
- [Operators](https://clickhouse.com/docs/sql-reference/operators)
- [Engines](https://clickhouse.com/docs/engines)
- [Database Engines](https://clickhouse.com/docs/engines/database-engines)
- [Table Engines](https://clickhouse.com/docs/engines/table-engines)
- [Functions](https://clickhouse.com/docs/sql-reference/functions)
- [Regular functions](https://clickhouse.com/docs/sql-reference/functions/regular-functions)
- [Aggregate functions](https://clickhouse.com/docs/sql-reference/aggregate-functions)
- [Table functions](https://clickhouse.com/docs/sql-reference/table-functions)
- [Window functions](https://clickhouse.com/docs/sql-reference/window-functions)
- [Formats](https://clickhouse.com/docs/interfaces/formats)
- [Data Lakes](https://clickhouse.com/docs/sql-reference/datalakes)
- [Introduction](https://clickhouse.com/docs/sql-reference)
- [Statements](https://clickhouse.com/docs/sql-reference/statements)
- [SELECT](https://clickhouse.com/docs/sql-reference/statements/select)
- SAMPLE
[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/statements/select/sample.md)
# SAMPLE Clause
The `SAMPLE` clause allows for approximated `SELECT` query processing.
When data sampling is enabled, the query is not performed on all the data, but only on a certain fraction of data (sample). For example, if you need to calculate statistics for all the visits, it is enough to execute the query on the 1/10 fraction of all the visits and then multiply the result by 10.
Approximated query processing can be useful in the following cases:
- When you have strict latency requirements (like below 100ms) but you can't justify the cost of additional hardware resources to meet them.
- When your raw data is not accurate, so approximation does not noticeably degrade the quality.
- Business requirements target approximate results (for cost-effectiveness, or to market exact results to premium users).
Note
You can only use sampling with the tables in the [MergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree) family, and only if the sampling expression was specified during table creation (see [MergeTree engine](https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree#table_engine-mergetree-creating-a-table)).
The features of data sampling are listed below:
- Data sampling is a deterministic mechanism. The result of the same `SELECT .. SAMPLE` query is always the same.
- Sampling works consistently for different tables. For tables with a single sampling key, a sample with the same coefficient always selects the same subset of possible data. For example, a sample of user IDs takes rows with the same subset of all the possible user IDs from different tables. This means that you can use the sample in subqueries in the [IN](https://clickhouse.com/docs/sql-reference/operators/in) clause. Also, you can join samples using the [JOIN](https://clickhouse.com/docs/sql-reference/statements/select/join) clause.
- Sampling allows reading less data from a disk. Note that you must specify the sampling key correctly. For more information, see [Creating a MergeTree Table](https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree#table_engine-mergetree-creating-a-table).
For the `SAMPLE` clause the following syntax is supported:
| SAMPLE Clause Syntax | Description |
|---|---|
| `SAMPLE k` | Here `k` is the number from 0 to 1. The query is executed on `k` fraction of data. For example, `SAMPLE 0.1` runs the query on 10% of data. [Read more](https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-k) |
| `SAMPLE n` | Here `n` is a sufficiently large integer. The query is executed on a sample of at least `n` rows (but not significantly more than this). For example, `SAMPLE 10000000` runs the query on a minimum of 10,000,000 rows. [Read more](https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-n) |
| `SAMPLE k OFFSET m` | Here `k` and `m` are the numbers from 0 to 1. The query is executed on a sample of `k` fraction of the data. The data used for the sample is offset by `m` fraction. [Read more](https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-k-offset-m) |
## SAMPLE K[](https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-k "Direct link to SAMPLE K")
Here `k` is the number from 0 to 1 (both fractional and decimal notations are supported). For example, `SAMPLE 1/2` or `SAMPLE 0.5`.
In a `SAMPLE k` clause, the sample is taken from the `k` fraction of data. The example is shown below:
```
SELECT
Title,
count() * 10 AS PageViews
FROM hits_distributed
SAMPLE 0.1
WHERE
CounterID = 34
GROUP BY Title
ORDER BY PageViews DESC LIMIT 1000
```
In this example, the query is executed on a sample from 0.1 (10%) of data. Values of aggregate functions are not corrected automatically, so to get an approximate result, the value `count()` is manually multiplied by 10.
## SAMPLE N[](https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-n "Direct link to SAMPLE N")
Here `n` is a sufficiently large integer. For example, `SAMPLE 10000000`.
In this case, the query is executed on a sample of at least `n` rows (but not significantly more than this). For example, `SAMPLE 10000000` runs the query on a minimum of 10,000,000 rows.
Since the minimum unit for data reading is one granule (its size is set by the `index_granularity` setting), it makes sense to set a sample that is much larger than the size of the granule.
When using the `SAMPLE n` clause, you do not know which relative percent of data was processed. So you do not know the coefficient the aggregate functions should be multiplied by. Use the `_sample_factor` virtual column to get the approximate result.
The `_sample_factor` column contains relative coefficients that are calculated dynamically. This column is created automatically when you [create](https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree#table_engine-mergetree-creating-a-table) a table with the specified sampling key. The usage examples of the `_sample_factor` column are shown below.
Let's consider the table `visits`, which contains the statistics about site visits. The first example shows how to calculate the number of page views:
```
SELECT sum(PageViews * _sample_factor)
FROM visits
SAMPLE 10000000
```
The next example shows how to calculate the total number of visits:
```
SELECT sum(_sample_factor)
FROM visits
SAMPLE 10000000
```
The example below shows how to calculate the average session duration. Note that you do not need to use the relative coefficient to calculate the average values.
```
SELECT avg(Duration)
FROM visits
SAMPLE 10000000
```
## SAMPLE K OFFSET M[](https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-k-offset-m "Direct link to SAMPLE K OFFSET M")
Here `k` and `m` are numbers from 0 to 1. Examples are shown below.
**Example 1**
```
SAMPLE 1/10
```
In this example, the sample is 1/10th of all data:
`[++------------]`
**Example 2**
```
SAMPLE 1/10 OFFSET 1/2
```
Here, a sample of 10% is taken from the second half of the data.
`[------++------]`
[Previous REPLACE](https://clickhouse.com/docs/sql-reference/statements/select/replace-modifier)
[Next UNION](https://clickhouse.com/docs/sql-reference/statements/select/union)
- [SAMPLE K](https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-k)
- [SAMPLE N](https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-n)
- [SAMPLE K OFFSET M](https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-k-offset-m)
Was this page helpful?
###### Try ClickHouse Cloud for FREE
Separation of storage and compute, automatic scaling, built-in SQL console, and lots more. \$300 in free credits when signing up.
[Try it for Free](https://console.clickhouse.cloud/signUp?loc=doc-card-banner&glxid=d9c1c271-d847-4188-bc4c-06053c07f641&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fsample&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fsample&utm_ga=GA1.1.1296615451.1775603010)
© 2016–2026 ClickHouse, Inc.
[Trademark](https://clickhouse.com/legal/trademark-policy)·[Privacy](https://clickhouse.com/legal/privacy-policy)·[Security](https://trust.clickhouse.com/)·[Terms of Service](https://clickhouse.com/legal/agreements/terms-of-service)

© 2016–2026 ClickHouse, Inc.
[Trademark](https://clickhouse.com/legal/trademark-policy)·[Privacy](https://clickhouse.com/legal/privacy-policy)·[Security](https://trust.clickhouse.com/)·[Terms of Service](https://clickhouse.com/legal/agreements/terms-of-service)

[](https://clickhouse.com/)
EN
- Get started▼
- Cloud▼
- Manage data▼
- Server admin▼
- Reference▼
- Integrations▼
- ClickStack▼
- chDB▼
- About▼
[](https://clickhouse.com/)
EN
main-menu
- Introduction▼
- [Syntax](https://clickhouse.com/docs/sql-reference/syntax)
- [Input and Output Formats](https://clickhouse.com/docs/sql-reference/formats)
- Data types▼
- Statements▼
- SELECT▼
- [ALL](https://clickhouse.com/docs/sql-reference/statements/select/all)
- [APPLY](https://clickhouse.com/docs/sql-reference/statements/select/apply-modifier)
- [ARRAY JOIN](https://clickhouse.com/docs/sql-reference/statements/select/array-join)
- [DISTINCT](https://clickhouse.com/docs/sql-reference/statements/select/distinct)
- [EXCEPT](https://clickhouse.com/docs/sql-reference/statements/select/except)
- [EXCEPT](https://clickhouse.com/docs/sql-reference/statements/select/except-modifier)
- [FORMAT](https://clickhouse.com/docs/sql-reference/statements/select/format)
- [FROM](https://clickhouse.com/docs/sql-reference/statements/select/from)
- [GROUP BY](https://clickhouse.com/docs/sql-reference/statements/select/group-by)
- [HAVING](https://clickhouse.com/docs/sql-reference/statements/select/having)
- [INTERSECT](https://clickhouse.com/docs/sql-reference/statements/select/intersect)
- [INTO OUTFILE](https://clickhouse.com/docs/sql-reference/statements/select/into-outfile)
- [JOIN](https://clickhouse.com/docs/sql-reference/statements/select/join)
- [LIMIT BY](https://clickhouse.com/docs/sql-reference/statements/select/limit-by)
- [LIMIT](https://clickhouse.com/docs/sql-reference/statements/select/limit)
- [OFFSET](https://clickhouse.com/docs/sql-reference/statements/select/offset)
- [ORDER BY](https://clickhouse.com/docs/sql-reference/statements/select/order-by)
- [PREWHERE](https://clickhouse.com/docs/sql-reference/statements/select/prewhere)
- [QUALIFY](https://clickhouse.com/docs/sql-reference/statements/select/qualify)
- [REPLACE](https://clickhouse.com/docs/sql-reference/statements/select/replace-modifier)
- [SAMPLE](https://clickhouse.com/docs/sql-reference/statements/select/sample)
- [UNION](https://clickhouse.com/docs/sql-reference/statements/select/union)
- [WHERE](https://clickhouse.com/docs/sql-reference/statements/select/where)
- [WITH](https://clickhouse.com/docs/sql-reference/statements/select/with)
- [INSERT INTO](https://clickhouse.com/docs/sql-reference/statements/insert-into)
- CREATE▼
- ALTER▼
- [DELETE](https://clickhouse.com/docs/sql-reference/statements/delete)
- [SYSTEM](https://clickhouse.com/docs/sql-reference/statements/system)
- [SHOW](https://clickhouse.com/docs/sql-reference/statements/show)
- [GRANT](https://clickhouse.com/docs/sql-reference/statements/grant)
- [EXPLAIN](https://clickhouse.com/docs/sql-reference/statements/explain)
- [REVOKE](https://clickhouse.com/docs/sql-reference/statements/revoke)
- [UPDATE](https://clickhouse.com/docs/sql-reference/statements/update)
- [ATTACH](https://clickhouse.com/docs/sql-reference/statements/attach)
- [CHECK TABLE](https://clickhouse.com/docs/sql-reference/statements/check-table)
- [DESCRIBE TABLE](https://clickhouse.com/docs/sql-reference/statements/describe-table)
- [DETACH](https://clickhouse.com/docs/sql-reference/statements/detach)
- [DROP](https://clickhouse.com/docs/sql-reference/statements/drop)
- [EXISTS](https://clickhouse.com/docs/sql-reference/statements/exists)
- [KILL](https://clickhouse.com/docs/sql-reference/statements/kill)
- [OPTIMIZE](https://clickhouse.com/docs/sql-reference/statements/optimize)
- [RENAME](https://clickhouse.com/docs/sql-reference/statements/rename)
- [EXCHANGE](https://clickhouse.com/docs/sql-reference/statements/exchange)
- [SET](https://clickhouse.com/docs/sql-reference/statements/set)
- [SET ROLE](https://clickhouse.com/docs/sql-reference/statements/set-role)
- [TRUNCATE](https://clickhouse.com/docs/sql-reference/statements/truncate)
- [EXECUTE AS](https://clickhouse.com/docs/sql-reference/statements/execute_as)
- [PARALLEL WITH](https://clickhouse.com/docs/sql-reference/statements/parallel_with)
- [USE](https://clickhouse.com/docs/sql-reference/statements/use)
- [WATCH](https://clickhouse.com/docs/sql-reference/statements/watch)
- [MOVE](https://clickhouse.com/docs/sql-reference/statements/move)
- [CHECK GRANT](https://clickhouse.com/docs/sql-reference/statements/check-grant)
- [UNDROP](https://clickhouse.com/docs/sql-reference/statements/undrop)
- Operators▼
- Engines▼
- Functions▼
- Formats▼
- [Data Lakes](https://clickhouse.com/docs/sql-reference/datalakes) |
| Readable Markdown | The `SAMPLE` clause allows for approximated `SELECT` query processing.
When data sampling is enabled, the query is not performed on all the data, but only on a certain fraction of data (sample). For example, if you need to calculate statistics for all the visits, it is enough to execute the query on the 1/10 fraction of all the visits and then multiply the result by 10.
Approximated query processing can be useful in the following cases:
- When you have strict latency requirements (like below 100ms) but you can't justify the cost of additional hardware resources to meet them.
- When your raw data is not accurate, so approximation does not noticeably degrade the quality.
- Business requirements target approximate results (for cost-effectiveness, or to market exact results to premium users).
Note
You can only use sampling with the tables in the [MergeTree](https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree) family, and only if the sampling expression was specified during table creation (see [MergeTree engine](https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree#table_engine-mergetree-creating-a-table)).
The features of data sampling are listed below:
- Data sampling is a deterministic mechanism. The result of the same `SELECT .. SAMPLE` query is always the same.
- Sampling works consistently for different tables. For tables with a single sampling key, a sample with the same coefficient always selects the same subset of possible data. For example, a sample of user IDs takes rows with the same subset of all the possible user IDs from different tables. This means that you can use the sample in subqueries in the [IN](https://clickhouse.com/docs/sql-reference/operators/in) clause. Also, you can join samples using the [JOIN](https://clickhouse.com/docs/sql-reference/statements/select/join) clause.
- Sampling allows reading less data from a disk. Note that you must specify the sampling key correctly. For more information, see [Creating a MergeTree Table](https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree#table_engine-mergetree-creating-a-table).
For the `SAMPLE` clause the following syntax is supported:
| SAMPLE Clause Syntax | Description |
|---|---|
| `SAMPLE k` | Here `k` is the number from 0 to 1. The query is executed on `k` fraction of data. For example, `SAMPLE 0.1` runs the query on 10% of data. [Read more](https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-k) |
| `SAMPLE n` | Here `n` is a sufficiently large integer. The query is executed on a sample of at least `n` rows (but not significantly more than this). For example, `SAMPLE 10000000` runs the query on a minimum of 10,000,000 rows. [Read more](https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-n) |
| `SAMPLE k OFFSET m` | Here `k` and `m` are the numbers from 0 to 1. The query is executed on a sample of `k` fraction of the data. The data used for the sample is offset by `m` fraction. [Read more](https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-k-offset-m) |
## SAMPLE K[](https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-k "Direct link to SAMPLE K")
Here `k` is the number from 0 to 1 (both fractional and decimal notations are supported). For example, `SAMPLE 1/2` or `SAMPLE 0.5`.
In a `SAMPLE k` clause, the sample is taken from the `k` fraction of data. The example is shown below:
```
SELECT
Title,
count() * 10 AS PageViews
FROM hits_distributed
SAMPLE 0.1
WHERE
CounterID = 34
GROUP BY Title
ORDER BY PageViews DESC LIMIT 1000
```
In this example, the query is executed on a sample from 0.1 (10%) of data. Values of aggregate functions are not corrected automatically, so to get an approximate result, the value `count()` is manually multiplied by 10.
## SAMPLE N[](https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-n "Direct link to SAMPLE N")
Here `n` is a sufficiently large integer. For example, `SAMPLE 10000000`.
In this case, the query is executed on a sample of at least `n` rows (but not significantly more than this). For example, `SAMPLE 10000000` runs the query on a minimum of 10,000,000 rows.
Since the minimum unit for data reading is one granule (its size is set by the `index_granularity` setting), it makes sense to set a sample that is much larger than the size of the granule.
When using the `SAMPLE n` clause, you do not know which relative percent of data was processed. So you do not know the coefficient the aggregate functions should be multiplied by. Use the `_sample_factor` virtual column to get the approximate result.
The `_sample_factor` column contains relative coefficients that are calculated dynamically. This column is created automatically when you [create](https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree#table_engine-mergetree-creating-a-table) a table with the specified sampling key. The usage examples of the `_sample_factor` column are shown below.
Let's consider the table `visits`, which contains the statistics about site visits. The first example shows how to calculate the number of page views:
```
SELECT sum(PageViews * _sample_factor)
FROM visits
SAMPLE 10000000
```
The next example shows how to calculate the total number of visits:
```
SELECT sum(_sample_factor)
FROM visits
SAMPLE 10000000
```
The example below shows how to calculate the average session duration. Note that you do not need to use the relative coefficient to calculate the average values.
```
SELECT avg(Duration)
FROM visits
SAMPLE 10000000
```
## SAMPLE K OFFSET M[](https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-k-offset-m "Direct link to SAMPLE K OFFSET M")
Here `k` and `m` are numbers from 0 to 1. Examples are shown below.
**Example 1**
```
SAMPLE 1/10
```
In this example, the sample is 1/10th of all data:
`[++------------]`
**Example 2**
```
SAMPLE 1/10 OFFSET 1/2
```
Here, a sample of 10% is taken from the second half of the data.
`[------++------]` |
| Shard | 89 (laksa) |
| Root Hash | 12633450985039531489 |
| Unparsed URL | com,clickhouse!/docs/sql-reference/statements/select/sample s443 |