Here are examples of Oracle SQL queries to fetch first n rows.
Oracle SQL - Fetch First n Rows Examples
You can use the FETCH FIRST
clause to get the first/top n
rows in Oracle. Below is an example:
SELECT order_no, order_date, customer_no FROM sales_orders order by order_date desc fetch first 10 rows only;
The above SQL query will fetch the latest 10 sales orders.
To fetch the same using the ROWNUM
pseudo column, check the following example:
Select * from ( SELECT order_no, order_date, customer_no FROM sales_orders order by order_date desc) where rownum <= 10;
Fetch the top n rows using the ROW_NUMBER()
analytical function:
Select * from ( SELECT order_no, order_date, customer_no, row_number() over (order by order_date desc) rn FROM sales_orders order by order_date desc) where rn <= 10;
FAQ
In Oracle SQL, the "FETCH FIRST" clause is used to limit the number of rows returned by a query result set.
In Oracle SQL, ROWNUM
is a pseudocolumn that is used to assign a unique sequential number to each row returned by a query result set.
In Oracle SQL, the ORDER BY
clause is used to sort the result set of a query in a specified order based on one or more columns.
In Oracle SQL, a "row" refers to a single record or entry in a database table.
Leave a comment
You must login or register to add a new comment.