In some DBMS packages, the top command may not work. Then how to do it? Suppose we need to find the 3rd largest salary in the employee table. Therefore, we select the reported salary from the table in descending order:
select distinct salary from employee order by salary desc
Now, among the selected salaries, we need 3 salaries, for which we write:
select salary from (select distinct salary from employee order by salary desc) where rownum<=3 order by salary
This gives the top 3 salaries in ascending order. This makes the third largest salary in the first position. Now we have the last task of printing the 3rd largest number.
select salary from (select salary from (select distinct salary from employee order by salary desc) where rownum<=3 order by salary) where rownum=1
This gives the third largest number. For any error in the request, please let me know. Basically, to get the nth largest number, we can rewrite the above query as
select salary from (select salary from (select distinct salary from employee order by salary desc) where rownum<=**n** order by salary) where rownum=1
source share