Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 355259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:00:04+00:00 2026-05-12T12:00:04+00:00

I have an oracle database populated with million records. I am trying to write

  • 0

I have an oracle database populated with million records. I am trying to write a SQL query that returns the first ‘N” sorted records ( say 100 records) from the database based on certain condition.

SELECT * 
FROM myTable 
Where SIZE > 2000 
ORDER BY NAME DESC

Then programmatically select first N records.

The problem with this approach is :

  • The query results into half million
    records and “ORDER BY NAME” causes
    all the records to be sorted on NAME in the descending order. This sorting is taking lot of time. (nearly 30-40 seconds. If I omit ORDER BY, it takes only 1 second).
  • After the sort I am interested in
    only first N (100) records. So the sorting of complete records is not useful.

My questions are:

  1. Is it possible to specify the ‘N’ in
    query itself? ( so that sort applies to only N records and query becomes faster).
  2. Any better way in SQL to improve the query to sort
    only N elements and return in quick
    time.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:00:05+00:00Added an answer on May 12, 2026 at 12:00 pm

    If your purpose is to find 100 random rows and sort them afterwards then Lasse’s solution is correct. If as I think you want the first 100 rows sorted by name while discarding the others you would build a query like this:

    SELECT * 
      FROM (SELECT * 
              FROM myTable 
             WHERE SIZE > 2000 ORDER BY NAME DESC) 
     WHERE ROWNUM <= 100
    

    The optimizer will understand that it is a TOP-N query and will be able to use an index on NAME. It won’t have to sort the entire result set, it will just start at the end of the index and read it backwards and stop after 100 rows.

    You could also add an hint to your original query to let the optimizer understand that you are interested in the first rows only. This will probably generate a similar access path:

    SELECT /*+ FIRST_ROWS*/* FROM myTable WHERE SIZE > 2000 ORDER BY NAME DESC
    

    Edit: just adding AND rownum <= 100 to the query won’t work since in Oracle rownum is attributed before sorting : this is why you have to use a subquery. Without the subquery Oracle will select 100 random rows then sort them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Oracle database column, say col1, that has the following values: Col1
I have an Oracle database that runs a PL/SQl job once every week to
We have an Oracle database here that's been around for about 10 years. It's
I have 3 Oracle database instances with the same data and scheme that I
I have an Oracle database that I am connecting to with PHP OCI8. One
I have an oracle 10g database that has 2 tables: a REBATES table, and
I have an Oracle 10g database of genomic data with several >100 million row
Let's say I have an Oracle database table of 40MM 9-digit ZIP codes which
I have a java.sql.Clob object which I populate from an Oracle query and then
I have an Oracle database (roughly 1.2 billion records) of data with a web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.