[Data Base] 9. 집합

김미숙's avatar
Mar 05, 2025
[Data Base] 9. 집합
‼️

집합 연산자

합집합(union, union all)
교집합(intersect)
차집합(Except)
 

Union all

select sum(sal),deptno from emp group by deptno union all select sum(sal),null from emp;
notion image
‼️
Union all은 중복 제거와 정렬은 수행하지 않는다
select * from dept where deptno > 10 union all select * from dept where deptno < 30;
notion image
 
 

Union

‼️
사용할 컬럼의 수와 데이터 형식이 일치해야 하며, 합친 후에 테이블에서 중복되는 데이터는 제거한다
올바른 정렬을 위해서는 ORDER BY 구문을 추가로 사용해주는 것이 좋다
select * from dept where deptno > 10 -- 20,30,40 union select * from dept where deptno < 30; -- 10,20
notion image
 

Intersect

‼️
교집합
두 개의 테이블에 대해 겹치는 부분을 추출하는 연산
추출 후 중복된 결과를 제거하여 보여준다
select * from dept where deptno > 10 -- 20,30,40 intersect select * from dept where deptno < 30; -- 10,20
notion image

Except

‼️
차집합
두 개의 테이블에서 겹치는 부분을 앞의 테이블에서 제외하여 추출하는 연산
추출 후 중복된 결과를 제거하여 보여준다
select * from dept where deptno > 10 -- 20,30,40 except select * from dept where deptno < 30; -- 10,20
notion image
Share article

parangdajavous