最简单解法 | 求薪水排名第二的员工,不用order by
获取当前薪水第二多的员工的emp_no以及其对应的薪水salary
https://www.nowcoder.com/practice/c1472daba75d4635b7f8540b837cc719
核心思路:分别求两次maxSalary,第二次maxSalary如果小于第一次的maxSalary,那么它就是排名第二的薪资。
-- 第一次求maxSalary select max(salary) as maxSalary from salaries; -- 第二次求maxSalary,同时判断是否小于第一次的maxSalary -- 小于就证明是排名第二的薪资 select max(salary) from salaries where salary < ( select max(salary) as maxSalary from salaries)); -- 根据以上结果,查询薪资第二的员工信息 select t1.emp_no, salary, last_name, first_name from employees t1 join salaries t2 on t1.emp_no = t2.emp_no and salary = ( select max(salary) from salaries where salary < ( select max(salary) as maxSalary from salaries));