Friday, July 27, 2007

Queries to select max(sal) from a table in SQL server

select distinct(sal) from emp e1 where ( select count(distinct(sal)) from emp e2 where e2.sal >= e1.sal ) = 2
where 2 is the second max salary .

4th Max

select max(sal) from EMP A where 4=( select count(sal) From EMP B where B.sal>=A.sal)

5th Max

select max(sal) from EMP A where 5=( select count(sal) From EMP B where B.sal>=A.sal)

Nth Max

select max(sal) from EMP A where N=( select count(sal) From EMP B where B.sal>=A.sal)

No comments:

Post a Comment

Please post your comments here.....