题解 | #获取当前薪水第二多的员工#
获取当前薪水第二多的员工的emp_no以及其对应的薪水salary
https://www.nowcoder.com/practice/c1472daba75d4635b7f8540b837cc719
SELECT s.emp_no,s.salary, e.last_name,e.first_name FROM salaries s JOIN employees e USING(emp_no) WHERE s.salary = ( SELECT max(salary) FROM salaries WHERE salary < ( SELECT max(salary) FROM salaries WHERE to_date = '9999-01-01' ) and to_date = '9999-01-01' ) and s.to_date = '9999-01-01'
1、找到最大的薪资水平
SELECT max(salary) FROM salaries WHERE to_date = '9999-01-01'
2、找到第二大的薪资水平
SELECT max(salary) FROM salaries WHERE salary < ( SELECT max(salary) FROM salaries WHERE to_date = '9999-01-01' ) and to_date = '9999-01-01'
3、输出该员工信息
SELECT s.emp_no,s.salary, e.last_name,e.first_name FROM salaries s JOIN employees e USING(emp_no) WHERE s.salary = ( SELECT max(salary) FROM salaries WHERE salary < ( SELECT max(salary) FROM salaries WHERE to_date = '9999-01-01' ) and to_date = '9999-01-01' ) and s.to_date = '9999-01-01'