개발/database
[DB] JOIN
yun000
2024. 12. 17. 16:38
<JOIN>
=하나 이상의 테이블로 부터 데이터 조회하기 위해 사용.
+) 조회 성능을 떨어뜨리므로 가능하면 안하는 것이 좋다
1.Oracle Join
🍏CARTESIAN PRODUCT
첫번째 테이블의 모든 행이 두번째 테이블의 모든행과 조인됨
🍏EQUI JOIN
= 서로 다른 테이블에 공통된 열을 '=' 연산자로 조인
select e.first_name, e.department_id, d.department_name
from employees e, departments d
where e.department_id=d.department_id;
🍏SELF JOIN
= 같은 테이블 내에서 조인
select e.first_name as employee_name, m.first_name as manager_name
from employees e, employees m
where e.manager_id=m.employee_id and e.employee_id=103;
🍏NON-EQUI JOIN
= 서로 다른 테이블에 공통된 열이 없을 때 '='이 아닌 연산자 사용해서 조인
select e.first_name, e.salary, j.job_title
from employees e, jobs j
where e.salary
between j.min_salary and j.max_salary;
🍏OUTER_JOIN
= 어느 한쪽에 데이터 존재하지 않을 때 사용
left outer join
=table1을 기준으로 join
select table.column, table.column
from table1, table2
where rable1.column=table2.column(+);
right outer join
=table2를 기준으로 join
select table.column, table.column
from table1, table2
where rable1.column(+)=table2.column;
2. ANSI JOIN
=ANSI SQL표준문법 join이다.
🍏CROSS JOIN
=cartisan product
select table1.column1, table2.column2
from table1 cross join table2;
🍏INNER JOIN
=가장 기본적인 join, 양 쪽 테이블 둘다 값이 있어야지 join된다.
select t1.c1, t2.c1
from table1 t1 join table2 t2;
🍏NATURAL JOIN
=모두 동일한 이름을 갖는 컬럼들에 대해 조인
select first_name, department_name
from employees
natural join departments;
🍏USING JOIN
=원하는 컬럼에 대해서만 선택적으로 Equi 조인
select first_name, department_name from employees
join departments using(department_id);
+) JOIN~ON
join이후에도 추가 조건 설정 가능
select department_name, street_address,city,state_province
from departments d
join locations 1
on d.location_id=1.location_id;
join~on절 반복으로 3개 이상의 테이블 조인도 가능
select e.first_name, d.department_name, l.cirt
from employee e
join departments d on e.department_id=d.department_id
join locations l on d.location_id=l.location_id;
+) JOIN,WHERE
=ANSI join은 where절 조건과 분리해서 작성할 수 있다
🍏OUTER JOIN
기준이 되는 테이블 기준으로 출력
구조
select table1.column, table2.column
from table1
[LEFT | RIGHT | FULL] OUTER JOIN table2
예제
select e.employee_id, e.first_name, e.hire_date, j.start_date,j.end_date, j.job_id, j.department_id
from employees e left outer join job_history j
on e.employee_id=j.employee_id
order by j.employee_id;