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;
Leave a comment