natural join
cross join(full join, cartesian join)
alias 사용
select e.employee_id, e.first_name, e.salary, d.department_name
from employees e, departments d
where e.department_id = d.department_id
and e.employee_id = 100;
inner join
-- 1
select e.employee_id, e.first_name, e.salary, d.department_name
from employees e inner join departments d
on e.department_id = d.department_id
where e.employee_id = 100;
-- 2
select e.employee_id, e.first_name, e.salary, d.department_name, l.street_address
from employees e inner join departments d join locations l
on e.department_id = d.department_id
and d.location_id = l.location_id
where l.city = 'Seattle';
select e.employee_id, e.first_name, e.salary, d.department_name, l.street_address
from employees e inner join departments d
using (department_id)
join locations l
using (location_id)
where l.city = 'Seattle';
select e.employee_id, e.first_name, e.salary, d.department_name
from employees e inner join departments d
using (department_id)
where e.employee_id = 100;
natural join
select e.employee_id, e.first_name, e.salary, d.department_name
from employees e natural join departments d
where e.employee_id = 100;
-- 부서번호가 10인 부서의 부서번호, 부서이름, 도시
select d.department_id, d.department_name, l.city
from departments d natural join locations l
where d.department_id = 10;
outer join
left outer, right outer, full outer join으로 구분됨
어느 한쪽 테이블에는 해당하는 데이터가 존재하는데 다른 쪽 테이블에는 데이터가 존재하지 않을 경우 그 데이터가 검색되지 않는 문제점을 해결하기 위해 사용
left outer join
select col1, col2, ..., colN
from table1 left outer join table2
on or using
-- 회사에 근무하는 사원수
-- 107명
select e.employee_id, e.first_name, d.department_name
from employees e inner join departments d
on e.department_id = d.department_id;
-- 회사에 근무하는 모든 사원의 사번, 이름, 부서이름
-- 106명 >> 문제 발생..
-- 부서가 없는(부서번호가 null) 사원 검색
select * from employees
where department_id is null;
-- 해결
select e.employee_id, e.first_name, d.department_name
from employees e left outer join departments d
on e.department_id = d.department_id;
right outer join
-- 회사의 부서수 >> 27
select department_id
from departments;
-- 사원이 근무하는 부서수 >> 12
select distinct department_id
from employees
where department_id is not null;
-- 사원이 없는 부서의 정보는 출력이 안됨.
select d.department_id, e.employee_id, e.first_name
from employees e join departments d
on e.department_id = d.department_id;
-- 해결
select d.department_id, e.employee_id, e.first_name
from employees e right outer join departments d
on e.department_id = d.department_id;
None-Equi join
-- 모든 사원의 사번, 이름, 급여, 급여등급
select e.employee_id, e.first_name, e.salary, s.grade
from employees e join salgrades s
on e.salary >= s.losal
and e.salary <= s.hisal;
--between 사용
select e.employee_id, e.first_name, e.salary, s.grade
from employees e join salgrades s
on e.salary between s.losal
and s.hisal;