§ Oracle Compatibility - Syntax - OFFSET ... FETCH


§ 1. Syntax

[ OFFSET offset { ROW | ROWS } ]
[ FETCH { FIRST | NEXT } [ { row_count | percent PERCENT } ]
    { ROW | ROWS } { ONLY | WITH TIES } ]
1
2
3

§ 2. Definition and Usage

The following is a description of the usage of SELECT ... OFFSET ... FETCH:

  • NEXT and FIRST are keywords with the same meaning.

  • ROW and ROWS are also keywords with the same meaning.

  • About row_count | percent PERCENT

The OFFSET clause is used to specify the number of rows to skip before the row limiting begins. The OFFSET clause is optional. If it is omitted, the offset is 0 and the row limit is calculated from the first row.

The offset must be a number or an expression (the result of evaluating the expression is a number). The offset follows these rules:

  1. If the offset is a negative value, it is treated as 0.

  2. If the offset is NULL or greater than the number of rows returned by the query, no rows are returned.

  3. If row_count contains a decimal, the fractional part is truncated.

  4. The PERCENT percentage value returns a percentage of the number of rows in the table. If the result of percent calculation contains a decimal, it is rounded up by one digit. For example: the table contains 100 rows in total; when percent=0.1, the calculation result is 1000.1/100=0.1*, which contains a decimal and is rounded up to 1.

  • About ONLY | WITH TIES
  1. ONLY: returns only the number of rows or the percentage of rows after FETCH NEXT/FIRST.

  2. WITH TIES: returns the records with the same value as the last row of ORDER BY. Note: if WITH TIES is used, an ORDER BY clause must be specified in the query. Otherwise, the query will not return additional rows.

§ 3. Oracle Compatibility Notes

  1. In Oracle, it cannot be used together with FOR UPDATE, but in GreatSQL, LIMIT OFFSET and FOR UPDATE can be used together.

  2. If the query list contains fields with the same name, aliases must be used to distinguish them.

  3. In GreatSQL, the OFFSET offset may undergo implicit conversion, and the implicit conversion results of different functions may be inconsistent.

§ 4. Examples

greatsql> CREATE TABLE t1 (a INT);
greatsql> INSERT INTO t1 VALUES (1), (1), (2), (3), (2);

greatsql> SELECT * FROM t1
OFFSET 2 ROWS;
+------+
| a    |
+------+
|    2 |
|    3 |
|    2 |
+------+
3 rows in set (0.00 sec)

greatsql> SELECT * FROM t1
FETCH FIRST ROW ONLY;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

greatsql> SELECT * FROM t1
FETCH FIRST ROWS ONLY;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

greatsql> SELECT * FROM t1
FETCH NEXT ROW ONLY;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

greatsql> SELECT * FROM t1
FETCH NEXT ROWS ONLY;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

greatsql> SELECT * FROM t1 ORDER BY a
OFFSET 2 ROW
FETCH FIRST ROW ONLY;
+------+
| a    |
+------+
|    2 |
+------+
1 row in set (0.00 sec)

greatsql> SELECT * FROM t1 ORDER BY a
OFFSET 2 ROWS
FETCH FIRST 1 ROW ONLY;
+------+
| a    |
+------+
|    2 |
+------+
1 row in set (0.00 sec)

greatsql> SELECT * FROM t1
FETCH FIRST ROW WITH TIES;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

greatsql> SELECT * FROM t1 ORDER BY a
FETCH FIRST ROW WITH TIES;
+------+
| a    |
+------+
|    1 |
|    1 |
+------+
2 rows in set (0.00 sec)

greatsql> SELECT * FROM t1 ORDER BY a
OFFSET 2 ROWS
FETCH FIRST ROW WITH TIES;
+------+
| a    |
+------+
|    2 |
|    2 |
+------+
2 rows in set (0.00 sec)

greatsql> SELECT * FROM t1
FETCH FIRST 3 ROW ONLY;
+------+
| a    |
+------+
|    1 |
|    1 |
|    2 |
+------+
3 rows in set (0.00 sec)

greatsql> SELECT * FROM t1
FETCH NEXT 1+2 ROWS ONLY;
+------+
| a    |
+------+
|    1 |
|    1 |
|    2 |
+------+
3 rows in set (0.00 sec)

greatsql> SELECT * FROM t1 ORDER BY a
OFFSET 2 ROWS FETCH FIRST 3 ROW ONLY;
+------+
| a    |
+------+
|    2 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)

greatsql> SELECT * FROM t1
OFFSET 2 ROWS FETCH NEXT 3 ROW ONLY;
+------+
| a    |
+------+
|    2 |
|    3 |
|    2 |
+------+
3 rows in set (0.00 sec)

greatsql> SELECT * FROM t1 ORDER BY a
FETCH FIRST 3 ROWS WITH TIES;
+------+
| a    |
+------+
|    1 |
|    1 |
|    2 |
|    2 |
+------+
4 rows in set (0.00 sec)

greatsql> SELECT * FROM t1 ORDER BY a
OFFSET 2 ROWS
FETCH NEXT 3 ROWS WITH TIES;
+------+
| a    |
+------+
|    2 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)

greatsql> SELECT * FROM t1
FETCH NEXT 49 PERCENT ROWS ONLY;
+------+
| a    |
+------+
|    1 |
|    1 |
|    2 |
+------+
3 rows in set (0.00 sec)

greatsql> SELECT * FROM t1
FETCH NEXT 50 PERCENT ROWS ONLY;
+------+
| a    |
+------+
|    1 |
|    1 |
|    2 |
+------+
3 rows in set (0.00 sec)

greatsql> SELECT * FROM t1
OFFSET 2 ROWS
FETCH NEXT 50 PERCENT ROWS ONLY;
+------+
| a    |
+------+
|    2 |
|    3 |
|    2 |
+------+
3 rows in set (0.00 sec)

greatsql> SELECT * FROM t1
FETCH NEXT 50 PERCENT ROWS WITH TIES;
+------+
| a    |
+------+
|    1 |
|    1 |
|    2 |
+------+
3 rows in set (0.00 sec)

greatsql> SELECT * FROM t1 ORDER BY a
OFFSET 1 ROWS
FETCH NEXT 49 PERCENT ROWS WITH TIES;
+------+
| a    |
+------+
|    1 |
|    2 |
|    2 |
+------+
3 rows in set (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

Scan the QR code to follow our WeChat official account

greatsql-wx