hi please find the exact solution for the nth highest salary.
METHOD 1:
select empname salary from emp where sal ( select min(sal) from (select distinct sal from emp order by sal desc) where rownum< n)
n---> nth salary
METHOD 2:
SELECT ename sal from emp a where (n-1) (select count(*) from (select distinct(sal) from emp) b where b.sal>a.sal)
for eg to calculate 5th highest salary. substitute 4 in where clause(ie 5-1)
Explanation of the query:
For every row in emp. Count the number of salaries greater than that row. when you find a row with only 3 greater salaries you must have the 4th highest right there.
NOTE: please dont write statements like 'where rownum n'. It wont work at all. because rownum will be generated at runtime. so rownum< 5 is correct and rownum 5 is wrong(means it wont give any error but will not return any rows) You can write only rownum 1 but not 2 3 etc..