Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: selecting top performers

Re: selecting top performers

From: amogh <amogh.r_at_gmail.com>
Date: Mon, 08 Aug 2005 19:32:25 +0530
Message-ID: <42F765F1.6050200@gmail.com>


Robert Klemme wrote:

> amogh wrote:
> 

>>baroo wrote:
>>
>>>Hi
>>>
>>>How do I select the top 20 earners from my database?
>>>
>>>urgent.
>>>
>>>cheers
>>>
>>
>>The simplest top(n) query you can write:
>>
>>SELECT a.earnings FROM
>>(SELECT earnings FROM
>> YOUR_TAB
>> ORDER BY earnings DESC
>>)a
>>WHERE ROWNUM < 21
>>
>>
>>Regards,
>>Amogh
> 
> 
> If I'm not mistaken you can even omit the query nesting and use the ROWNUM
> filter on the inner query directly.
> 
>     robert
> 

Every row is associated with ROWNUM which is a psuedo column. A WHERE clause with ROWNUM=1 will/may not fetch you the exact first row. It can be 'any' row in the table. So using ROWNUM directly in the inline query will now fetch the top 20 rows. In the above case, ROWNUM is run on a resultset which already has the ROWNUM column set for the top 20 rows(because of the ORDER BY). That would give you the exact top n rows! Received on Mon Aug 08 2005 - 09:02:25 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US