βΉοΈ 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.1 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/limit-by |
| Last Crawled | 2026-04-07 23:08:09 (2 days ago) |
| First Indexed | 2025-02-21 02:31:48 (1 year ago) |
| HTTP Status Code | 200 |
| Meta Title | LIMIT BY Clause | ClickHouse Docs |
| Meta Description | Documentation for LIMIT BY Clause |
| Meta Canonical | null |
| Boilerpipe Text | A query with the
LIMIT n BY expressions
clause selects the first
n
rows for each distinct value of
expressions
. The key for
LIMIT BY
can contain any number of
expressions
.
ClickHouse supports the following syntax variants:
LIMIT [offset_value, ]n BY expressions
LIMIT n OFFSET offset_value BY expressions
During query processing, ClickHouse selects data ordered by sorting key. The sorting key is set explicitly using an
ORDER BY
clause or implicitly as a property of the table engine (row order is only guaranteed when using
ORDER BY
, otherwise the row blocks will not be ordered due to multi-threading). Then ClickHouse applies
LIMIT n BY expressions
and returns the first
n
rows for each distinct combination of
expressions
. If
OFFSET
is specified, then for each data block that belongs to a distinct combination of
expressions
, ClickHouse skips
offset_value
number of rows from the beginning of the block and returns a maximum of
n
rows as a result. If
offset_value
is bigger than the number of rows in the data block, ClickHouse returns zero rows from the block.
Note
LIMIT BY
is not related to
LIMIT
. They can both be used in the same query.
If you want to use column numbers instead of column names in the
LIMIT BY
clause, enable the setting
enable_positional_arguments
.
Examples
β
Sample table:
CREATE
TABLE
limit_by
(
id
Int
,
val
Int
)
ENGINE
=
Memory
;
INSERT
INTO
limit_by
VALUES
(
1
,
10
)
,
(
1
,
11
)
,
(
1
,
12
)
,
(
2
,
20
)
,
(
2
,
21
)
;
Queries:
SELECT
*
FROM
limit_by
ORDER
BY
id
,
val
LIMIT
2
BY
id
ββidββ¬βvalββ
β 1 β 10 β
β 1 β 11 β
β 2 β 20 β
β 2 β 21 β
ββββββ΄ββββββ
SELECT
*
FROM
limit_by
ORDER
BY
id
,
val
LIMIT
1
,
2
BY
id
ββidββ¬βvalββ
β 1 β 11 β
β 1 β 12 β
β 2 β 21 β
ββββββ΄ββββββ
The
SELECT * FROM limit_by ORDER BY id, val LIMIT 2 OFFSET 1 BY id
query returns the same result.
The following query returns the top 5 referrers for each
domain, device_type
pair with a maximum of 100 rows in total (
LIMIT n BY + LIMIT
).
SELECT
domainWithoutWWW
(
URL
)
AS
domain
,
domainWithoutWWW
(
REFERRER_URL
)
AS
referrer
,
device_type
,
count
(
)
cnt
FROM
hits
GROUP
BY
domain
,
referrer
,
device_type
ORDER
BY
cnt
DESC
LIMIT
5
BY
domain
,
device_type
LIMIT
100
LIMIT BY ALL
β
LIMIT BY ALL
is equivalent to listing all the SELECT-ed expressions that are not aggregate functions.
For example:
SELECT
col1
,
col2
,
col3
FROM
table
LIMIT
2
BY
ALL
is the same as
SELECT
col1
,
col2
,
col3
FROM
table
LIMIT
2
BY
col1
,
col2
,
col3
For a special case that if there is a function having both aggregate functions and other fields as its arguments, the
LIMIT BY
keys will contain the maximum non-aggregate fields we can extract from it.
For example:
SELECT
substring
(
a
,
4
,
2
)
,
substring
(
substring
(
a
,
1
,
2
)
,
1
,
count
(
b
)
)
FROM
t
LIMIT
2
BY
ALL
is the same as
SELECT
substring
(
a
,
4
,
2
)
,
substring
(
substring
(
a
,
1
,
2
)
,
1
,
count
(
b
)
)
FROM
t
LIMIT
2
BY
substring
(
a
,
4
,
2
)
,
substring
(
a
,
1
,
2
)
Examples
β
Sample table:
CREATE
TABLE
limit_by
(
id
Int
,
val
Int
)
ENGINE
=
Memory
;
INSERT
INTO
limit_by
VALUES
(
1
,
10
)
,
(
1
,
11
)
,
(
1
,
12
)
,
(
2
,
20
)
,
(
2
,
21
)
;
Queries:
SELECT
*
FROM
limit_by
ORDER
BY
id
,
val
LIMIT
2
BY
id
ββidββ¬βvalββ
β 1 β 10 β
β 1 β 11 β
β 2 β 20 β
β 2 β 21 β
ββββββ΄ββββββ
SELECT
*
FROM
limit_by
ORDER
BY
id
,
val
LIMIT
1
,
2
BY
id
ββidββ¬βvalββ
β 1 β 11 β
β 1 β 12 β
β 2 β 21 β
ββββββ΄ββββββ
The
SELECT * FROM limit_by ORDER BY id, val LIMIT 2 OFFSET 1 BY id
query returns the same result.
Using
LIMIT BY ALL
:
SELECT
id
,
val
FROM
limit_by
ORDER
BY
id
,
val
LIMIT
2
BY
ALL
This is equivalent to:
SELECT
id
,
val
FROM
limit_by
ORDER
BY
id
,
val
LIMIT
2
BY
id
,
val
The following query returns the top 5 referrers for each
domain, device_type
pair with a maximum of 100 rows in total (
LIMIT n BY + LIMIT
).
SELECT
domainWithoutWWW
(
URL
)
AS
domain
,
domainWithoutWWW
(
REFERRER_URL
)
AS
referrer
,
device_type
,
count
(
)
cnt
FROM
hits
GROUP
BY
domain
,
referrer
,
device_type
ORDER
BY
cnt
DESC
LIMIT
5
BY
domain
,
device_type
LIMIT
100 |
| Markdown | [Skip to main content](https://clickhouse.com/docs/sql-reference/statements/select/limit-by#__docusaurus_skipToContent_fallback)
[](https://clickhouse.com/)
- [Products](https://clickhouse.com/docs/sql-reference/statements/select/limit-by)
- [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/limit-by)
- [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/limit-by)
- [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/limit-by)
- [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=66b0bd70-8178-4af1-8668-a09558d0d04d&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Flimit-by&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Flimit-by&utm_ga=GA1.1.510657712.1775603292)
[Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=66b0bd70-8178-4af1-8668-a09558d0d04d&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Flimit-by&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Flimit-by&utm_ga=GA1.1.510657712.1775603292)
[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/limit-by)
- [English](https://clickhouse.com/docs/sql-reference/statements/select/limit-by)
- [ζ₯ζ¬θͺ](https://clickhouse.com/docs/jp/sql-reference/statements/select/limit-by)
- [δΈζ](https://clickhouse.com/docs/zh/sql-reference/statements/select/limit-by)
- [Π ΡΡΡΠΊΠΈΠΉ](https://clickhouse.com/docs/ru/sql-reference/statements/select/limit-by)
- [νκ΅μ΄](https://clickhouse.com/docs/ko/sql-reference/statements/select/limit-by)
[Skip to main content](https://clickhouse.com/docs/sql-reference/statements/select/limit-by#__docusaurus_skipToContent_fallback)
[](https://clickhouse.com/)
- [Products](https://clickhouse.com/docs/sql-reference/statements/select/limit-by)
- [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/limit-by)
- [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/limit-by)
- [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/limit-by)
- [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=66b0bd70-8178-4af1-8668-a09558d0d04d&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Flimit-by&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Flimit-by&utm_ga=GA1.1.510657712.1775603292)
[Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=66b0bd70-8178-4af1-8668-a09558d0d04d&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Flimit-by&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Flimit-by&utm_ga=GA1.1.510657712.1775603292)
[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/limit-by)
- [English](https://clickhouse.com/docs/sql-reference/statements/select/limit-by)
- [ζ₯ζ¬θͺ](https://clickhouse.com/docs/jp/sql-reference/statements/select/limit-by)
- [δΈζ](https://clickhouse.com/docs/zh/sql-reference/statements/select/limit-by)
- [Π ΡΡΡΠΊΠΈΠΉ](https://clickhouse.com/docs/ru/sql-reference/statements/select/limit-by)
- [νκ΅μ΄](https://clickhouse.com/docs/ko/sql-reference/statements/select/limit-by)
[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)
- LIMIT BY
[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/statements/select/limit-by.md)
# LIMIT BY Clause
A query with the `LIMIT n BY expressions` clause selects the first `n` rows for each distinct value of `expressions`. The key for `LIMIT BY` can contain any number of [expressions](https://clickhouse.com/docs/sql-reference/syntax#expressions).
ClickHouse supports the following syntax variants:
- `LIMIT [offset_value, ]n BY expressions`
- `LIMIT n OFFSET offset_value BY expressions`
During query processing, ClickHouse selects data ordered by sorting key. The sorting key is set explicitly using an [ORDER BY](https://clickhouse.com/docs/sql-reference/statements/select/order-by) clause or implicitly as a property of the table engine (row order is only guaranteed when using [ORDER BY](https://clickhouse.com/docs/sql-reference/statements/select/order-by), otherwise the row blocks will not be ordered due to multi-threading). Then ClickHouse applies `LIMIT n BY expressions` and returns the first `n` rows for each distinct combination of `expressions`. If `OFFSET` is specified, then for each data block that belongs to a distinct combination of `expressions`, ClickHouse skips `offset_value` number of rows from the beginning of the block and returns a maximum of `n` rows as a result. If `offset_value` is bigger than the number of rows in the data block, ClickHouse returns zero rows from the block.
Note
`LIMIT BY` is not related to [LIMIT](https://clickhouse.com/docs/sql-reference/statements/select/limit). They can both be used in the same query.
If you want to use column numbers instead of column names in the `LIMIT BY` clause, enable the setting [enable\_positional\_arguments](https://clickhouse.com/docs/operations/settings/settings#enable_positional_arguments).
## Examples[β](https://clickhouse.com/docs/sql-reference/statements/select/limit-by#examples "Direct link to Examples")
Sample table:
```
CREATE TABLE limit_by(id Int, val Int) ENGINE = Memory;
INSERT INTO limit_by VALUES (1, 10), (1, 11), (1, 12), (2, 20), (2, 21);
```
Queries:
```
SELECT * FROM limit_by ORDER BY id, val LIMIT 2 BY id
```
```
ββidββ¬βvalββ
β 1 β 10 β
β 1 β 11 β
β 2 β 20 β
β 2 β 21 β
ββββββ΄ββββββ
```
```
SELECT * FROM limit_by ORDER BY id, val LIMIT 1, 2 BY id
```
```
ββidββ¬βvalββ
β 1 β 11 β
β 1 β 12 β
β 2 β 21 β
ββββββ΄ββββββ
```
The `SELECT * FROM limit_by ORDER BY id, val LIMIT 2 OFFSET 1 BY id` query returns the same result.
The following query returns the top 5 referrers for each `domain, device_type` pair with a maximum of 100 rows in total (`LIMIT n BY + LIMIT`).
```
SELECT
domainWithoutWWW(URL) AS domain,
domainWithoutWWW(REFERRER_URL) AS referrer,
device_type,
count() cnt
FROM hits
GROUP BY domain, referrer, device_type
ORDER BY cnt DESC
LIMIT 5 BY domain, device_type
LIMIT 100
```
## LIMIT BY ALL[β](https://clickhouse.com/docs/sql-reference/statements/select/limit-by#limit-by-all "Direct link to LIMIT BY ALL")
`LIMIT BY ALL` is equivalent to listing all the SELECT-ed expressions that are not aggregate functions.
For example:
```
SELECT col1, col2, col3 FROM table LIMIT 2 BY ALL
```
is the same as
```
SELECT col1, col2, col3 FROM table LIMIT 2 BY col1, col2, col3
```
For a special case that if there is a function having both aggregate functions and other fields as its arguments, the `LIMIT BY` keys will contain the maximum non-aggregate fields we can extract from it.
For example:
```
SELECT substring(a, 4, 2), substring(substring(a, 1, 2), 1, count(b)) FROM t LIMIT 2 BY ALL
```
is the same as
```
SELECT substring(a, 4, 2), substring(substring(a, 1, 2), 1, count(b)) FROM t LIMIT 2 BY substring(a, 4, 2), substring(a, 1, 2)
```
## Examples[β](https://clickhouse.com/docs/sql-reference/statements/select/limit-by#examples-limit-by-all "Direct link to Examples")
Sample table:
```
CREATE TABLE limit_by(id Int, val Int) ENGINE = Memory;
INSERT INTO limit_by VALUES (1, 10), (1, 11), (1, 12), (2, 20), (2, 21);
```
Queries:
```
SELECT * FROM limit_by ORDER BY id, val LIMIT 2 BY id
```
```
ββidββ¬βvalββ
β 1 β 10 β
β 1 β 11 β
β 2 β 20 β
β 2 β 21 β
ββββββ΄ββββββ
```
```
SELECT * FROM limit_by ORDER BY id, val LIMIT 1, 2 BY id
```
```
ββidββ¬βvalββ
β 1 β 11 β
β 1 β 12 β
β 2 β 21 β
ββββββ΄ββββββ
```
The `SELECT * FROM limit_by ORDER BY id, val LIMIT 2 OFFSET 1 BY id` query returns the same result.
Using `LIMIT BY ALL`:
```
SELECT id, val FROM limit_by ORDER BY id, val LIMIT 2 BY ALL
```
This is equivalent to:
```
SELECT id, val FROM limit_by ORDER BY id, val LIMIT 2 BY id, val
```
The following query returns the top 5 referrers for each `domain, device_type` pair with a maximum of 100 rows in total (`LIMIT n BY + LIMIT`).
```
SELECT
domainWithoutWWW(URL) AS domain,
domainWithoutWWW(REFERRER_URL) AS referrer,
device_type,
count() cnt
FROM hits
GROUP BY domain, referrer, device_type
ORDER BY cnt DESC
LIMIT 5 BY domain, device_type
LIMIT 100
```
[Previous JOIN](https://clickhouse.com/docs/sql-reference/statements/select/join)
[Next LIMIT](https://clickhouse.com/docs/sql-reference/statements/select/limit)
- [Examples](https://clickhouse.com/docs/sql-reference/statements/select/limit-by#examples)
- [LIMIT BY ALL](https://clickhouse.com/docs/sql-reference/statements/select/limit-by#limit-by-all)
- [Examples](https://clickhouse.com/docs/sql-reference/statements/select/limit-by#examples-limit-by-all)
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=66b0bd70-8178-4af1-8668-a09558d0d04d&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Flimit-by&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect%2Flimit-by&utm_ga=GA1.1.510657712.1775603292)
Β© 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 | A query with the `LIMIT n BY expressions` clause selects the first `n` rows for each distinct value of `expressions`. The key for `LIMIT BY` can contain any number of [expressions](https://clickhouse.com/docs/sql-reference/syntax#expressions).
ClickHouse supports the following syntax variants:
- `LIMIT [offset_value, ]n BY expressions`
- `LIMIT n OFFSET offset_value BY expressions`
During query processing, ClickHouse selects data ordered by sorting key. The sorting key is set explicitly using an [ORDER BY](https://clickhouse.com/docs/sql-reference/statements/select/order-by) clause or implicitly as a property of the table engine (row order is only guaranteed when using [ORDER BY](https://clickhouse.com/docs/sql-reference/statements/select/order-by), otherwise the row blocks will not be ordered due to multi-threading). Then ClickHouse applies `LIMIT n BY expressions` and returns the first `n` rows for each distinct combination of `expressions`. If `OFFSET` is specified, then for each data block that belongs to a distinct combination of `expressions`, ClickHouse skips `offset_value` number of rows from the beginning of the block and returns a maximum of `n` rows as a result. If `offset_value` is bigger than the number of rows in the data block, ClickHouse returns zero rows from the block.
Note
`LIMIT BY` is not related to [LIMIT](https://clickhouse.com/docs/sql-reference/statements/select/limit). They can both be used in the same query.
If you want to use column numbers instead of column names in the `LIMIT BY` clause, enable the setting [enable\_positional\_arguments](https://clickhouse.com/docs/operations/settings/settings#enable_positional_arguments).
## Examples[β](https://clickhouse.com/docs/sql-reference/statements/select/limit-by#examples "Direct link to Examples")
Sample table:
```
CREATE TABLE limit_by(id Int, val Int) ENGINE = Memory;
INSERT INTO limit_by VALUES (1, 10), (1, 11), (1, 12), (2, 20), (2, 21);
```
Queries:
```
SELECT * FROM limit_by ORDER BY id, val LIMIT 2 BY id
```
```
ββidββ¬βvalββ
β 1 β 10 β
β 1 β 11 β
β 2 β 20 β
β 2 β 21 β
ββββββ΄ββββββ
```
```
SELECT * FROM limit_by ORDER BY id, val LIMIT 1, 2 BY id
```
```
ββidββ¬βvalββ
β 1 β 11 β
β 1 β 12 β
β 2 β 21 β
ββββββ΄ββββββ
```
The `SELECT * FROM limit_by ORDER BY id, val LIMIT 2 OFFSET 1 BY id` query returns the same result.
The following query returns the top 5 referrers for each `domain, device_type` pair with a maximum of 100 rows in total (`LIMIT n BY + LIMIT`).
```
SELECT
domainWithoutWWW(URL) AS domain,
domainWithoutWWW(REFERRER_URL) AS referrer,
device_type,
count() cnt
FROM hits
GROUP BY domain, referrer, device_type
ORDER BY cnt DESC
LIMIT 5 BY domain, device_type
LIMIT 100
```
## LIMIT BY ALL[β](https://clickhouse.com/docs/sql-reference/statements/select/limit-by#limit-by-all "Direct link to LIMIT BY ALL")
`LIMIT BY ALL` is equivalent to listing all the SELECT-ed expressions that are not aggregate functions.
For example:
```
SELECT col1, col2, col3 FROM table LIMIT 2 BY ALL
```
is the same as
```
SELECT col1, col2, col3 FROM table LIMIT 2 BY col1, col2, col3
```
For a special case that if there is a function having both aggregate functions and other fields as its arguments, the `LIMIT BY` keys will contain the maximum non-aggregate fields we can extract from it.
For example:
```
SELECT substring(a, 4, 2), substring(substring(a, 1, 2), 1, count(b)) FROM t LIMIT 2 BY ALL
```
is the same as
```
SELECT substring(a, 4, 2), substring(substring(a, 1, 2), 1, count(b)) FROM t LIMIT 2 BY substring(a, 4, 2), substring(a, 1, 2)
```
## Examples[β](https://clickhouse.com/docs/sql-reference/statements/select/limit-by#examples-limit-by-all "Direct link to Examples")
Sample table:
```
CREATE TABLE limit_by(id Int, val Int) ENGINE = Memory;
INSERT INTO limit_by VALUES (1, 10), (1, 11), (1, 12), (2, 20), (2, 21);
```
Queries:
```
SELECT * FROM limit_by ORDER BY id, val LIMIT 2 BY id
```
```
ββidββ¬βvalββ
β 1 β 10 β
β 1 β 11 β
β 2 β 20 β
β 2 β 21 β
ββββββ΄ββββββ
```
```
SELECT * FROM limit_by ORDER BY id, val LIMIT 1, 2 BY id
```
```
ββidββ¬βvalββ
β 1 β 11 β
β 1 β 12 β
β 2 β 21 β
ββββββ΄ββββββ
```
The `SELECT * FROM limit_by ORDER BY id, val LIMIT 2 OFFSET 1 BY id` query returns the same result.
Using `LIMIT BY ALL`:
```
SELECT id, val FROM limit_by ORDER BY id, val LIMIT 2 BY ALL
```
This is equivalent to:
```
SELECT id, val FROM limit_by ORDER BY id, val LIMIT 2 BY id, val
```
The following query returns the top 5 referrers for each `domain, device_type` pair with a maximum of 100 rows in total (`LIMIT n BY + LIMIT`).
```
SELECT
domainWithoutWWW(URL) AS domain,
domainWithoutWWW(REFERRER_URL) AS referrer,
device_type,
count() cnt
FROM hits
GROUP BY domain, referrer, device_type
ORDER BY cnt DESC
LIMIT 5 BY domain, device_type
LIMIT 100
``` |
| Shard | 89 (laksa) |
| Root Hash | 12633450985039531489 |
| Unparsed URL | com,clickhouse!/docs/sql-reference/statements/select/limit-by s443 |