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

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

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

相关标签:
9条回答
  • 2020-12-19 09:42
    select * from employee2 e
    where 2=(select count(distinct salary) from employee2
             where e.salary<=salary)
    

    its working

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

    You can find it by using this query:

    select top 1 salary 
    from (select top 5 salary
          from tbl_Employee
          order by salary desc) as tbl 
    order by salary asc
    
    0 讨论(0)
  • 2020-12-19 09:53

    In SQL Server 2005 & 2008, create a ranked subselect query, then add a where clause where the rank = 5.

    select
      *
    from
    (
      Select
        SalesOrderID, CustomerID, Row_Number() Over (Order By SalesOrderID) as RunningCount
      From
        Sales.SalesOrderHeader
      Where
        SalesOrderID > 10000
      Order By
        SalesOrderID 
    ) ranked
    where 
      RunningCount = 5
    
    0 讨论(0)
提交回复
热议问题