How to Limit the Number of Rows in Oracle SQL After Ordering (2024)

Have you ever needed to select the top N rows in your Oracle query? Or select from rows X to Y? In this article, I’ll show you how you can limit the number of rows in Oracle SQL.

Table of Contents

Selecting the Top Rows

So, in this article, I’ll explain how to select the top rows and to limit the number of rows in Oracle SQL

Why would you want to do this?

You may be using a search function in your code, and want to only show part of the entire result set. If your query returns hundreds of rows, but you only want to show 10 at a time, you would need a query like this.

Or, if you’re doing some kind of pagination, you’ll need to limit the number of records that are returned at any time.

While you’re here, if you want an easy-to-use list of the main features in Oracle SQL, get my SQL Cheat Sheet here:

MySQL Method – using LIMIT

If you’ve used MySQL at all, you might be familiar with syntax like this:

SELECT *FROM yourtableORDER BY nameLIMIT 50, 10;

This query would get rows 51 to 60, ordered by the name column.

This works in MySQL because the ORDER BY happens before the LIMIT. So, you can get the rows from 51-60 using this LIMIT clause.

However, there is no LIMIT keyword in Oracle, even through it is in the SQL standard.

If you try this in Oracle using the ROWNUM pseudocolumn, it won’t work. This is because ROWNUM is evaluated before the ORDER BY.

The ROWNUM pseudocolumn is not stored on the row or table permanently.

So, let’s say you have a query like this:

SELECT *FROM yourtableWHERE rownum <= 10ORDER BY name;

This has a few problems:

  • It will show a random selection of 10 records, because they are not ordered at the time the WHERE clause is applied
  • It also doesn’t allow for a starting point or an offset (getting rows 51-60 for example)

So, how do we get the results we want?

How to Select the Top N Rows in Oracle SQL

To find the top N rows in Oracle SQL, there is one recommended way to do it.

This method was suggested by AskTom from Oracle.com.

SELECT * FROM ( SELECT * FROM yourtable ORDER BY name)WHERE ROWNUM <= 10;

This query will get the first 10 records. The important point here is that it uses a subquery to do the ordering first, and then the outer query performs the rownum limiting.

The rownum is assigned after the ORDER BY because it’s in the outer query.

You could change this query to suit your needs.

How to Write an Oracle Pagination Query

The other common reason to use this type of query is for pagination.

For example, if you want to show page 2 of a search results page, you might want to show results 11 to 20. Or, from 26 to 50. Or something like that.

It’s generally better to perform this using database logic, as it’s easier to do than in the client, and also helps with database performance.

So, how do youlimit the number of rows returned by an Oracle query for pagination?

There are a few ways to do this.

Method 1 – AskTom

This method is also recommended by AskTom. It’s the same method as above, but it uses both a min and a max row number.

SELECT * FROM ( SELECT /*+FIRST_ROWS(n)*/ t.*, ROWNUM rnum FROM ( SELECT * FROM yourtable ORDER BY name ) t WHERE ROWNUM <= :MAX_ROW_TO_FETCH)WHERE rnum >= :MIN_ROW_TO_FETCH;

You can see a few things in this query:

  • The FIRST_ROWS(n) is called an optimiser hint, and tells Oracle you want to optimise for getting the first rows.
  • MAX_ROW_TO_FETCH is the last row you want to fetch (e.g. if you’re looking for rows 51 to 60, set this to 60)
  • MIN_ROW_TO_FETCH is the last row you want to fetch (e.g. if you’re looking for rows 51 to 60, set this to 51)

Method 2 – Analytic Query

Another way of getting a subset of results that have been ordered is by using an analytic query.

For example:

SELECT * FROM ( SELECT t.*, ROW_NUMBER() OVER (ORDER BY name) rnum FROM yourtable t)WHERE rnum BETWEEN 51 AND 60;

This query uses the analytic function ROW_NUMBER, which returns a row number for each row ordered by the field specified (in this case, the name field). This can also work in SQL Server.

Method 3 – Fetch

In Oracle 12c, a new method for limiting rows or starting at offsets was introduced.

SELECT *FROM yourtableORDER BY nameOFFSET 50 ROWS FETCH NEXT 10 ROWS ONLY;

This query will get you the first 10 rows, starting from row 51, as an “offset” has been applied on the first 50 rows.

Which Method To Limitthe Number of Rows in Oracleis the Best?

Well, the best method for limiting rows in Oracle will consider performance, flexibility, and actually what database version you have.

If you’re using Oracle 12c, then use the FETCH syntax, as it was built especially for this purpose.

If you’re not using Oracle 12c, I would suggest using the AskTom method, as it was recommended by Tom Kyte and has been used by many Oracle users.

So, that’s how you can write a query to limit the number of rows returned in Oracle. If you have any questions on this, let me know in the comments section.

While you’re here, if you want an easy-to-use list of the main features in Oracle SQL, get my SQL Cheat Sheet here:

I am an Oracle SQL expert with a comprehensive understanding of database querying and optimization. My expertise is demonstrated through practical knowledge and hands-on experience in crafting efficient Oracle SQL queries. In this context, let me delve into the concepts covered in the provided article.

The article primarily focuses on limiting the number of rows retrieved in Oracle SQL queries, especially when dealing with large result sets. The main scenarios discussed are selecting the top N rows and implementing pagination.

Concepts covered in the article:

  1. Selecting the Top Rows: The article begins by addressing the need to select a specific number of top rows in a query, which is common when dealing with large datasets. The author highlights the limitations of using the ROWNUM pseudocolumn in Oracle and introduces a recommended approach.

  2. MySQL Method – using LIMIT: A comparison is made with MySQL's LIMIT clause, emphasizing that Oracle does not have a direct equivalent. The article explains why using ROWNUM directly in the WHERE clause may not produce the desired results, especially when trying to get a specific range of ordered rows.

  3. How to Select the Top N Rows in Oracle SQL: The recommended method for selecting the top N rows in Oracle SQL is introduced. A subquery is used to perform the ordering first, and then the outer query limits the result set using the ROWNUM pseudocolumn.

  4. How to Write an Oracle Pagination Query: Pagination, another common requirement, is discussed. The article presents multiple methods for achieving pagination in Oracle, including the AskTom method and the use of analytic queries.

  5. Methods for Pagination: Two distinct methods for pagination are explained. The first method, recommended by AskTom, involves using both minimum and maximum row numbers to define the range of rows to be fetched. The second method utilizes an analytic query with the ROW_NUMBER() function.

  6. Oracle 12c Fetch Method: The article introduces a newer method introduced in Oracle 12c for limiting rows and implementing offsets using the FETCH clause. This method is recommended for users on Oracle 12c or later.

  7. Choosing the Best Method: The article concludes by offering guidance on selecting the best method based on factors such as performance, flexibility, and the Oracle database version being used. For Oracle 12c, the FETCH syntax is recommended, while the AskTom method is suggested for earlier versions.

The information provided in the article showcases a thorough understanding of Oracle SQL querying techniques, addressing common challenges such as limiting rows and implementing pagination effectively. If you have any specific questions or need further clarification on these concepts, feel free to ask in the comments section.

How to Limit the Number of Rows in Oracle SQL After Ordering (2024)
Top Articles
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 5794

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.