0

Hi ,

Can we have an sql query to get the max 5 values from a column?
Please paste a sample code if its possible.

Thanks in advance.

3
Contributors
5
Replies
8
Views
9 Years
Discussion Span
Last Post by pulsar8700
0

Got the soln, as below

select * from (select a from <tbl> order by a DESC) where rownum<=5;

Thanks.

0
select * from <tab> where rownum <=5
order by <a> desc.

I dont think extra subquery is needed here.
pls correct me if m wrong.

0

Yes buddy, the extra sub querry is needed, as in below case :

select * from <tab> where rownum <=5
order by <a> desc

the first 5 rown are getting sorted and then ordered, which would not essesntaily return max 5 values.

However if we use it as :

select * from (select a from <tbl> order by a DESC) where rownum<=5;

the results are first ordered in desceding order i.e max to lowest and on selecting the frist 5 rows, we get the 5 max values.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.