There are two ways to fetch Nth highest salary/number.
1 - By using general sql query which works on all database.
2 - By using database specific sql query.
Database specific sql query example Nth highest salary/number
SQL Queries in Oracle to fetch Nth highest salary/number.
select min(salary) from (select * from (select * from
order by SALARY desc) where rownum < N + 1)
SQL Queries in SQL Server to fetch Nth highest salary/number.
select min(SALARY) from (select top N * from
) AS derivedTable
SQL Queries in MySQL to fetch Nth highest salary/number.
select min(SALARY) from (select * from
order by salary desc limit N) AS derivedTable
General sql query to fetch Nth highest salary/number.
SELECT DISTINCT salary FROM emp X WHERE n =
( SELECT COUNT(DISTINCT salary) FROM
WHERE salary >=X.salary )
Note --
Replace n with the required number to fetch Nth highest salary. for example to get the 3rd highest salary replace n= 3.
0 Comment(s)