Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to fetch Nth highest salary from a table.

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 359
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: