SQL에서 join이란?
- 두 개 이상의 table들의 관계를 찾아 이를 기반으로 조합하여 출력할 때 사용하는 문법입니다.
- join에는 inner, outer, natural, cross 등 여러 종류가 있습니다.
implicit join vs explicit join
implicit join
select d.name
from employee as e, department as d
where e.id = 1 and e.dept_id = d.id;
- implicit join은 from 절에서 join할 table들을 나열하고 where 절에서 join condition을 설정합니다. 이러한 방식을 implicit joing이라고 합니다.
- implicit join의 단점은 join condition을 where절에서 select condition과 같이 사용하기 때문에 가독성이 매우 떨어집니다. 이러한 단점을 보완한 것이 추후에 추가된 explicit join입니다.
explicit join
select d.name
from employee as e join department as d on e.dept_id = d.id
where e.id = 1;
- explicit join은 implicit join의 단점이였던 가시성을 보완한 문법입니다. join을 from 절에 명시하고 join condition을 on 뒤에 작성함으로써 join을 사용 여부와 join condition의 가시성을 높였습니다.
inner join vs outer join
inner join
select d.name
from employee as e [ inner ] join department as d on e.dept_id = d.id
where e.id = 1;
- inner join은 위의 explicit join 예시에서 inner를 생략한 join과 동일합니다. 즉, inner 부분은 생략이 가능합니다.
- inner join은 join condition이 true를 만족하는 tuple들로 result table을 만드는 join 입니다.
outer join
from table1 left [outer] join table2 on join_condition
from table1 right [outer] join table2 on join_condition
from table1 full [outer] join table2 on join_condition
- outer join은 두 테이블에서 join condition을 만족하지 않는 tuple들도 result table에 포함되는 join입니다. left outer join, right outer join, ful outer join이 있습니다.
- []안에 있는 outer 부분은 생략 가능합니다.
left outer join
select *
from employee as e left outer join department as d
on e.dept_id = d.id
- left outer join은 left outer join 절의 왼쪽의 테이블을 기준으로 join condition에 맞지 않는 튜플도 출력되며 오른쪽 테이블의 join condition이 맞지 않는 attribute 부분들은 null로 채워져 출력됩니다.
right outer join
select *
from employee as e right outer join department as d
on e.dept_id = d.id;
- right outer join은 right outer join 절의 오른쪽 테이블을 기준으로 join condition에 맞지 않는 튜플도 출력되며 왼쪽 테이블의 join condition이 맞지 않는 attribtue 부분들은 null로 채워져 출력됩니다.
full outer join
pgrsql# select *
from employee as e full outer join department as d on e.dept_id = d.id;
- full outer join은 MySQL에서는 지원되지 않습니다. 그래서 위 쿼리는 PostgreSQL을 기준으로 작성되었습니다.
- full outer join은 left, right outer join이 합쳐진 join이며 양쪽 테이블의 모든 튜플을 출력하는데 join condition이 맞지 않을 경우 null로 채워 출력됩니다.
join과 using
select d.name
from employee as e inner join department as d on e.dept_id = d.dept_id
-- using 사용
select d.name
from employee as e inner join department as d using(dept_id)
where e.id = 1;
- 첫 번째 쿼리문을 사용할 경우 join condition에서 사용된 양측의 dept_id attribute가 두 번 출력됩니다. 이를 한 번만 출력되도록 변경하려면 using을 사용해서 on e.dept_id = d.dept_id을 생략할 수 있습니다.
- using의 조건은 equi join(=) 할 때 join하는 attribute의 이름이 같으면 사용할 수 있습니다.
natural join
from table1 natural [inner] join table2
from table1 natural left [outer] join table2
from table1 natural right [outer] join table2
from table1 natural full [outer] join table2
- 두 테이블에서 같은 이름을 가진 모든 attribute pair에 대해서 equi join을 수행합니다. 그렇기 때문에 이름은 같지만 다른 목적으로 사용되는 attribute가 있는지 유의하여 사용해야 합니다.
- join condition은 따로 명시하지 않습니다.
cross join
implicit cross join : from table1, table2
explicit cross join : from table1 cross join table2
-- 예시
select * from employee cross join department;
- 두 테이블의 tuple마다 모든 조합을 만들어 result table을 반환합니다.
- join condition을 사용하지 않습니다.
- MySQL에서는 cross join = inner join = join으로써, cross join에 on을 같이 쓰면 inner join으로 동작되며,inner join이 on 없이 사용되면 corss join으로 동작합니다.
self join
- table이 자신에게 join 하는 경우를 self join 이라고 합니다.
'데이터 베이스 > RDBMS' 카테고리의 다른 글
SQL의 stored function이란? (0) | 2023.02.03 |
---|---|
SQL의 order by, aggregate function, group by, having 사용법 ( select 최종 문법 정리 ) (0) | 2023.02.03 |
RDBMS에서 null 비교 연산 사용법 (0) | 2023.02.01 |
subquery를 이용한 select 사용법 (1) | 2023.02.01 |
select 사용법 ( with as, distinct, like ) (0) | 2023.02.01 |