SET_OPERATION
SET OPERATION (์งํฉ์ฐ์ฐ)
UNION : ํฉ์งํฉ
UNION ALL : ๊ณตํต๋๋ถ๋ถ์ ๋๋ฒ๋ํ๊ฑฐ (๊ต์งํฉ๋ถ๋ถ์ ์ ์ธํ์ง ์๊ณ ๊ทธ๋๋ก๋ํ๊ฑฐ)
INTERSECT : ๊ต์งํฉ
MINUS : ์ฐจ์งํฉ
SQL
๋ณต์ฌ
1. UNION : ์ฌ๋ฌ๊ฐ์ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ํ๋๋ก ํฉ์น๋ ์ฐ์ฐ์์ด๋ค
์ค๋ณต๋ ์์ญ์ ์ ์ธํ์ฌ ํ๋๋ก ํฉ์น๋ค.(๋ง์ด์ฐ์)
EX)
SELECT
EMP_ID
, EMP_NAME
, DEPT_CODE
, SALARY
FROM EMPLOYEE
WHERE DEPT_CODE = 'D5'
UNION
SELECT EMP_ID
, EMP_NAME
, DEPT_CODE
, SALARY
FROM EMPLOYEE
WHERE SALARY > 3000000;
---------------------------------------------------------------
2. UNION ALL : ์ฌ๋ฌ๊ฐ์ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ํ๋๋ก ํฉ์น๋ ์ฐ์ฐ์
UNION๊ณผ์ ์ฐจ์ด์ ์ ์ค๋ณต์์ญ์ ๋ชจ๋ ํฌํจ์ํจ๋ค.
SELECT
EMP_ID
, EMP_NAME
, DEPT_CODE
, SALARY
FROM EMPLOYEE
WHERE DEPT_CODE = 'D5'
UNION ALL
SELECT EMP_ID
, EMP_NAME
, DEPT_CODE
, SALARY
FROM EMPLOYEE
WHERE SALARY > 3000000;
----------------------------------------------------------------
SELECT
DEPT_CODE
, JOB_CODE
, SUM(SALARY)
FROM EMPLOYEE
GROUP BY ROLLUP(DEPT_CODE, JOB_CODE)
UNION
SELECT '' >>์ปฌ๋ผ์๋ฅผ ๋ง์ถฐ์ฃผ๊ธฐ ์ํด์ ''๋ฅผ ์ถ๊ฐํจ
, JOB_CODE
, SUM(SALARY)
FROM EMPLOYEE
GROUP BY ROLLUP(DEPT_CDOE ,JOB_CODE)
ORDER BY 1;
----------------------------------------------------------------------------
3. INTERSECT : ์ฌ๋ฌ๊ฐ์ SELCTํ ๊ฒฐ๊ณผ์์ ๊ณตํต๋ถ๋ถ๋ง ๊ฒฐ๊ณผ๋ก ์ถ์ถ
์ํ์์ ๊ต์งํฉ๊ณผ ๋น์ท
SELECT
EMP_ID
, EMP_NAME
, DEPT_CODE
, SALARY
FROM EMPLOYEE
WHERE DEPT_CODE = 'D5'
INTERSECT
SELECT EMP_ID
, EMP_NAME
, DEPT_CODE
, SALARY
FROM EMPLOYEE
WHERE SALARY > 3000000;
-------------------------------------------------------------------
4. MINUS : ์ ํ SELECT ๊ฒฐ๊ณผ์์ ํํ SELECT ๊ฒฐ๊ณผ์ ๊ฒน์น๋๋ถ๋ถ์ ์ ์ธํ
๋๋จธ์ง ๋ถ๋ถ๋ง ์ถ์ถ, ์ํ์์ ์ฐจ์งํฉ๊ณผ ๋น์ทํ๋ค.
SELECT
EMP_ID
, EMP_NAME
, DEPT_CODE
, SALARY
FROM EMPLOYEE
WHERE DEPT_CODE = 'D5'
MINUS
SELECT EMP_ID
, EMP_NAME
, DEPT_CODE
, SALARY
FROM EMPLOYEE
WHERE SALARY > 3000000;
SQL
๋ณต์ฌ