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.
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.
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:
Queries:
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
).
LIMIT BY ALL
LIMIT BY ALL
is equivalent to listing all the SELECT-ed expressions that are not aggregate functions.
For example:
is the same as
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:
is the same as
Examples
Sample table:
Queries:
The SELECT * FROM limit_by ORDER BY id, val LIMIT 2 OFFSET 1 BY id
query returns the same result.
Using LIMIT BY ALL
:
This is equivalent to:
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
).