SQL query to find Nth highest salary from a salary

2019-01-06 16:34发布

How can I find the Nth highest salary in a table containing salaries in SQL Server?

11条回答
何必那么认真
2楼-- · 2019-01-06 16:44

Very simple one query to find nth highest salary

SELECT DISTINCT(Sal) FROM emp ORDER BY Salary DESC LIMIT n,1
查看更多
可以哭但决不认输i
3楼-- · 2019-01-06 16:45
EmpID   Name    Salary
1   A   100
2   B   800
3   C   300
4   D   400
5   E   500
6   F   200
7   G   600

SELECT * FROM Employee E1
WHERE (N-1) = (
                SELECT COUNT(DISTINCT(E2.Salary))
                FROM Employee E2
                WHERE E2.Salary > E1.Salary
              )

Suppose you want to find 5th highest salary, which means there are total 4 employees who have salary greater than 5th highest employee. So for each row from the outer query check the total number of salaries which are greater than current salary. Outer query will work for 100 first and check for number of salaries greater than 100. It will be 6, do not match (5-1) = 6 where clause of outerquery. Then for 800, and check for number of salaries greater than 800, 4=0 false then work for 300 and finally there are totally 4 records in the table which are greater than 300. Therefore 4=4 will meet the where clause and will return 3 C 300.

查看更多
你好瞎i
4楼-- · 2019-01-06 16:53

You can use a Common Table Expression (CTE) to derive the answer.

Let's say you have the following salaries in the table Salaries:

 EmployeeID  Salary
--------------------
     10101   50,000
     90140   35,000
     90151   72,000
     18010   39,000
     92389   80,000

We will use:

DECLARE @N int
SET @N = 3  -- Change the value here to pick a different salary rank

SELECT Salary
FROM (
    SELECT row_number() OVER (ORDER BY Salary DESC) as SalaryRank, Salary
    FROM Salaries
) as SalaryCTE
WHERE SalaryRank = @N

This will create a row number for each row after it has been sorted by the Salary in descending order, then retrieve the third row (which contains the third-highest record).


For those of you who don't want a CTE (or are stuck in SQL 2000):

[Note: this performs noticably worse than the above example; running them side-by-side with an exceution plans shows a query cost of 36% for the CTE and 64% for the subquery]:

SELECT TOP 1 Salary
FROM 
(
    SELECT TOP N Salary
    FROM Salaries
    ORDER BY Salary DESC
) SalarySubquery
ORDER BY Salary ASC

where N is defined by you.

SalarySubquery is the alias I have given to the subquery, or the query that is in parentheses.

What the subquery does is it selects the top N salaries (we'll say 3 in this case), and orders them by the greatest salary.

If we want to see the third-highest salary, the subquery would return:

 Salary
-----------
80,000
72,000
50,000

The outer query then selects the first salary from the subquery, except we're sorting it ascending this time, which sorts from smallest to largest, so 50,000 would be the first record sorted ascending.

As you can see, 50,000 is indeed the third-highest salary in the example.

查看更多
贪生不怕死
5楼-- · 2019-01-06 16:53

The easiest method is to get 2nd higest salary from table in SQL:

sql> select max(sal) from emp where sal not in (select max(sal) from emp);
查看更多
放荡不羁爱自由
6楼-- · 2019-01-06 16:54
SELECT * FROM 
(select distinct postalcode  from Customers order by postalcode DESC)
limit 4,1;

4 here means leave first 4 and show the next 1.

Try this it works for me.

查看更多
Deceive 欺骗
7楼-- · 2019-01-06 16:55
select MIN(salary) from (
select top 5 salary from employees order by salary desc) x
查看更多
登录 后发表回答