题解 | #获取当前薪水第二多的员工的emp_no以及其对应的薪水salary#

获取当前薪水第二多的员工的emp_no以及其对应的薪水salary

http://www.nowcoder.com/practice/c1472daba75d4635b7f8540b837cc719

//找到前2名,然后取第二个.

SELECT
t2.emp_no ,t2.salary , (SELECT t3.last_name from employees t3 where t2.emp_no=t3.emp_no ) last_name, (SELECT t3.first_name from employees t3 where t2.emp_no=t3.emp_no ) first_name from salaries t2 where 2 > ( select count(1) from salaries t1
where t1.salary > t2.salary ) and t2.salary < (select max(t1.salary) from salaries t1 )

先找到前几名,注意,这里是取笛卡尔积,然后根据条件进行比较:

select t2.salary from salaries t2 where 2 > ( select count(1) from salaries t1
where t1.salary > t2.salary )

全部评论

相关推荐

把球:这个听过,你加了就会发现是字节的hr
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务