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

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

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

select 
    t2.last_name,
    t2.first_name,
    t1.dept_name
from (
    select a.dept_name, b.emp_no, b.dept_no
    from departments a right join dept_emp b --先把部门表和员工部门关系表链接起来,以后者为基准进行连接
    on a.dept_no=b.dept_no
    where b.to_date='9999-01-01'
) t1 
right join employees t2 --再跟员工表连接,注意要返回所有员工的部门分配情况,所以按照员工表的员工Id连接
on t1.emp_no=t2.emp_no
全部评论

相关推荐

投递恒生电子股份有限公司等公司7个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务