2020-05-15 11:25:18 +00:00
---
toc_title: LIMIT
---
2020-05-15 04:34:54 +00:00
# LIMIT Clause {#limit-clause}
`LIMIT m` allows to select the first `m` rows from the result.
`LIMIT n, m` allows to select the `m` rows from the result after skipping the first `n` rows. The `LIMIT m OFFSET n` syntax is equivalent.
`n` and `m` must be non-negative integers.
2020-06-18 08:24:31 +00:00
If there is no [ORDER BY ](../../../sql-reference/statements/select/order-by.md ) clause that explicitly sorts results, the choice of rows for the result may be arbitrary and non-deterministic.
2020-07-13 21:52:40 +00:00
2021-04-23 06:52:17 +00:00
!!! note "Note"
The number of rows in the result set can also depend on the [limit ](../../../operations/settings/settings.md#limit ) setting.
2021-04-22 18:27:06 +00:00
2020-07-14 21:02:41 +00:00
## LIMIT … WITH TIES Modifier {#limit-with-ties}
2020-07-13 21:52:40 +00:00
When you set `WITH TIES` modifier for `LIMIT n[,m]` and specify `ORDER BY expr_list` , you will get in result first `n` or `n,m` rows and all rows with same `ORDER BY` fields values equal to row at position `n` for `LIMIT n` and `m` for `LIMIT n,m` .
2020-07-14 21:02:41 +00:00
This modifier also can be combined with [ORDER BY … WITH FILL modifier ](../../../sql-reference/statements/select/order-by.md#orderby-with-fill ).
2020-07-13 21:52:40 +00:00
For example, the following query
2020-07-14 21:02:41 +00:00
``` sql
2020-07-13 21:52:40 +00:00
SELECT * FROM (
SELECT number%50 AS n FROM numbers(100)
) ORDER BY n LIMIT 0,5
```
2020-07-14 21:02:41 +00:00
returns
``` text
2020-07-13 21:52:40 +00:00
┌─n─┐
│ 0 │
│ 0 │
│ 1 │
│ 1 │
│ 2 │
└───┘
```
but after apply `WITH TIES` modifier
2020-07-14 21:02:41 +00:00
``` sql
2020-07-13 21:52:40 +00:00
SELECT * FROM (
SELECT number%50 AS n FROM numbers(100)
) ORDER BY n LIMIT 0,5 WITH TIES
```
it returns another rows set
2020-07-14 21:02:41 +00:00
``` text
2020-07-13 21:52:40 +00:00
┌─n─┐
│ 0 │
│ 0 │
│ 1 │
│ 1 │
│ 2 │
│ 2 │
└───┘
```
2020-07-14 21:02:41 +00:00
cause row number 6 have same value “2” for field `n` as row number 5