列表

详情


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

描述

有一个员工表employees简况如下:
emp_no
birth_date
first_name
last_name
gender hire_date
10001
1953-09-02
Georgi
Facello
M 1986-06-26
10002
1964-06-02
Bezalel
Simmel
F 1985-11-21
10003
1959-12-03
Parto
Bamford
M 1986-08-28
10004
1954-05-01
Chirstian
Koblick
M 1986-12-01


有一个部门表departments表简况如下:
dept_no dept_name
d001 Marketing
d002
Finance
d003
Human Resources


有一个,部门员工关系表dept_emp简况如下:
emp_no
dept_no
from_date
to_date
10001 d001
1986-06-26 9999-01-01
10002 d001
1996-08-03 9999-01-01
10003 d002
1990-08-05 9999-01-01


请你查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工,以上例子输出如下:
last_name first_name dept_name
Facello Georgi Marketing
Simmel Bezalel Marketing
Bamford Parto Finance
Koblick Chirstian NULL

示例1

输入:

drop table if exists  `departments` ; 
drop table if exists  `dept_emp` ; 
drop table if exists  `employees` ; 
CREATE TABLE `departments` (
`dept_no` char(4) NOT NULL,
`dept_name` varchar(40) NOT NULL,
PRIMARY KEY (`dept_no`));
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
INSERT INTO departments VALUES('d001','Marketing');
INSERT INTO departments VALUES('d002','Finance');
INSERT INTO departments VALUES('d003','Human Resources');
INSERT INTO dept_emp VALUES(10001,'d001','1986-06-26','9999-01-01');
INSERT INTO dept_emp VALUES(10002,'d001','1996-08-03','9999-01-01');
INSERT INTO dept_emp VALUES(10003,'d002','1990-08-05','9999-01-01');
INSERT INTO employees VALUES(10001,'1953-09-02','Georgi','Facello','M','1986-06-26');
INSERT INTO employees VALUES(10002,'1964-06-02','Bezalel','Simmel','F','1985-11-21');
INSERT INTO employees VALUES(10003,'1959-12-03','Parto','Bamford','M','1986-08-28');
INSERT INTO employees VALUES(10004,'1954-05-01','Chirstian','Koblick','M','1986-12-01');

输出:

Facello|Georgi|Marketing
Simmel|Bezalel|Marketing
Bamford|Parto|Finance
Koblick|Chirstian|None

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3244KB, 提交时间: 2020-07-12

select e.last_name, e.first_name, d.dept_name
from employees as e
left join dept_emp as de
on e.emp_no = de.emp_no
left join departments as d
on de.dept_no = d.dept_no

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3320KB, 提交时间: 2020-10-30

select a.last_name,a.first_name,c.dept_name from employees as a left join dept_emp as b
on a.emp_no = b.emp_no left join departments as c on b.dept_no = c.dept_no

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3320KB, 提交时间: 2020-10-30

select e.last_name, e.first_name,de.dept_name
from employees e 
left join dept_emp d 
on e.emp_no = d.emp_no
left join departments de 
on d.dept_no = de.dept_no

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3336KB, 提交时间: 2020-11-23

select b.last_name,b.first_name,a.dept_name
from (select a.last_name,a.first_name,b.dept_no
      from employees as a left join dept_emp as b on a.emp_no=b.emp_no) as b
      left join departments as a
on a.dept_no=b.dept_no

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3356KB, 提交时间: 2021-08-04

select e.last_name,e.first_name,d.dept_name
from employees e left join dept_emp de
on e.emp_no=de.emp_no
left join departments d
on d.dept_no=de.dept_no