Second Highest Salary

用limit实现,注意边界条件,如果不存在,输出null

SELECT IFNULL((SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 1,1),NULL)

耗时:964 ms

上述方法并不高效,更高效的方法是:先用max函数求出最大值,再在剩下的值中取出最大值

select max(salary) from Employee where salary !=(select max(salary) from Employee)

耗时:764 ms

你可能感兴趣的:(Second Highest Salary)