How to get second-highest salary employees in a table

后端 未结 30 960
离开以前
离开以前 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: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 
    

提交回复
热议问题