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