FETCH Clause
The FETCH clause specifies the number of rows to return. If no FETCH clause is specified, then SQL returns all rows.
FETCH follows this syntax. FETCH {FIRST||NEXT} {count} {ROW|ROWS} ONLY
- FIRST
Returns the count of rows beginning from the first row in the dataset.
- NEXT
Returns the count of rows that immediately follow the current row.
- count
Optional. The number of rows to return. If you don't specify a count, FETCH returns one row by default.
FETCH FIRST ROW ONLY is the same as FETCH FIRST 1 ROW ONLY.
In this example, because there’s an offset of one row, FETCH starts from the second value of the City column, ordered ascending. The query returns the next three rows, which are the second, third, and fourth rows.
1SELECT City
2FROM "Superstore"
3OFFSET 1
4ORDER BY City
5FETCH NEXT 3 ROWS ONLY;
Here, the query returns the first row.
1SELECT City
2FROM "Superstore"
3ORDER BY City
4FETCH FIRST ROW ONLY;