1. 집합 연산자(SET OPERATOR)의 종류
2. 합집합(UNION, UNION ALL)
3. 교집합(INTERSECT)
4. 차집합(EXCEPT)
h1.1. 집합 연산자(SET OPERATOR)의 종류]
SELECT 칼럼명1, 칼럼명2
FROM 테이블명1
[WHERE 조건식 ]
[[GROUP BY 칼럼(Column)이나 표현식 [HAVING 그룹조건식 ] ]
집합 연산자
SELECT 칼럼명1, 칼럼명2
FROM 테이블명2
[WHERE 조건식 ]
[[GROUP BY 칼럼(Column)이나 표현식 [HAVING 그룹조건식 ] ]
[ORDER BY 1, 2 [ASC또는 DESC ];
TEAM_ID | PLAYER_NAME | BACK_NO |
---|---|---|
A | 박지성 | 01 |
A | 차범근 | 02 |
A | 손흥민 | 03 |
B | 박지성 | 04 |
B | 기성용 | 05 |
h3.2. 합집합(UNION, UNION ALL)
SELECT PLAYER_NAME 선수명, BACK_NO 백넘버 FROM PLAYER WHERE TEAM_ID = 'K01'
UNION
SELECT PLAYER_NAME 선수명, BACK_NO 백넘버 FROM PLAYER WHERE TEAM_ID = 'K01'
ORDER BY 1;
SELECT PLAYER_NAME 선수명, BACK_NO 백넘버 FROM PLAYER WHERE TEAM_ID = 'K01'
UNION ALL
SELECT PLAYER_NAME 선수명, BACK_NO 백넘버 FROM PLAYER WHERE TEAM_ID = 'K01'
ORDER BY 1;
h3.3. 교집합(INTERSECT)
WITH T1 AS (
SELECT 'a' a FROM DUAL UNION ALL
SELECT 'b' a FROM DUAL UNION ALL
SELECT 'c' a FROM DUAL
), T2 AS (
SELECT 'a' a FROM DUAL UNION ALL
SELECT 'b' a FROM DUAL
)
SELECT a FROM T1
INTERSECT
SELECT a FROM T2
h1.4. 차집합(EXCEPT)
WITH T1 AS (
SELECT 'a' a FROM DUAL UNION ALL
SELECT 'b' a FROM DUAL UNION ALL
SELECT 'c' a FROM DUAL
), T2 AS (
SELECT 'a' a FROM DUAL UNION ALL
SELECT 'b' a FROM DUAL
)
SELECT a FROM T1
MINUS -- ANSI SQL은 EXCEP
SELECT a FROM T2