Sunday, February 5, 2012

How to find nth highest salary in oracle?

The following are the ways to find nth highest salary from emp table.

using correlated query
================
select * from emp a where 2=(select count(distinct sal) from emp b where a.sal <= b.sal
< div="">

using order by
===========
select * from
(
select sal,rownum rnum
from (
select sal
from emp a
where sal is not null
order by sal desc)
) x
where x.rnum=2

using connect by prior hierarchical query
=============================

select level,max(sal)
from emp
where level=2
connect by prior sal > sal
group by level

using analytic function
================
select * from ((select sal,dense_rank() over (order by sal desc) rank from emp)) where
rank=2


No comments:

Post a Comment