题解 | #获取当前薪水第二多的员工的emp_no#

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

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

select a.emp_no,salary,last_name,first_name
from salaries a 
left join employees b 
on a.emp_no  = b.emp_no 
where 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 to_date = '9999-01-01'

确实有点麻烦,先找出最高的工资,然后作为where的条件,在筛选出第二高的工资,并也作为where的条件,最后用这个条件找出工资和第二高一样的人的详细信息

select s.emp_no, s.salary, e.last_name, e.first_name
from salaries s join employees e
on s.emp_no = e.emp_no
where s.salary = (
select a.salary
from salaries  a 
join salaries b 
on a.salary < b.salary
where  a.to_date = '9999-01-01'and b.to_date = '9999-01-01'
group by a.salary
having count(distinct b.salary) =1 

)

让薪水表自己形成大小关系,然后取第一行中小于的那一方值

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-20 20:30
工作没了,落户没了,什么都没了
梦想是成为七海千秋:是因为什么原因呀,如果是因为导师恶意卡你就和他爆了
点赞 评论 收藏
分享
05-09 13:22
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务