8.2.4左外连接和右外连接
示例:
查询所有雇员的名字以及他们的部门名称,包含那些没有部门的雇员。
select e.last_name,d.department_name from employees e LEFT OUTER JOIN departments d on e.dept_id = d.department_id;
示例:
查询所有雇员的名字以及他们的部门名称,包含那些没有雇员的部门。
select e.last_name,d.department_name from employees e RIGHT OUTER JOIN departments d on e.DEPARTMENT_ID = d.department_id;
8.2.5多表连接查询练习
1.写一个查询显示所有雇员的 last name、department id、and department name。
select e.LAST_NAME,e.DEPARTMENT_ID,d.DEPARTMENT_NAME FROM employees e,departments d WHERE e.DEPARTMENT_ID = d.DEPARTMENT_ID;
2.写一个查询显示所有有佣金的雇员的 last name、department name、location ID 和城
select e.LAST_NAME,d.DEPARTMENT_NAME,l.LOCATION_ID,l.CITY FROM employees e,departments d,locations l WHERE e.DEPARTMENT_ID = d.DEPARTMENT_ID AND d.LOCATION_ID = l.LOCATION_ID AND e.COMMISSION_PCT IS NOT NULL;
3.显示所有在其 last names 中有一个小写 a 的雇员的 last name 和 department name。
SELECT e.LAST_NAME,d.DEPARTMENT_NAME FROM employees e,departments d WHERE e.DEPARTMENT_ID = d.DEPARTMENT_ID AND e.LAST_NAME like '%a%'
4.用sql99的内连接写一个查询显示那些工作在 Toronto 的所有雇员的 last name、job、department number 和 department name。
SELECT e.LAST_NAME,e.JOB_ID,e.DEPARTMENT_ID,d.DEPARTMENT_NAME FROM employees e INNER JOIN departments d ON e.DEPARTMENT_ID = d.DEPARTMENT_ID INNER JOIN locations l ON d.LOCATION_ID = l.LOCATION_ID WHERE l.CITY = 'Toronto';
6.显示雇员的 last name 和 employee number 连同他们的经理的 last name 和manager number。列标签分别为 Employee、Emp#、Manager 和 Mgr#
SELECT w.LAST_NAME empname,w.DEPARTMENT_ID empid,m.LAST_NAME mgrname,m.EMPLOYEE_ID mgrid FROM employees w INNER JOIN employees m ON w.MANAGER_ID = m.EMPLOYEE_ID;