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;
说明:返回 employees 表中没有匹配 departments 表的记录。
5. 右外连接 - 无匹配记录 (RIGHT JOIN with WHERE) 右中图
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.employee_id ISNULL;
说明:返回 departments 表中没有匹配 employees 表的记录。
6. 全外连接 (FULL OUTER JOIN) 左下图
SELECT e.employee_id, e.last_name, d.department_name
FROM employees e
LEFTJOIN departments d ON e.department_id = d.department_id
UNIONALLSELECT e.employee_id, e.last_name, d.department_name
FROM employees e
RIGHTJOIN departments d ON e.department_id = d.department_id
WHERE e.employee_id ISNULL;
说明:返回两个表中所有的记录,包括不匹配的记录。由于 SQL 标准不支持 FULL OUTER JOIN,这里使用 UNION ALL 来模拟。
7. 全外连接 - 无匹配记录 (FULL OUTER JOIN with WHERE) 右下图
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 ISNULLUNIONALLSELECT e.employee_id, e.last_name, d.department_name
FROM employees e
RIGHTJOIN departments d ON e.department_id = d.department_id
WHERE e.employee_id ISNULL;
说明:返回两个表中没有匹配的记录。同样使用 UNION ALL 来模拟 FULL OUTER JOIN 的效果。