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

  • SEARCH
  • Home
  • 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 8939299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:47:42+00:00 2026-06-15T10:47:42+00:00

I am writing a query to give the next 5 smallest items greater than

  • 0

I am writing a query to give the next 5 smallest items greater than a value, which is the primary key (e.g. 101). Id is char(19) in this case and Id is the clustered index.

My attempt at the query is as follows:

SELECT *
(SELECT *
(SELECT * FROM 
PERSON
ORDER BY ID)
WHERE ID >=  '101')
WHERE rownum <= 5

I want to guarantee that the 5 results returned are the 5 biggest ids in table greater than 101, rather than the 5 biggest results that are encountered first. Hence I added an Order by into the query. I know the rownum is assigned after the where clause but before the order by is executed.

Is my query/logic correct? Do I require the order by clause to be there?

  • 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-06-15T10:47:43+00:00Added an answer on June 15, 2026 at 10:47 am

    You need the order by but descending if you want the 5 biggest. You can also merge the two inner queries:

    Select
      *
    From (
      Select
        * 
      From
        Person
      Where 
        ID >= '101'
      Order By
        ID Desc
      ) a
    Where
      rownum <= 5
    

    If you actually meant the 5 smallest IDs bigger than 100 then stick with ascending.

    Doing greater than on a character field containing numbers, may not be doing what you expect, it will be doing an alphabetic order rather than a numeric order. So 201 will be bigger than 1000000.

    Example of weird ordering

    If you want numeric comparison then change the where filter to be To_Number(ID) >= 101 (or better still change the column to be a numeric type)

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

Sidebar

Related Questions

Can someone please help me in writing the exact query, which can give me
I'm writing this query and I'm running into a mental road block. I keep
I am writing a query which needs to return all child entries (and their
I am writing a script which will list 25 items of all 12 categories.
I have some troubles re-writing this query to using joins instead of the two
I am having trouble writing query so that I can query the content of
When writing a query for paging on a web page what is the least
I am writing the query to count the number of distinct values in the
I'm writing a query and as I go along I'm analysing the SQL Profiler
I'm writing a query to get ALL of the products in the products table,

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.