题解 | #查找所有员工的last_name和first_name以及对应的dept_name#

查找所有员工的last_name和first_name以及对应的dept_name

http://www.nowcoder.com/practice/5a7975fabe1146329cee4f670c27ad55

查找所有员工的last_name和first_name以及对应的dept_name:
select employees.last_name, employees.first_name, departments.dept_name from employees
先连接dept_emp表,连接的基础为:
employees.emp_no = dept_emp.emp_no
因为包括暂时没有分配部门的员工,所以选择left join
再连接departments表,连接的基础为:
on departments.dept_no = dept_emp.dept_no
因为包括暂时没有分配部门的员工,还是选择left join

最终:select employees.last_name, employees.first_name, departments.dept_name from employees
left join dept_emp
on employees.emp_no = dept_emp.emp_no
left join departments
on departments.dept_no = dept_emp.dept_no

至于为什么不线连接departments表而要先连接dept_emp表,是因为employees无法直接与departments建立连接

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
迟缓的斜杠青年巴比Q...:简历被投过的公司卖出去了,我前两天遇到过更离谱的,打电话来问我有没有意向报班学Java学习,服了,还拿我学校一个学长在他们那报班学了之后干了华为OD当招牌
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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