How to get second-highest salary employees in a table

后端 未结 30 952
离开以前
离开以前 2020-12-23 20:15

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

相关标签:
30条回答
  • 2020-12-23 20:49

    this is the simple query .. if u want the second minimum then just change the max to min and change the less than(<) sign to grater than(>).

        select max(column_name) from table_name where column_name<(select max(column_name) from table_name)
    
    0 讨论(0)
  • 2020-12-23 20:49

    Using this SQL, Second highest salary will get with Employee Name

    Select top 1 start at 2 salary from employee group by salary order by salary desc;
    
    0 讨论(0)
  • 2020-12-23 20:50

    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)  
    
    0 讨论(0)
  • 2020-12-23 20:51

    All of the following queries work for MySQL:

    SELECT MAX(salary) FROM Employee WHERE Salary NOT IN (SELECT Max(Salary) FROM Employee);
    
    SELECT MAX(Salary) From Employee WHERE Salary < (SELECT Max(Salary) FROM Employee);
    
    SELECT Salary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET 1;
    
    SELECT Salary FROM (SELECT Salary FROM Employee ORDER BY Salary DESC LIMIT 2) AS Emp ORDER BY Salary LIMIT 1;
    
    0 讨论(0)
  • 2020-12-23 20:51
    select max(age) from yd where age<(select max(age) from HK) ; /// True two table Highest 
    
    SELECT * FROM HK E1 WHERE 1 =(SELECT COUNT(DISTINCT age) FROM HK E2 WHERE E1.age < E2.age); ///Second Hightest age RT single table 
    
    select age from hk e1 where (3-1) = (select count(distinct (e2.age)) from yd e2 where e2.age>e1.age);//// same True Second Hight age RT two table
    
    select max(age) from YD where age not in (select max(age) from YD);  //second hight age in single table 
    
    0 讨论(0)
  • 2020-12-23 20:52

    This might help you

    SELECT 
          MIN(SALARY) 
    FROM 
          EMP 
    WHERE 
          SALARY in (SELECT 
                          DISTINCT TOP 2 SALARY 
                     FROM 
                          EMP 
                     ORDER BY 
                          SALARY DESC
                    )
    

    We can find any nth highest salary by putting n (where n > 0) in place of 2

    Example for 5th highest salary we put n = 5

    0 讨论(0)
提交回复
热议问题