§ Oracle Compatibility - Syntax - OFFSET ... FETCH
§ 1. Syntax
[ OFFSET offset { ROW | ROWS } ]
[ FETCH { FIRST | NEXT } [ { row_count | percent PERCENT } ]
{ ROW | ROWS } { ONLY | WITH TIES } ]
2
3
§ 2. Definition and Usage
The following is a description of the usage of SELECT ... OFFSET ... FETCH:
NEXTandFIRSTare keywords with the same meaning.ROWandROWSare 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:
If the offset is a negative value, it is treated as
0.If the offset is
NULLor greater than the number of rows returned by the query, no rows are returned.If
row_countcontains a decimal, the fractional part is truncated.The
PERCENTpercentage value returns a percentage of the number of rows in the table. If the result ofpercentcalculation 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
ONLY: returns only the number of rows or the percentage of rows afterFETCH NEXT/FIRST.WITH TIES: returns the records with the same value as the last row ofORDER BY. Note: ifWITH TIESis used, anORDER BYclause must be specified in the query. Otherwise, the query will not return additional rows.
§ 3. Oracle Compatibility Notes
In Oracle, it cannot be used together with
FOR UPDATE, but in GreatSQL,LIMIT OFFSETandFOR UPDATEcan be used together.If the query list contains fields with the same name, aliases must be used to distinguish them.
In GreatSQL, the
OFFSEToffset 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)
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
