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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/358542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:30:47  来源:igfitidea点击:

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

sqlsql-serversql-server-2005

提问by Yogini

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

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

回答by Jayden

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

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

回答by recursive

These work in SQL Server 2000

这些在 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.

同样,如果语法完全正确,则不是肯定的,但该方法有效。

回答by Ritesh Kumar

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

要从数据库中找到第 5 高的薪水,查询是..

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

its working check it out

它的工作检查出来

回答by user683393

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

It's working correctly, please use it.

它工作正常,请使用它。

回答by user683393

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.

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

回答by Yagnesh84

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

回答by Bell

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

回答by Nilesh

select * from employee2 e
where 2=(select count(distinct salary) from employee2
         where e.salary<=salary)

its working

它的工作

回答by Nilesh

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