1. To Find the nth highest salary
Eg: General query for all Database
SELECT DISTINCT salary FROM emp X WHERE n =
( SELECT COUNT(DISTINCT salary) FROM emp WHERE salary >=X.salary )
Replace n with the given number. for example to get the 3rd highest salary
SELECT DISTINCT salary FROM emp X WHERE 3 =
( SELECT COUNT(DISTINCT salary) FROM emp WHERE salary >=X.salary )
In Postgresql:
simly we can use offest and limit
Query:select salary from emp order by salary desc offset n limit 1;
-> Here replace the n with the given number-1. for example to get the 3rd highest salary we have to give 2.
1.To Find the nth least salary
Query:select salary from emp order by salary asc offset 2 limit 1
-> Here replace the n with the given number-1. for example to get the 3rd least salary we have to give 2.