--多表查询
select employees.employee_id,employees.department_id,departments.department_name
from departments,employees
where employees.department_id=departments.department_id;
运行结果
--多表查询 其他情况
select e.employee_id,e.department_id,d.department_name
from departments d,employees e
where e.department_id=d.department_id;
--多表查询 其他情况
select employee_id,e.department_id,department_name
from departments d,employees e
where e.department_id=d.department_id;
运行结果
--员工在什么城市工作 三个表连接
select employee_id,e.department_id,department_name,city
from departments d,employees e,locations l
where e.department_id=d.department_id and d.location_id=l.location_id
运行结果
--根据档次转换 非等式连接
select employee_id,last_name,salary,grade_level from employees e,job_grades j
where e.salary between j.lowest_sal and j.highest_sal
运行结果
外连接
--多表查询 外连接
select employee_id,e.department_id,department_name
from departments d,employees e
where e.department_id=d.department_id(+)
运行结果
--多表查询 右外连接
select employee_id,e.department_id,department_name
from departments d,employees e
where e.department_id(+)=d.department_id
运行结果
--自然连接
select employee_id,department_id,department_name
from employees join departments
using(department_id)
运行结果
--自然连接
select employee_id,d.department_id,department_name
from employees e join departments d
on e.department_id=d.department_id
运行结果
--自然连接 三表连接
select employee_id,d.department_id,department_name
from employees e join departments d
on e.department_id=d.department_id
join locations l
on d.location_id=l.location_id
运行结果
--自然连接 左外
select employee_id,d.department_id,department_name
from employees e left outer join departments d
on e.department_id=d.department_id
运行结果
--自连接
--查询公司中员工'chen'的manager信息
select emp.last_name,manager.last_name,manager.salary,manager.email
from employees emp,employees manager
where emp.manager_id=manager.employee_id and lower(emp.last_name)='chen'
运行结果