如何在SQL Server中的单个查询中找到第五高工资

时间:2008-12-11 06:06:47

标签: sql sql-server sql-server-2005

如何在SQL Server中的单个查询中找到第五高薪

9 个答案:

答案 0 :(得分:22)

在SQL Server 2005& 2008年,创建一个排名的子选择查询,然后添加一个where子句,其中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

答案 1 :(得分:4)

这些在SQL Server 2000中起作用

DECLARE @result int

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

语法应该关闭。我现在无法测试它。

或者您可以使用子查询:

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

同样,如果语法完全正确,则不是正面的,但方法可行。

答案 2 :(得分:1)

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)

您可以使用此查询找到任意数量的最高薪水。

答案 3 :(得分:1)

要查找数据库中第5个最高薪水,查询为..

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

它的工作检查出来

答案 4 :(得分:1)

SELECT MIN(Salary) FROM (
    SELECT TOP 2 Salary FROM empa ORDER BY Salary DESC
) AS TopFive

它工作正常,请使用它。

答案 5 :(得分:0)

你可以尝试一下这样的事情:

select salary
from Employees a
where 5=(select count(distinct salary)
         from Employees b
         where a.salary > b.salary)
order by salary desc

答案 6 :(得分:0)

您可以使用此查询找到它:

select top 1 salary 
from (select top 5 salary
      from tbl_Employee
      order by salary desc) as tbl 
order by salary asc

答案 7 :(得分:0)

以下查询获取特定员工姓名后的最高薪水。

请看一下!

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

答案 8 :(得分:0)