Summary: in this tutorial, you will learn how to use the Oracle FETCH clause to limit the rows returned by a query.
Introduction to Oracle FETCH clause
Some RDBMS such as MySQL and PostgreSQL have the LIMIT clause that allows you to retrieve a portion of rows generated by a query.
See the following products and inventories tables in the sample database.
The following query uses the LIMIT clause to get the top 5 products with the highest inventory level:
In this example, the ORDER BY clause sorts the products by stock quantity in descending order and the LIMIT clause returns only the first 5 products with the highest stock quantity.
Oracle Database does not have the LIMIT clause. However, since 12c release, it provided a similar but more flexible clause named row limiting clause.
By using the row limiting clause, you can rewrite the query that uses the LIMIT clause above as follows:
In this statement, the row limiting clause is:
Similar to the statement that uses LIMIT clause above, the row limiting clause returns the top 5 products with the highest inventory level.
Oracle FETCH clause syntax
The following illustrates the syntax of the row limiting clause:
OFFSET clause
The OFFSET clause specifies the number of rows to skip before the row limiting starts. The OFFSET clause is optional. If you skip it, then offset is 0 and row limiting starts with the first row.
The offset must be a number or an expression that evaluates to a number. The offset is subjected to the following rules:
- If the offset is negative, then it is treated as 0.
- If the offset is NULL or greater than the number of rows returned by the query, then no row is returned.
- If the offset includes a fraction, then the fractional portion is truncated.
FETCH clause
The FETCH clause specifies the number of rows or percentage of rows to return.
For the semantic clarity purpose, you can use the keyword ROWinstead ofROWS, FIRSTinstead ofNEXT. For example, the following clauses behavior the same:
ONLY | WITH TIES
TheONLY returns exactly the number of rows or percentage of rows after FETCH NEXT [or FIRST].
TheWITH TIESreturns additional rows with the same sort key as the last row fetched. Note that if you use WITH TIES, you must specify an ORDER BY clause in the query. If you dont, the query will not return the additional rows.
Oracle FETCH clause examples
A] Top N rows example
The following statement returns the top 10 products with the highest inventory level:
B] WITH TIES example
The following query uses the row limiting clause with the WITH TIES option:
Even though the query requested 10 rows, because it had the WITH TIES option, the query returned two more additional rows. Notice that these two additional rows have the same value inthe quantity column as the row 10.
C] Limit by percentage of rows example
The following query returns top 5% products with the highest inventory level:
The inventories table has 1112 rows, therefore, 5% of 1112 is 55.6 which is rounded up to 56 [rows].
D] OFFSET example
The following query skips the first 10 products with the highest level of inventory and returns the next 10 ones:
In this tutorial, you have learned how to use the Oracle FETCH clause to limit rows returned by a query.