Home » SQL & PL/SQL » SQL & PL/SQL » select top 20
select top 20 [message #10714] Wed, 11 February 2004 14:33 Go to next message
Erin
Messages: 17
Registered: October 2001
Junior Member
How do you select top 20 records, based on value of a field?

so basically: select order_id, amount from orders

Want the top 20 orders based on highest amount.

Tried using order by amount desc and rownum < 21 but that didn't work.

Really only want the top 20 so i can automate a script for the output.

Thanks

 

 

 
Re: select top 20 [message #10716 is a reply to message #10714] Wed, 11 February 2004 15:07 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
The ORDER BY needs to be applied prior to the ROWNUM evaluation, so just move the ORDER BY into an inline view:

select *
  from (select order_id, amount 
          from orders 
         order by amount desc)
 where rownum <= 20;
Re: select top 20 [message #10718 is a reply to message #10714] Wed, 11 February 2004 16:03 Go to previous message
Erin
Messages: 17
Registered: October 2001
Junior Member
thanks! that worked.
Previous Topic: Mutation error
Next Topic: Generating xsd based on query results
Goto Forum:
  


Current Time: Tue Apr 23 16:38:40 CDT 2024