OFFSET and FETCH clauses
- Last Updated: October 17, 2015
- 3 minute read
- OpenEdge
- Version 13.0
- Documentation
OFFSET and FETCH clauses
The OFFSET clause specifies the number of
rows to skip, before starting to return rows from the query expression. The FETCH clause specifies the number of rows to return, after
processing the OFFSET clause. The OFFSET and FETCH clauses are now supported in
subqueries.
Syntax
|
ROW and ROWS interchangeably.
Similarly, FIRST and NEXT can
be used interchangeably.Parameters
- expr
-
Expression of one or more columns of the tables specified in the
FROMclause of theSELECTstatement. - posn
-
Integer column position of the columns selected by the
SELECTstatement. - ASC | DESC
-
Indicates whether to order by ascending order
(ASC)or descending order. The default isASC. - offset_value
-
Specifies the number of rows to skip, before starting to return the result rows of the query. This value must be a constant: literal or parameter. It does not support expressions or self-contained subqueries. The value must not be negative and the maximum number allowed is 9,223,372,036,854,775,807.
- fetch_value
-
Specifies the number of rows to return, after processing the
OFFSETclause. This value must be a constant: literal or parameter. It does not support expressions or self-contained subqueries. The value must not be negative and the maximum number allowed is 9,223,372,036,854,775,807.
Notes
- Both
the clauses are optional. However, if present, the
OFFSETclause must come before theFETCHclause. - If the
OFFSETclause is specified without theFETCHclause, the SQL engine skips the specified number of rows and returns all other qualifying rows without an upper limit. - If the
FETCHclause is specified without theOFFSETclause, the SQL engine does not skip any rows and the initial value of theOFFSETis 0. This is similar to theTOPclause. - The
OFFSETandFETCHclauses need not be preceded by theORDER BYclause. it is a good idea to use anORDER BYclause that constrains the result rows into a unique order. If theORDER BYclause is specified, theOFFSETandFETCHclauses must appear right after theORDER BYclause of a query. - The
TOPclause cannot be combined with theOFFSETandFETCHclauses in the same query expression (in the same query scope). - The
TOPandOFFSET/FETCHclauses are mutually exclusive. - The
OFFSETandFETCHclauses cannot be used in the following instances:- As part of a subquery
- Within the derived tables
- Within the
CREATE TABLE,CREATE VIEW,UPDATE,DELETE,andINSERTstatements. - In queries used with set operators such as
UNION,INTERSECT, andMINUS - In views
- The
OFFSETandFETCHclauses are interpreted as being applied last, after all the other clauses specified in the query are applied.
Example
OFFSET clause in the SELECT statement
The following example demonstrates
the OFFSET clause in the SELECT statement:
|
The above query skips the first 10 rows and returns the rest of the qualified rows.
FETCH clause in the SELECT statement
The following example demonstrates
the FETCH clause in the SELECT statement:
|
The above query returns the first 10 rows without skipping any
OFFSET and FETCH clause in the SELECT statement
The
following example demonstrates both the OFFSET and FETCH clauses
in the SELECT statement:
|
The above query returns rows 51 through 60 in the result set of the query.
In the following example, the SELECT statement finds find employee details whose salary is less than 5th highest salary:
|