@@ -7,40 +7,121 @@ description: LIMIT SQL keyword reference documentation.
77Specify the number and position of records returned by a
88[ SELECT statement] ( /docs/reference/sql/select/ ) .
99
10- In other implementations of SQL, this is sometimes replaced by statements such
11- as ` OFFSET ` or ` ROWNUM ` Our implementation of ` LIMIT ` encompasses both in one
12- statement.
10+ Other implementations of SQL sometimes use clauses such as ` OFFSET ` or ` ROWNUM ` .
11+ Our implementation uses ` LIMIT ` for both the offset from start and limit.
1312
1413## Syntax
1514
1615![ Flow chart showing the syntax of the LIMIT keyword] ( /images/docs/diagrams/limit.svg )
1716
1817- ` numberOfRecords ` is the number of records to return.
19- - ` upperBound ` and ` lowerBound ` is the return range. ` lowerBound ` is
20- ** exclusive** and ` upperBound ` is ** inclusive** .
18+ - ` upperBound ` and ` lowerBound ` is the range of records to return.
2119
22- A ` positive ` number will return the ` first ` ` n ` records. A ` negative ` number
23- will return the ` last ` ` n ` records.
20+ Here's the exhaustive list of supported combinations of arguments. ` m ` and ` n `
21+ are positive numbers, and negative numbers are explicitly labeled ` -m ` and ` -n ` .
22+
23+ - ` LIMIT n ` : take the first ` n ` records
24+ - ` LIMIT -n ` : take the last ` n ` records
25+ - ` LIMIT m, n ` : skip the first ` m ` records, then take up to record number ` n `
26+ (inclusive)
27+ - result is the range of records ` (m, n] ` number 1 denoting the first record
28+ - if ` m > n ` , implicitly swap the arguments
29+ - PostgreSQL equivalent: ` OFFSET m LIMIT (n-m) `
30+ - ` LIMIT -m, -n ` : take the last ` m ` records, then drop the last ` n ` records from
31+ that
32+ - result is the range of records ` [-m, -n) ` , number -1 denoting the last
33+ record
34+ - if ` m < n ` , implicitly swap them
35+ - ` LIMIT m, -n ` : drop the first ` m ` and the last ` n ` records. This gives you the
36+ range ` (m, -n) ` . These arguments will not be swapped.
37+
38+ These are additional edge-case variants:
39+
40+ - ` LIMIT n, 0 ` = ` LIMIT 0, n ` = ` LIMIT n, ` = ` LIMIT , n ` = ` LIMIT n `
41+ - ` LIMIT -n, 0 ` = ` LIMIT -n, ` = ` LIMIT -n `
2442
2543## Examples
2644
27- ``` questdb-sql title="First 5 results"
28- SELECT * FROM ratings LIMIT 5;
45+ Examples use this schema and dataset:
46+
47+ ``` questdb-sql
48+ CREATE TABLE tango (id LONG);
49+ INSERT INTO tango VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
50+ ```
51+
52+ ``` questdb-sql title="First 5 records"
53+ SELECT * FROM tango LIMIT 5;
54+
55+ id
56+ ----
57+ 1
58+ 2
59+ 3
60+ 4
61+ 5
2962```
3063
31- ``` questdb-sql title="Last 5 results"
32- SELECT * FROM ratings LIMIT -5;
64+ ``` questdb-sql title="Last 5 records"
65+ SELECT * FROM tango LIMIT -5;
66+
67+
68+ id
69+ ----
70+ 6
71+ 7
72+ 8
73+ 9
74+ 10
3375```
3476
35- ``` questdb-sql title="Range results - this will return records 3, 4 and 5"
36- SELECT * FROM ratings LIMIT 2,5;
77+ ``` questdb-sql title="Records 3, 4, and 5"
78+ SELECT * FROM tango LIMIT 2, 5;
79+
80+ id
81+ ----
82+ 3
83+ 4
84+ 5
85+ ```
86+
87+ ``` questdb-sql title="Records -5 and -4"
88+ SELECT * FROM tango LIMIT -5, -3;
89+
90+ id
91+ ----
92+ 6
93+ 7
94+ ```
95+
96+ ``` questdb-sql title="Records 3, 4, ..., -3, -2"
97+ SELECT * FROM tango LIMIT 2, -1;
98+
99+ id
100+ ----
101+ 3
102+ 4
103+ 5
104+ 6
105+ 7
106+ 8
107+ 9
108+ ```
109+
110+ ``` questdb-sql title="Implicit argument swap, records 3, 4, 5"
111+ SELECT * FROM tango LIMIT 5, 2;
112+
113+ id
114+ ----
115+ 3
116+ 4
117+ 5
37118```
38119
39- ` negative ` range parameters will return results from the bottom of the table.
40- Assuming a table with ` n ` records, the following will return records between
41- ` n-7 ` (exclusive) and ` n-3 ` (inclusive), i.e ` {n-6, n-5, n-4, n-3} ` . Both
42- ` upperBound ` and ` lowerBound ` must be negative numbers, in this case:
120+ ``` questdb-sql title="Implicit argument swap, records -5 and -4"
121+ SELECT * FROM tango LIMIT -3, -5;
43122
44- ``` questdb-sql title="Range results (negative)"
45- SELECT * FROM ratings LIMIT -7, -3;
123+ id
124+ ----
125+ 6
126+ 7
46127```
0 commit comments