-- 查询所有员工对应的ID号,名字,部门名称,和国家对应名字
select te.employee_id, te.first_name, td.department_name, tc.country_name
from t_employees te
inner join t_departments td on te.department_id = td.department_id
inner join t_locations tl on td.location_id = tl.location_id
inner join t_countries tc on tl.country_id = tc.country_id;
5 内连接查询 inner join on 五张表
-- 查询所有员工对应的ID号,名字,工作职称,部门名称,和国家对应名字
select te.employee_id, te.first_name, tj.job_title, td.department_name, tc.country_name
from t_employees te
inner join t_jobs tj on te.job_id = tj.job_id
inner join t_departments td on te.department_id = td.department_id
inner join t_locations tl on td.location_id = tl.location_id
inner join t_countries tc on tl.country_id = tc.country_id;
6 左外连接 left join on
-- 左外连接 左表是主表,要求左表完整显示,右表匹配左表数据,如果右表没有数据匹配,显示null
-- 查询所有的员工信息ID,first_ame,已经对应的部门名字
select te.employee_id, te.first_name, td.department_name
from t_employees te
left join t_departments td on te.department_id = td.department_id;
7 右外连接 right join on
-- 右外连接查询,右表是主表,要求右表完整展示,左表匹配右表数据,如果左表没有数据匹配,显示null
-- 查询所有部门对应员工信息,员工信息没有显示null
select td.department_name,te.employee_id, te.first_name
from t_employees te
right join t_departments td on te.department_id = td.department_id;