Monday 17 October 2011

Top Nth Salary

Select min (sal) from EMP a
Where &N > (select count (distinct sal) from emp b where a.sal < b.sal)
Order by sal desc;

Select deptno, min (sal) from EMP a
Where &n > (select count (distinct sal) from EMP b
Where a.sal < b.sal and a.deptno = b.deptno)
Group by deptno
Order by deptno;

Select empno,sal from (select empno,sal from emp order by sal desc) where rownum<4

Select empno,sal from EMP a
Where 3 > (select count (distinct sal) from emp b where a.sal < b.sal)
Order by sal desc

Query to return Nth highest salary for each department
-----------------------------------------------------
Select deptno, sal from emp a
Where &N > (select count (sal) from emp b where a.sal < b.sal and a.deptno = b.deptno)
Minus
Select deptno, sal From emp a
Where &(N-1) > (select count (sal) from emp b where a.sal < b.sal and a.deptno = b.deptno)
Order by deptno, sal desc;

No comments:

Post a Comment