union, intersect, minus, union all
union, intersect, minus, union all 는 연산시 relation degree(컬럼개수)가 동일해야 한다.
확인하는 차원에서 간단한 훑어보겠다.
[test1 테이블]
ID NAME
———- ———-
maddog 강명규
test1 아무개1
[test2 테이블]
ID NAME
———- ———-
maddog 강명규
test2 아무개2
SQL> create table test1 (id varchar(10), name varchar(10));
SQL> create table test2 (id varchar(10), name varchar(10));
SQL> insert into test1 values(‘maddog’, ‘강명규’);
SQL> insert into test2 values(‘maddog’, ‘강명규’);
SQL> insert into test1 values(‘test1’, ‘아무개1’);
SQL> insert into test2 values(‘test2’, ‘아무개2’);
[union]
SQL> select * from test1
2 union
3 select * from test2;
ID NAME
———- ———-
maddog 강명규
test1 아무개1
test2 아무개2
[union all]
SQL> select * from test1
2 union all
3 select * from test2;
ID NAME
———- ———-
maddog 강명규
test1 아무개1
maddog 강명규
test2 아무개2
* union의 경우, 중복행을 제거하기 위한 부하가 발생하므로 중복되는 레코드에 무관한 작업이라면 되도록 union all을 사용하도록 하라.
[intersect]
SQL> select * from test1
2 intersect
3 select * from test2;
ID NAME
———- ———-
maddog 강명규
[minus]
SQL> select * from test1
2 minus
3 select * from test2;
ID NAME
———- ———-
test1 아무개1
union, intersect, minus, union all중 차집합(minus)은 교환법칙이 성립되지 않는다.
그럼 test1, test2의 순서를 바꾸어(교환법칙) 확인해보자.
SQL> select * from test2
2 union
3 select * from test1;
ID NAME
———- ———-
maddog 강명규
test1 아무개1
test2 아무개2
SQL> select * from test2
2 intersect
3 select * from test1;
ID NAME
———- ———-
maddog 강명규
SQL> select * from test2
2 minus
3 select * from test1;
ID NAME
———- ———-
test2 아무개2
SQL>
This article comes from dbakorea.pe.kr (Leave this line as is)
