βΉοΈ 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.5 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/distinct |
| Last Crawled | 2026-03-27 06:57:46 (13 days ago) |
| First Indexed | 2025-02-21 03:07:42 (1 year ago) |
| HTTP Status Code | 200 |
| Meta Title | DISTINCT Clause | ClickHouse Docs |
| Meta Description | Documentation for DISTINCT Clause |
| Meta Canonical | null |
| Boilerpipe Text | If
SELECT DISTINCT
is specified, only unique rows will remain in a query result. Thus, only a single row will remain out of all the sets of fully matching rows in the result.
You can specify the list of columns that must have unique values:
SELECT DISTINCT ON (column1, column2,...)
. If the columns are not specified, all of them are taken into consideration.
Consider the table:
ββaββ¬βbββ¬βcββ
β 1 β 1 β 1 β
β 1 β 1 β 1 β
β 2 β 2 β 2 β
β 2 β 2 β 2 β
β 1 β 1 β 2 β
β 1 β 2 β 2 β
βββββ΄ββββ΄ββββ
Using
DISTINCT
without specifying columns:
SELECT
DISTINCT
*
FROM
t1
;
ββaββ¬βbββ¬βcββ
β 1 β 1 β 1 β
β 2 β 2 β 2 β
β 1 β 1 β 2 β
β 1 β 2 β 2 β
βββββ΄ββββ΄ββββ
Using
DISTINCT
with specified columns:
SELECT
DISTINCT
ON
(
a
,
b
)
*
FROM
t1
;
ββaββ¬βbββ¬βcββ
β 1 β 1 β 1 β
β 2 β 2 β 2 β
β 1 β 2 β 2 β
βββββ΄ββββ΄ββββ
DISTINCT and ORDER BY
β
ClickHouse supports using the
DISTINCT
and
ORDER BY
clauses for different columns in one query. The
DISTINCT
clause is executed before the
ORDER BY
clause.
Consider the table:
ββaββ¬βbββ
β 2 β 1 β
β 1 β 2 β
β 3 β 3 β
β 2 β 4 β
βββββ΄ββββ
Selecting data:
SELECT
DISTINCT
a
FROM
t1
ORDER
BY
b
ASC
;
ββaββ
β 2 β
β 1 β
β 3 β
βββββ
Selecting data with the different sorting direction:
SELECT
DISTINCT
a
FROM
t1
ORDER
BY
b
DESC
;
ββaββ
β 3 β
β 1 β
β 2 β
βββββ
Row
2, 4
was cut before sorting.
Take this implementation specificity into account when programming queries.
Null Processing
β
DISTINCT
works with
NULL
as if
NULL
were a specific value, and
NULL==NULL
. In other words, in the
DISTINCT
results, different combinations with
NULL
occur only once. It differs from
NULL
processing in most other contexts.
Alternatives
β
It is possible to obtain the same result by applying
GROUP BY
across the same set of values as specified as
SELECT
clause, without using any aggregate functions. But there are few differences from
GROUP BY
approach:
DISTINCT
can be applied together with
GROUP BY
.
When
ORDER BY
is omitted and
LIMIT
is defined, the query stops running immediately after the required number of different rows has been read.
Data blocks are output as they are processed, without waiting for the entire query to finish running. |
| Markdown | [Skip to main content](https://clickhouse.com/docs/sql-reference/statements/select/distinct#__docusaurus_skipToContent_fallback)
[](https://clickhouse.com/)
- [Products](https://clickhouse.com/docs/sql-reference/statements/select/distinct)
- [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/distinct)
- [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/distinct)
- [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/distinct)
- [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\.6k](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=7113a784-3608-47e5-8713-9b61fec3a7c0&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fdistinct&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fdistinct&utm_ga=GA1.1.143345195.1774594668)
[Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=7113a784-3608-47e5-8713-9b61fec3a7c0&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fdistinct&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fdistinct&utm_ga=GA1.1.143345195.1774594668)
[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/distinct)
- [English](https://clickhouse.com/docs/sql-reference/statements/select/distinct)
- [ζ₯ζ¬θͺ](https://clickhouse.com/docs/jp/sql-reference/statements/select/distinct)
- [δΈζ](https://clickhouse.com/docs/zh/sql-reference/statements/select/distinct)
- [Π ΡΡΡΠΊΠΈΠΉ](https://clickhouse.com/docs/ru/sql-reference/statements/select/distinct)
- [νκ΅μ΄](https://clickhouse.com/docs/ko/sql-reference/statements/select/distinct)
[Skip to main content](https://clickhouse.com/docs/sql-reference/statements/select/distinct#__docusaurus_skipToContent_fallback)
[](https://clickhouse.com/)
- [Products](https://clickhouse.com/docs/sql-reference/statements/select/distinct)
- [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/distinct)
- [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/distinct)
- [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/distinct)
- [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\.6k](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=7113a784-3608-47e5-8713-9b61fec3a7c0&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fdistinct&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fdistinct&utm_ga=GA1.1.143345195.1774594668)
[Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=7113a784-3608-47e5-8713-9b61fec3a7c0&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fdistinct&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fdistinct&utm_ga=GA1.1.143345195.1774594668)
[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/distinct)
- [English](https://clickhouse.com/docs/sql-reference/statements/select/distinct)
- [ζ₯ζ¬θͺ](https://clickhouse.com/docs/jp/sql-reference/statements/select/distinct)
- [δΈζ](https://clickhouse.com/docs/zh/sql-reference/statements/select/distinct)
- [Π ΡΡΡΠΊΠΈΠΉ](https://clickhouse.com/docs/ru/sql-reference/statements/select/distinct)
- [νκ΅μ΄](https://clickhouse.com/docs/ko/sql-reference/statements/select/distinct)
[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)
- DISTINCT
[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/statements/select/distinct.md)
# DISTINCT Clause
If `SELECT DISTINCT` is specified, only unique rows will remain in a query result. Thus, only a single row will remain out of all the sets of fully matching rows in the result.
You can specify the list of columns that must have unique values: `SELECT DISTINCT ON (column1, column2,...)`. If the columns are not specified, all of them are taken into consideration.
Consider the table:
```
ββaββ¬βbββ¬βcββ
β 1 β 1 β 1 β
β 1 β 1 β 1 β
β 2 β 2 β 2 β
β 2 β 2 β 2 β
β 1 β 1 β 2 β
β 1 β 2 β 2 β
βββββ΄ββββ΄ββββ
```
Using `DISTINCT` without specifying columns:
```
SELECT DISTINCT * FROM t1;
```
```
ββaββ¬βbββ¬βcββ
β 1 β 1 β 1 β
β 2 β 2 β 2 β
β 1 β 1 β 2 β
β 1 β 2 β 2 β
βββββ΄ββββ΄ββββ
```
Using `DISTINCT` with specified columns:
```
SELECT DISTINCT ON (a,b) * FROM t1;
```
```
ββaββ¬βbββ¬βcββ
β 1 β 1 β 1 β
β 2 β 2 β 2 β
β 1 β 2 β 2 β
βββββ΄ββββ΄ββββ
```
## DISTINCT and ORDER BY[β](https://clickhouse.com/docs/sql-reference/statements/select/distinct#distinct-and-order-by "Direct link to DISTINCT and ORDER BY")
ClickHouse supports using the `DISTINCT` and `ORDER BY` clauses for different columns in one query. The `DISTINCT` clause is executed before the `ORDER BY` clause.
Consider the table:
```
ββaββ¬βbββ
β 2 β 1 β
β 1 β 2 β
β 3 β 3 β
β 2 β 4 β
βββββ΄ββββ
```
Selecting data:
```
SELECT DISTINCT a FROM t1 ORDER BY b ASC;
```
```
ββaββ
β 2 β
β 1 β
β 3 β
βββββ
```
Selecting data with the different sorting direction:
```
SELECT DISTINCT a FROM t1 ORDER BY b DESC;
```
```
ββaββ
β 3 β
β 1 β
β 2 β
βββββ
```
Row `2, 4` was cut before sorting.
Take this implementation specificity into account when programming queries.
## Null Processing[β](https://clickhouse.com/docs/sql-reference/statements/select/distinct#null-processing "Direct link to Null Processing")
`DISTINCT` works with [NULL](https://clickhouse.com/docs/sql-reference/syntax#null) as if `NULL` were a specific value, and `NULL==NULL`. In other words, in the `DISTINCT` results, different combinations with `NULL` occur only once. It differs from `NULL` processing in most other contexts.
## Alternatives[β](https://clickhouse.com/docs/sql-reference/statements/select/distinct#alternatives "Direct link to Alternatives")
It is possible to obtain the same result by applying [GROUP BY](https://clickhouse.com/docs/sql-reference/statements/select/group-by) across the same set of values as specified as `SELECT` clause, without using any aggregate functions. But there are few differences from `GROUP BY` approach:
- `DISTINCT` can be applied together with `GROUP BY`.
- When [ORDER BY](https://clickhouse.com/docs/sql-reference/statements/select/order-by) is omitted and [LIMIT](https://clickhouse.com/docs/sql-reference/statements/select/limit) is defined, the query stops running immediately after the required number of different rows has been read.
- Data blocks are output as they are processed, without waiting for the entire query to finish running.
[Previous ARRAY JOIN](https://clickhouse.com/docs/sql-reference/statements/select/array-join)
[Next EXCEPT](https://clickhouse.com/docs/sql-reference/statements/select/except)
- [DISTINCT and ORDER BY](https://clickhouse.com/docs/sql-reference/statements/select/distinct#distinct-and-order-by)
- [Null Processing](https://clickhouse.com/docs/sql-reference/statements/select/distinct#null-processing)
- [Alternatives](https://clickhouse.com/docs/sql-reference/statements/select/distinct#alternatives)
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=7113a784-3608-47e5-8713-9b61fec3a7c0&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fdistinct&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Fdistinct&utm_ga=GA1.1.143345195.1774594668)
Β© 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 | If `SELECT DISTINCT` is specified, only unique rows will remain in a query result. Thus, only a single row will remain out of all the sets of fully matching rows in the result.
You can specify the list of columns that must have unique values: `SELECT DISTINCT ON (column1, column2,...)`. If the columns are not specified, all of them are taken into consideration.
Consider the table:
```
ββaββ¬βbββ¬βcββ
β 1 β 1 β 1 β
β 1 β 1 β 1 β
β 2 β 2 β 2 β
β 2 β 2 β 2 β
β 1 β 1 β 2 β
β 1 β 2 β 2 β
βββββ΄ββββ΄ββββ
```
Using `DISTINCT` without specifying columns:
```
SELECT DISTINCT * FROM t1;
```
```
ββaββ¬βbββ¬βcββ
β 1 β 1 β 1 β
β 2 β 2 β 2 β
β 1 β 1 β 2 β
β 1 β 2 β 2 β
βββββ΄ββββ΄ββββ
```
Using `DISTINCT` with specified columns:
```
SELECT DISTINCT ON (a,b) * FROM t1;
```
```
ββaββ¬βbββ¬βcββ
β 1 β 1 β 1 β
β 2 β 2 β 2 β
β 1 β 2 β 2 β
βββββ΄ββββ΄ββββ
```
## DISTINCT and ORDER BY[β](https://clickhouse.com/docs/sql-reference/statements/select/distinct#distinct-and-order-by "Direct link to DISTINCT and ORDER BY")
ClickHouse supports using the `DISTINCT` and `ORDER BY` clauses for different columns in one query. The `DISTINCT` clause is executed before the `ORDER BY` clause.
Consider the table:
```
ββaββ¬βbββ
β 2 β 1 β
β 1 β 2 β
β 3 β 3 β
β 2 β 4 β
βββββ΄ββββ
```
Selecting data:
```
SELECT DISTINCT a FROM t1 ORDER BY b ASC;
```
```
ββaββ
β 2 β
β 1 β
β 3 β
βββββ
```
Selecting data with the different sorting direction:
```
SELECT DISTINCT a FROM t1 ORDER BY b DESC;
```
```
ββaββ
β 3 β
β 1 β
β 2 β
βββββ
```
Row `2, 4` was cut before sorting.
Take this implementation specificity into account when programming queries.
## Null Processing[β](https://clickhouse.com/docs/sql-reference/statements/select/distinct#null-processing "Direct link to Null Processing")
`DISTINCT` works with [NULL](https://clickhouse.com/docs/sql-reference/syntax#null) as if `NULL` were a specific value, and `NULL==NULL`. In other words, in the `DISTINCT` results, different combinations with `NULL` occur only once. It differs from `NULL` processing in most other contexts.
## Alternatives[β](https://clickhouse.com/docs/sql-reference/statements/select/distinct#alternatives "Direct link to Alternatives")
It is possible to obtain the same result by applying [GROUP BY](https://clickhouse.com/docs/sql-reference/statements/select/group-by) across the same set of values as specified as `SELECT` clause, without using any aggregate functions. But there are few differences from `GROUP BY` approach:
- `DISTINCT` can be applied together with `GROUP BY`.
- When [ORDER BY](https://clickhouse.com/docs/sql-reference/statements/select/order-by) is omitted and [LIMIT](https://clickhouse.com/docs/sql-reference/statements/select/limit) is defined, the query stops running immediately after the required number of different rows has been read.
- Data blocks are output as they are processed, without waiting for the entire query to finish running. |
| Shard | 89 (laksa) |
| Root Hash | 12633450985039531489 |
| Unparsed URL | com,clickhouse!/docs/sql-reference/statements/select/distinct s443 |