Powered By Blogger

Sunday 22 April 2012

How to get SECOND HIGHEST SALARY


SQL> select ename , sal from emp a where 1 = ( select count(*) from emp b
where b.sal > a.sal);
 
ENAME             SAL
---------- ----------
JONES            2975

We can generalized this query for other highest salary.

SQL> select ename , sal from emp a where N-1 = ( select count(*) from emp b where b.sal > a.sal);

where N-1 is the any number which you want to select rank wise salary.
for example if we want to select third highest salary then N-1 becomes 2 and
if we want fourth then it becomes 3.
 

No comments: