Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get second-highest salary employees in a table

It's a question I got this afternoon:

There a table contains ID, Name, and Salary of Employees, get names of the second-highest salary employees, in SQL Server

Here's my answer, I just wrote it in paper and not sure that it's perfectly valid, but it seems to work:

SELECT Name FROM Employees WHERE Salary =  ( SELECT DISTINCT TOP (1) Salary FROM Employees WHERE Salary NOT IN  (SELECT DISTINCT TOP (1) Salary FROM Employees ORDER BY Salary DESCENDING) ORDER BY Salary DESCENDING) 

I think it's ugly, but it's the only solution come to my mind.

Can you suggest me a better query?

Thank you very much.

like image 566
Quan Mai Avatar asked Sep 14 '11 13:09

Quan Mai


People also ask

How do you retrieve 2nd 3rd or nth highest salary from employee table?

Here is a way to do this task using dense_rank() function. Query : select * from( select ename, sal, dense_rank() over(order by sal desc)r from Employee) where r=&n; To find to the 2nd highest sal set n = 2 To find 3rd highest sal set n = 3 and so on.

How do I get the second highest value in SQL?

SELECT MAX (column_name) FROM table_name WHERE column_name NOT IN (SELECT Max (column_name) FROM table_name); First we selected the max from that column in the table then we searched for the max value again in that column with excluding the max value which has already been found, so it results in the 2nd maximum value.

How can I get maximum salary from employee table?

Try using this SQL SELECT statement: SELECT * FROM employees WHERE department_id=30 AND salary = (SELECT MAX(salary) FROM employees WHERE department_id=30); This will return the employee information for only the employee in department 30 that has the highest salary.


2 Answers

To get the names of the employees with the 2nd highest distinct salary amount you can use.

;WITH T AS ( SELECT *,        DENSE_RANK() OVER (ORDER BY Salary Desc) AS Rnk FROM Employees ) SELECT Name FROM T WHERE Rnk=2; 

If Salary is indexed the following may well be more efficient though especially if there are many employees.

SELECT Name FROM   Employees WHERE  Salary = (SELECT MIN(Salary)                  FROM   (SELECT DISTINCT TOP (2) Salary                          FROM   Employees                          ORDER  BY Salary DESC) T); 

Test Script

CREATE TABLE Employees   (      Name   VARCHAR(50),      Salary FLOAT   )  INSERT INTO Employees SELECT TOP 1000000 s1.name,                    abs(checksum(newid())) FROM   sysobjects s1,        sysobjects s2  CREATE NONCLUSTERED INDEX ix   ON Employees(Salary)  SELECT Name FROM   Employees WHERE  Salary = (SELECT MIN(Salary)                  FROM   (SELECT DISTINCT TOP (2) Salary                          FROM   Employees                          ORDER  BY Salary DESC) T);  WITH T      AS (SELECT *,                 DENSE_RANK() OVER (ORDER BY Salary DESC) AS Rnk          FROM   Employees) SELECT Name FROM   T WHERE  Rnk = 2;  SELECT Name FROM   Employees WHERE  Salary = (SELECT DISTINCT TOP (1) Salary                  FROM   Employees                  WHERE  Salary NOT IN (SELECT DISTINCT TOP (1) Salary                                        FROM   Employees                                        ORDER  BY Salary DESC)                  ORDER  BY Salary DESC)  SELECT Name FROM   Employees WHERE  Salary = (SELECT TOP 1 Salary                  FROM   (SELECT TOP 2 Salary                          FROM   Employees                          ORDER  BY Salary DESC) sel                  ORDER  BY Salary ASC)   
like image 189
Martin Smith Avatar answered Oct 05 '22 14:10

Martin Smith


 SELECT * from Employee  WHERE Salary IN (SELECT MAX(Salary)                   FROM Employee                   WHERE Salary NOT IN (SELECT MAX(Salary)                                        FFROM employee)); 

Try like this..

like image 35
amateur Avatar answered Oct 05 '22 15:10

amateur