OFFSET, FETCH and LIMIT¶
The OFFSET, FETCH and LIMIT clauses limit the number of rows
obtained when executing a query.
Use OFFSET <number> [ ROW | ROWS ] to skip the first n rows of the
result set.
Use LIMIT [ <count> ] or
FETCH { FIRST | NEXT } [ <count> ] { ROW | ROWS } ONLY to obtain
only <count> rows of the result set.
The parameters ROW and ROWS have the same meaning and can be
used indistinctly. FIRST and NEXT can also be used indistinctly.
For consistent results, the query must ensure a deterministic sort order.
You can use OFFSET combined with LIMIT or FETCH (see the
syntax of these clauses in the Syntax of the SELECT statement).
Examples
Example 1
SELECT f1, f2
FROM employee
FETCH FIRST 10 ROWS ONLY
Executes the query and returns the first ten rows of the result set.
Example 2
SELECT f1, f2
FROM employee
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY
Executes the query and returns the rows number 10 to number 19 (both included). The first row is row number 0.
The following query uses LIMIT and is equivalent to the previous
one:
SELECT f1, f2
FROM employee
OFFSET 10 ROWS
LIMIT 10
Example 3
If you use FETCH without <count>, the Server only returns one
row.
For example, the following query only returns the first row of the result set:
SELECT f1, f2
FROM employee
FETCH NEXT ROW ONLY
Delegation of ORDER BY/FETCH/OFFSET in Subqueries¶
In order to delegate this structures to the source the following requisites should be met:
The corresponding DELEGATEOFFSET/DELEGATEFETCH/DELEGATEORDERBY property should be enabled.
The corresponding DELEGATEOFFSETINSUBQUERIES/DELEGATEFETCHINSUBQUERIES/DELEGATEORDERBYINSUBQUERIES property should be enabled.
The subquery should contain at least an ORDER BY and a LIMIT clauses.
Note
Ultimately, full support depends on database-specific limitations, meaning certain clause and situational combinations can’t be pushed down.