数据库SQL实战:查找所有员工的last_name和first_name以及对应的dept_name(教程)

查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工

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));

这题主要用到3个表的连接

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 de.dept_no=d.dept_no
(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐