#中图:内连接 A∩B SELECT e.employee_id, e.last_name, d.department_name FROM employees e JOIN departments d ON e.`department_id` = d.`department_id`;
#左上图:左外连接 SELECT e.employee_id, e.last_name, d.department_name FROM employees e LEFTJOIN departments d ON e.`department_id` = d.`department_id`;
#右上图:右外连接 SELECT e.employee_id, e.last_name, d.department_name FROM employees e RIGHTJOIN departments d ON e.`department_id` = d.`department_id`;
#左中图:A - A∩B SELECT e.employee_id, e.last_name, d.department_name FROM employees e LEFTJOIN departments d ON e.`department_id` = d.`department_id` WHERE d.`department_id` ISNULL
#右中图:B-A∩B SELECT e.employee_id, e.last_name, d.department_name FROM employees e RIGHTJOIN departments d ON e.`department_id` = d.`department_id` WHERE e.`department_id` ISNULL
#左下图:A∪B SELECT e.employee_id, e.last_name, d.department_name FROM employees e LEFTJOIN departments d ON e.`department_id` = d.`department_id` WHERE d.`department_id` ISNULL UNIONALL #没有去重操作,效率高 SELECT e.employee_id, e.last_name, d.department_name FROM employees e RIGHTJOIN departments d ON e.`department_id` = d.`department_id`;
#右下图:A∪B - A∩B SELECT e.employee_id, e.last_name, d.department_name FROM employees e LEFTJOIN departments d ON e.`department_id` = d.`department_id` WHERE d.`department_id` ISNULL UNIONALL SELECT e.employee_id, e.last_name, d.department_name FROM employees e RIGHTJOIN departments d ON e.`department_id` = d.`department_id` WHERE e.`department_id` ISNULL
#在SQL92标准中: SELECT e.employee_id, e.last_name, d.department_name FROM employees e JOIN departments d ON e.`department_id` = d.`department_id` AND e.`manager_id` = d.`manager_id`;
#在SQL99标准中: SELECT e.employee_id, e.last_name, d.department_name FROM employees e NATURALJOIN departments d;
SELECT e.last_name, d.department_name, d.location_id, l.city FROM employees e LEFTOUTERJOIN departments d ON e.`department_id` = d.`department_id` LEFTOUTERJOIN locations l ON d.`location_id` = l.`location_id` WHERE commission_pct ISNOTNULL;
SELECT e.last_name, e.job_id, e.department_id, d.department_name FROM employees e, departments d, locations l WHERE e.`department_id` = d.`department_id` AND d.`location_id` = l.`location_id` AND l.city ='Toronto';
#或者
SELECT e.last_name, e.job_id , e.department_id , d.department_name FROM employees e JOIN departments d ON e.`department_id` = d.`department_id` JOIN locations l ON l.`location_id` = d.`location_id` WHERE l.`city` ='Toronto';
SELECT d.department_name, l.street_address, e.last_name, e.job_id, e.salary FROM employees e JOIN departments d ON e.department_id = d.department_id JOIN locations l ON d.`location_id` = l.`location_id` WHERE d.department_name ='Executive';
6.选择指定员工的姓名,员工号,以及他的管理者的姓名和员工号,结果类似于下面的格式
1 2 3 4 5 6 7 8
employees Emp# manager Mgr# kochhar 101 king 100
#答案: SELECT emp.last_name AS "employees", emp.employee_id AS "Emp#", mgr.last_name AS "manager", mgr.employee_id AS "Mgr#" FROM employees emp LEFTOUTERJOIN employees mgr ON emp.manager_id = mgr.employee_id;
7.查询哪些部门没有员工
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#方式1: SELECT d.department_id FROM departments d LEFTJOIN employees e ON e.department_id = d.department_id WHERE e.department_id ISNULL;
#方式2: SELECT d.department_id FROM departments d WHERENOTEXISTS ( SELECT* FROM employees e WHERE e.`department_id` = d.`department_id` );
8.查询哪个城市没有部门
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#方式1: SELECT l.location_id, l.city FROM locations l LEFTJOIN departments d ON l.`location_id` = d.`location_id` WHERE d.`location_id` ISNULL
#方式2: SELECT l.location_id, l.city FROM locations l WHERENOTEXISTS ( SELECT* FROM departments d WHERE d.`location_id` = l.`location_id` );