How do I limit the number of rows returned by an Oracle query after ordering?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When working with Oracle databases, it's common to face scenarios where you need to retrieve only a subset of rows from your data set, especially after sorting the data in a specific order. Limiting the number of rows returned by a query can be crucial for performance, especially when dealing with large volumes of data. There are several strategies in Oracle SQL to achieve this, primarily using the ROWNUM pseudocolumn, the FETCH clause, and subqueries. Each method has its advantages and appropriate use cases.
Using ROWNUM to Limit Rows
In Oracle, ROWNUM is a pseudocolumn that assigns a unique, sequential number to each row as it is retrieved from the database. You can use ROWNUM to limit the number of rows returned. However, because ROWNUM is applied before sorting (ORDER BY), you must use a subquery if you want to sort the results first and then apply the row limit.
In this example, the inner query sorts the table by column1, and then the outer query limits the results to the first 10 rows.
Using FETCH FIRST/NEXT
Oracle 12c introduced the FETCH FIRST clause, providing a more straightforward and SQL-standard way to limit the results of a query. This method is particularly readable and avoids the need for subqueries used with ROWNUM.
You can also use FETCH NEXT which is functionally equivalent to FETCH FIRST:
Using the OFFSET Clause
Sometimes, in addition to limiting the number of rows, you might want to skip a certain amount of rows before starting to return the rows (similar to SQL pagination). This can be done using the OFFSET clause along with FETCH.
This query will skip the first 5 rows and then fetch the next 5 rows from the result set.
Comparing Methods
| Method | Description | Use Case |
| ROWNUM | Uses a pseudocolumn to limit rows, requires a subquery for ordered data. | Legacy systems, or where FETCH is not available. |
| FETCH FIRST/NEXT | SQL standard way to limit rows, very readable, part of the SQL order operation. | Most use cases in Oracle 12c and later. |
| OFFSET with FETCH | Skips a certain number of rows before beginning to return rows in conjunction. | Pagination, especially in web applications. |
Performance Considerations
It's vital to consider performance when limiting query results. Using ROWNUM on an ordered subquery might be less efficient than using ORDER BY with FETCH, because the database might be able to optimize the retrieval strategy better with newer clauses like FETCH. Always check the execution plan for your queries to ensure optimal performance.
Conclusion
Oracle provides various methods for limiting the number of rows returned by a query, and choosing the right method depends on the specific requirements of your application and the version of Oracle you are using. Modern approaches like FETCH are generally recommended for their readability and alignment with SQL standards, but in some environments, using ROWNUM might still be necessary.
Understanding these techniques and their trade-offs empowers you to write more efficient and effective SQL queries, enhancing both performance and maintainability of your database operations.
Related reading
- How do I list all the columns in a table?
- How do I modify a MySQL column to allow NULL?
- How do I obtain a list of all schemas in a Sql Server database
- How do I put an 'if clause' in an SQL string?
- How do I make the method return type generic?
- How do I make the return type of a method generic?
- How do I query between two dates using MySQL?
- How do I query by only part of a composite key in DynamoDB?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.