How to find fifth highest salary in a single query in SQL Server

后端 未结 9 2433
暗喜
暗喜 2020-12-19 08:55

How to find fifth highest salary in a single query in SQL Server

相关标签:
9条回答
  • 2020-12-19 09:27

    These work in SQL Server 2000

    DECLARE @result int
    
    SELECT TOP 5 @result = Salary FROM Employees ORDER BY Salary DESC
    

    Syntax should be close. I can't test it at the moment.

    Or you could go with a subquery:

    SELECT MIN(Salary) FROM (
        SELECT TOP 5 Salary FROM Employees ORDER BY Salary DESC
    ) AS TopFive
    

    Again, not positive if the syntax is exactly right, but the approach works.

    0 讨论(0)
  • 2020-12-19 09:33

    You can try some thing like :

    select salary
    from Employees a
    where 5=(select count(distinct salary)
             from Employees b
             where a.salary > b.salary)
    order by salary desc
    
    0 讨论(0)
  • 2020-12-19 09:34

    The below query to gets the Highest salary after particular Employee name.

    Just have a look into that!

    SELECT TOP 1 salary FROM (
        SELECT DISTINCT min(salary) salary
        FROM emp where salary > (select salary from emp where empname = 'John Hell') 
        ) a 
    ORDER BY salary
    
    0 讨论(0)
  • 2020-12-19 09:41
    SELECT TOP 1 salary
    FROM (
        SELECT DISTINCT TOP n salary
        FROM employee
        ORDER BY salary DESC) a
    ORDER BY salary
    where n > 1 -- (n is always greater than one)
    

    You can find any number of highest salary using this query.

    0 讨论(0)
  • 2020-12-19 09:41
    SELECT MIN(Salary) FROM (
        SELECT TOP 2 Salary FROM empa ORDER BY Salary DESC
    ) AS TopFive
    

    It's working correctly, please use it.

    0 讨论(0)
  • 2020-12-19 09:42

    To find the 5th higest salary from a database, the query is..

    select MIN(esal) from (
        select top 5 esal from tbemp order by esal desc) as sal
    

    its working check it out

    0 讨论(0)
提交回复
热议问题