逻辑很清晰的答案,不用not in,不用自连接

获取所有非manager员工当前的薪水情况

http://www.nowcoder.com/questionTerminal/8fe212a6c71b42de9c15c56ce354bebe

分享一个逻辑很清晰的答案:

步骤

  1. 找出所有非manger的emp_no,这里通过差集实现:
    select t1.emp_no
     from employees t1
     left join dept_manager t2 on t1.emp_no = t2.emp_no
     where t2.emp_no is null
  2. 再通过上表的 emp_no 去连接 dept_emp 和 salaries,找出对应的 dept_no 和 salary
  3. 注意 salaries 的 to_date='9999-01-01'

    完整代码

    select t4.dept_no, t3.emp_no, t5.salary
    from(
     select t1.emp_no
     from employees t1
     left join dept_manager t2 on t1.emp_no = t2.emp_no
     where t2.emp_no is null
    ) t3
    inner join dept_emp t4 on t3.emp_no = t4.emp_no
    inner join salaries t5 on t3.emp_no = t5.emp_no
    where t5.to_date='9999-01-01'
    

```

全部评论
很多题都要加上and xxx='9999-01-01' 这个是什么作用.我菜不懂
1 回复 分享
发布于 2021-03-25 23:09
为什么left join manager后emp_no是null的
点赞 回复 分享
发布于 2023-10-18 15:49 四川
应该是t2.dept_no is null,emp_no是表连接的主键
点赞 回复 分享
发布于 2022-05-03 16:16
我想问这个用左连接 为什么会出现t2.emp_no is null的情况呢?
点赞 回复 分享
发布于 2021-04-02 16:04

相关推荐

有担当的灰太狼又在摸鱼:零帧起手查看图片
点赞 评论 收藏
分享
评论
32
6
分享

创作者周榜

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