ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/DataBase

[SQL] WHERE ์ ˆ

NaNaRin๐Ÿ™ƒ 2021. 2. 20. 21:52

1. WHERE ์ ˆ

- ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ํŠน์ • ์กฐ๊ฑด์„ ๊ธฐ์ค€์œผ๋กœ ์›ํ•˜๋Š” ํ–‰์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ

SELECT 	[์กฐํšŒํ•  ์นผ๋Ÿผ 1 ์ด๋ฆ„], [์นผ๋Ÿผ 2 ์ด๋ฆ„], … , [์นผ๋Ÿผ N ์ด๋ฆ„]
FROM 	[์กฐํšŒํ•  ํ…Œ์ด๋ธ” ์ด๋ฆ„]
WHERE	[์กฐํšŒํ•  ์นผ๋Ÿผ์„ ์„ ๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์‹];

-- ์กฐ๊ฑด์‹์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ธ ์นผ๋Ÿผ๋งŒ ์ถœ๋ ฅ
SELECT 	* FROM EMP
WHERE 	DEPTNO = 30;

- WHERE์ ˆ์ด ํฌํ•จ๋œ SELECT๋ฌธ์„ ์‹คํ–‰ํ•˜๋ฉด ์กฐํšŒํ•  ํ…Œ์ด๋ธ”์˜ ๊ฐ ์นผ๋Ÿผ์— WHERE์ ˆ์˜ ์กฐ๊ฑด์‹์„ ๋Œ€์ž…ํ•˜์—ฌ ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ธ ์นผ๋Ÿผ๋งŒ ์ถœ๋ ฅ

 

2. ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž AND , OR

- WHERE์ ˆ์—์„œ ์กฐ๊ฑด์‹์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž AND, OR

-- 1. AND
SELECT * FROM EMP
WHERE DEPTNO = 30
	AND JOB = 'SALESMAN';
    
-- 2. OR
SELECT * FROM EMP
WHERE DEPTNO = 30
	AND JOB = 'SALESMAN';

  (1) AND ์—ฐ์‚ฐ์ž : DEPTNO๊ฐ€ 30์ด๊ณ  ๋™์‹œ์— JOB์ด SALESMAN์ธ ์นผ๋Ÿผ๋งŒ ์ถœ๋ ฅ. ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ๋ชจ๋‘ ์ฐธ์ผ๋•Œ๋งŒ ์ฐธ

  (2) OR ์—ฐ์‚ฐ์ž : DEPTNO๊ฐ€ 30์ด๊ฑฐ๋‚˜ JOB์ด SALESMAN์ธ ์นผ๋Ÿผ ์ถœ๋ ฅ. ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์ฐธ์ด๋ฉด ์ฐธ

- SQL์— ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ณธ ํ˜•์‹์€ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•˜์ง€ ์•Š์ง€๋งŒ 'SALESMAN'๊ณผ ๊ฐ™์ด ํ…Œ์ด๋ธ” ์•ˆ์— ๋“ค์–ด์žˆ๋Š” ๋ฌธ์ž ๋˜๋Š” ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์˜ํ•  ๊ฒƒ

- WHERE์ ˆ์— ์‚ฌ์šฉํ•˜๋Š” ์กฐ๊ฑด์‹์˜ ๊ฐœ์ˆ˜๋Š” ์‚ฌ์‹ค์ƒ ์ œํ•œ์ด ์—†๋‹ค. A AND B AND C AND … ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค

 

3. ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž +, -, *, /

-- 1. ๋”ํ•˜๊ธฐ
SELECT	* FROM EMP
WHERE	EMPNO + 10 = 3000;

-- 2. ๋นผ๊ธฐ
SELECT	* FROM EMP
WHERE	EMPNO - 10 = 3000;

-- 3. ๊ณฑํ•˜๊ธฐ
SELECT 	* FROM EMP
WHERE 	SAL * 12 = 12000;

-- 4. ๋‚˜๋ˆ„๊ธฐ
SELECT	* FROM EMP
WHERE	SAL * 12 / 3000 = 4;

- ์ผ๋ฐ˜์ ์ธ ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž๋Š” ๋ชจ๋‘ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

- ๋‚˜๋ˆ„๊ธฐ ์—ฐ์‚ฐ์ž(%)๋Š” SQL๋ฌธ์—์„œ ์ œ๊ณตํ•˜์ง€ ์•Š์Œ. ๋Œ€์‹  ์˜ค๋ผํด์—์„œ๋Š” mod ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ ๊ธฐ๋Šฅ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅ

 

4. ๋Œ€์†Œ ๋น„๊ต ์—ฐ์‚ฐ์ž

-- 1. ์ด์ƒ
SELECT	* FROM EMP
WHERE	SAL >= 3000;

-- 2. ์ดˆ๊ณผ
SELECT	* FROM EMP
WHERE	SAL > 3000;

-- 3. ์ดํ•˜
SELECT	* FROM EMP
WHERE	SAL <= 3000;

-- 4. ๋ฏธ๋งŒ
SELECT	* FROM EMP
WHERE	SAL < 3000;

-- 5. ๋ฌธ์ž ํ•˜๋‚˜ ๋น„๊ต
SELECT	* FROM EMP
WHERE	ENAME >= 'F';

-- 6. ๋ฌธ์ž์—ด ๋น„๊ต
SELECT	* FROM EMP
WHERE	ENAME >= 'FORZ';

  (1) ์ˆซ์ž๋Š” ์ผ๋ฐ˜์ ์ธ ๋Œ€์†Œ ๋น„๊ต ์—ฐ์‚ฐ์ž์˜ ๊ทœ์น™์„ ๋”ฐ๋ฆ„

  (2) ๋ฌธ์ž์—ด์€ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋กœ ๋ฌธ์ž์—ด์˜ ๋Œ€์†Œ๋ฅผ ๋น„๊ต

 

5. ๋“ฑ๊ฐ€ ๋น„๊ต ์—ฐ์‚ฐ์ž

-- 1. ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ๊ฐ™์„ ๋•Œ TRUE
SELECT 	* FROM EMP
WHERE	SAL = 3000;

-- 2. ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ๋‹ค๋ฅผ ๋•Œ TRUE
SELECT 	* FROM EMP
WHERE	SAL != 3000;

SELECT 	* FROM EMP
WHERE	SAL <> 3000;

SELECT 	* FROM EMP
WHERE	SAL ^= 3000;

  (1) ์–‘์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋งŒ ์ถœ๋ ฅ

  (2) ์–‘์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ๋‹ค๋ฅผ ๊ฒฝ์šฐ๋งŒ ์ถœ๋ ฅ. != ์™€ <> ์™€ ^= ๋Š” ๋ชจ๋‘ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ’์„ ์ถœ๋ ฅ

 

6. ๋…ผ๋ฆฌ ๋ถ€์ • ์—ฐ์‚ฐ์ž

- ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ TRUE๋ฉด FALSE๋ฅผ, FALSE๋ฉด TRUE๋ฅผ ์ถœ๋ ฅ

SELECT 	* FROM EMP
WHERE	NOT SAL = 3000;

- NOT ์—ฐ์‚ฐ์ž๋Š” ๋ณดํ†ต IN, BETWEEN, IS NULL ์—ฐ์‚ฐ์ž์™€ ๋ณตํ•ฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค

- ๋Œ€์†Œ / ๋“ฑ๊ฐ€ ๋น„๊ต ์—ฐ์‚ฐ์ž์— ์ง์ ‘ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์ ๋‹ค

- ๋ณต์žกํ•œ ์—ฌ๋Ÿฌ๊ฐœ ์กฐ๊ฑด์‹์ด AND, OR๋กœ ๋ฌถ์—ฌ์žˆ๋Š” ์ƒํƒœ์—์„œ ์ •๋ฐ˜๋Œ€ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ณ ์ž ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค

 

7. IN ์—ฐ์‚ฐ์ž

- '=' ๊ธฐํ˜ธ๋Š” WHERE ์กฐ์„ ์‹์—์„œ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์นผ๋Ÿผ๋งŒ์„ ์กฐํšŒํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

- ์ด๋ฅผ OR ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ์กฐ๊ฑด ์ค‘ ํ•˜๋‚˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์นผ๋Ÿผ์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

- ํ•˜์ง€๋งŒ ์กฐ๊ฑด์ด ๋Š˜์–ด๋‚ ์ˆ˜๋ก WHERE์ ˆ์ด ๊ธธ์–ด์ง€๋Š”๋ฐ ์ด๋ฅผ IN ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค

SELECT 	[์กฐํšŒํ•  ์นผ๋Ÿผ 1 ์ด๋ฆ„], [์นผ๋Ÿผ 2 ์ด๋ฆ„], … , [์นผ๋Ÿผ N ์ด๋ฆ„]
FROM 	[์กฐํšŒํ•  ํ…Œ์ด๋ธ” ์ด๋ฆ„]
WHERE	์นผ๋Ÿผ ์ด๋ฆ„ IN (๋ฐ์ดํ„ฐ 1, ๋ฐ์ดํ„ฐ 2, … ,๋ฐ์ดํ„ฐ N);

-- IN ์—ฐ์‚ฐ์ž ์—†์ด
SELECT * FROM EMP
WHERE  JOB = 'MANAGER',
	OR JOB = 'SALESMAN',
	OR JOB = 'CLERK';

-- IN ์—ฐ์‚ฐ์ž ์‚ฌ์šฉํ•ด์„œ
SELECT * FROM EMP
WHERE  JOB IN ('MANAGER', 'SALESMAN', 'CLERK');

-- IN ์—ฐ์‚ฐ์ž ์—†์ด
SELECT * FROM EMP
WHERE   JOB != 'MANAGER',
	AND JOB <> 'SALESMAN',
	AND JOB ^= 'CLERK';

-- IN ์—ฐ์‚ฐ์ž ์‚ฌ์šฉํ•ด์„œ
SELECT * FROM EMP
WHERE  JOB NOT IN ('MANAGER', 'SALESMAN', 'CLERK');

  (1) EMP ํ…Œ์ด๋ธ”์—์„œ JOB ์ด MANAGER ์ด๊ฑฐ๋‚˜ SALESMAN ์ด๊ฑฐ๋‚˜ CLERK ์ธ ์นผ๋Ÿผ์„ ๋ชจ๋‘ ์กฐํšŒ

  (2) EMP ํ…Œ์ด๋ธ”์—์„œ JOB ์ด MANAGER ๋„ SALESMAN ๋„ CLERK ๋„ ์•„๋‹Œ ์นผ๋Ÿผ์„ ๋ชจ๋‘ ์กฐํšŒ

 

8. BETWEEN A AND B ์—ฐ์‚ฐ์ž

- ์นผ๋Ÿผ ๊ฐ’์ด ์ตœ์†Ÿ๊ฐ’ ์ด์ƒ ์ตœ๋Œ“๊ฐ’ ์ดํ•˜ ์‚ฌ์ด์— ์žˆ๋Š” ์นผ๋Ÿผ๋งŒ์„ ์กฐํšŒํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ

- ๋Œ€์†Œ ๋น„๊ต ์—ฐ์‚ฐ์ž์™€ AND ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ BETWEEN A AND B ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„ ๊ฐ€๋Šฅ

-- ๋Œ€์†Œ ๋น„๊ต ์—ฐ์‚ฐ์ž์™€ AND ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉ
SELECT * FROM EMP
WHERE 	SAL >= 2000
	AND SAL <= 3000;
    
-- BETWEEN A AND B ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ
SELECT * FROM EMP
WHERE 	SAL BETWEEN 2000 AND 3000;

SELECT * FROM EMP
WHERE 	SAL NOT BETWEEN 2000 AND 3000;

  (1) EMP ํ…Œ์ด๋ธ”์—์„œ SAL ๊ฐ’์ด 2000์ด์ƒ 3000์ดํ•˜์ธ ์นผ๋Ÿผ๋งŒ ์ถœ๋ ฅ

  (2) EMP ํ…Œ์ด๋ธ”์—์„œ SAL ๊ฐ’์ด 2000๋ฏธ๋งŒ 3000์ดˆ๊ณผ์ธ ์นผ๋Ÿผ๋งŒ ์ถœ๋ ฅ

 

9. LIKE ์—ฐ์‚ฐ์ž์™€ ์™€์ผ๋“œ์นด๋“œ '%', '_'

- ์ œ๋ชฉ ๋˜๋Š” ๋‚ด์šฉ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์ฒ˜๋Ÿผ ์ผ๋ถ€ ๋ฌธ์ž์—ด์ด ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์‚ฌ์šฉ

- ์™€์ผ๋“œ ์นด๋“œ : ํŠน์ • ๋ฌธ์ž ๋˜๋Š” ๋ฌธ์ž์—ด์„ ๋Œ€์ฒดํ•˜๊ฑฐ๋‚˜ ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ์˜ ํŒจํ„ด์„ ํ‘œ๊ธฐํ•˜๋Š” ํŠน์ˆ˜ ๋ฌธ์ž

-- 1
SELECT * FROM EMP WHERE ENAME LIKE 'S%';
SELECT * FROM EMP WHERE ENAME NOT LIKE 'S%';

-- 2
SELECT * FROM EMP WHERE ENAME LIKE '%S';
SELECT * FROM EMP WHERE ENAME NOT LIKE '%S';

-- 3
SELECT * FROM EMP WHERE ENAME LIKE 'S_';
SELECT * FROM EMP WHERE ENAME NOT LIKE 'S_';

-- 4
SELECT * FROM EMP WHERE ENAME LIKE '_S';
SELECT * FROM EMP WHERE ENAME NOT LIKE '_S';

-- 5
SELECT * FROM EMP WHERE ENAME LIKE '_S%';
SELECT * FROM EMP WHERE ENAME NOT LIKE '_S%';

-- 6
SELECT * FROM EMP WHERE ENAME LIKE '%S_';
SELECT * FROM EMP WHERE ENAME NOT LIKE '%S_';

-- 7
SELECT * FROM EMP WHERE ENAME LIKE '%S%';
SELECT * FROM EMP WHERE ENAME NOT LIKE '%S%';

- '%' : ๊ธธ์ด์™€ ์ƒ๊ด€์—†์ด(๋ฌธ์ž ์—†๋Š” ๊ฒฝ์šฐ๋„ ํฌํ•จ) ๋ชจ๋“  ๋ฌธ์ž ๋ฐ์ดํ„ฐ๋ฅผ ์˜๋ฏธ

-  '_'  : ์–ด๋–ค ๊ฐ’์ด๋“  ์ƒ๊ด€์—†์ด ํ•œ ๊ฐœ์˜ ๋ฌธ์ž ๋ฐ์ดํ„ฐ๋ฅผ ์˜๋ฏธ

  (1) WHERE ENAME LIKE 'S%' : S๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋ชจ๋“  ์‚ฌ์›

  (2) WHERE ENAME LIKE '%S' : S๋กœ ๋๋‚˜๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋ชจ๋“  ์‚ฌ์›

  (3) WHERE ENAME LIKE 'S_' : S๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋‘ ๊ธ€์ž ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋ชจ๋“  ์‚ฌ์›

  (4) WHERE ENAME LIKE '_S' : S๋กœ ๋๋‚˜๋Š” ๋‘ ๊ธ€์ž ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋ชจ๋“  ์‚ฌ์›

  (5) WHERE ENAME LIKE '_S%' : ๋‘ ๋ฒˆ์งธ ๊ธ€์ž๊ฐ€ S์ธ ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋ชจ๋“  ์‚ฌ์›

  (6) WHERE ENAME LIKE '%S_' : ๋์—์„œ ๋‘ ๋ฒˆ์งธ ๊ธ€์ž๊ฐ€ S์ธ ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋ชจ๋“  ์‚ฌ์›

  (7) WHERE ENAME LIKE '%S%' : ์ด๋ฆ„ ์•ˆ์— S๋ฅผ ํฌํ•จํ•˜๋Š” ๋ชจ๋“  ์‚ฌ์›

  + NOT์ด ๋ถ™์œผ๋ฉด ๋ฐ˜๋Œ€

 

10. ESCAPE ์ ˆ

- ์™€์ผ๋“œ ์นด๋“œ ๋ฌธ์ž๊ฐ€ ๋ฐ์ดํ„ฐ์˜ ์ผ๋ถ€์ผ ๊ฒฝ์šฐ ( '_' ๋‚˜ '%' ๋ฅผ ํฌํ•จํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด์„œ ) ESCAPE ์ ˆ์„ ์‚ฌ์šฉ

-- 1
SELECT * FROM DEPT
WHERE DEPTNO LIKE 'S@_S%' ESCAPE '@';

SELECT * FROM DEPT
WHERE DEPTNO LIKE 'S^_S%' ESCAPE '^';

  (1) WHERE DEPTNO LIKE 'S@_S%' ESCAPE '@' : ๋ถ€์„œ ๋ฒˆํ˜ธ๊ฐ€ S_S๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ๋ถ€์„œ

    =  WHERE DEPTNO LIKE 'S^_S%' ESCAPE '^'

- '@' ๋ฐ”๋กœ ๋’ค์˜ '_'๋ฅผ ์™€์ผ๋“œ ์นด๋“œ๊ฐ€ ์•„๋‹Œ ๋ฐ์ดํ„ฐ์— ํฌํ•จ๋œ ๋ฌธ์ž๋กœ ์ธ์‹ํ•˜๋ผ๋Š” ์˜๋ฏธ

- '@'์™€ '^'๋Œ€์‹  ๋‹ค๋ฅธ ๋ฌธ์ž๋ฅผ ์ง€์ •ํ•ด์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

 

11. IS NULL / IS NOT NULL ์—ฐ์‚ฐ์ž

- ์นผ๋Ÿผ์˜ ๊ฐ’์ด NULL์ธ ๊ฒƒ / NULL์ด ์•„๋‹Œ ๊ฒƒ๋งŒ ์กฐํšŒ

SELECT * FROM EMP WHERE COMM IS NULL;
SELECT * FROM EMP WHERE COMM IS NOT NULL;

 

12. ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž

- ๋‘ ๊ฐœ ์ด์ƒ์˜ SELECT๋ฌธ์˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ์—ฐ๊ฒฐํ•  ๋•Œ ์‚ฌ์šฉ

- ์—ฐ๊ฒฐํ•˜๋ ค๋Š” ๋‘ SELECT๋ฌธ์˜ ์นผ๋Ÿผ ๊ฐœ์ˆ˜์™€ ์ž๋ฃŒํ˜•์ด ๊ฐ™๋‹ค๋ฉด ์„œ๋กœ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์—์„œ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ์กฐํšŒํ•˜๋Š” ์นผ๋Ÿผ ์ด๋ฆ„์ด ๋‹ค๋ฅธ ๊ฒƒ์€ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š์œผ๋ฉฐ ์ตœ์ข… ์ถœ๋ ฅ๋˜๋Š” ์นผ๋Ÿผ ์ด๋ฆ„์€ ๋จผ์ € ์ž‘์„ฑํ•œ SELECT๋ฌธ์˜ ์นผ๋Ÿผ ์ด๋ฆ„์œผ๋กœ ํ‘œ๊ธฐ๋จ

-- 1. UNION
SELECT	EMPNO, ENALE, SAL, DEPTNO	
FROM	EMP
WHERE	DEPTNO = 10
UNION
SELECT	EMPNO, ENALE, SAL, DEPTNO
FROM	EMP
WHERE	DEPTNO = 20;

-- 2. UNION ALL
SELECT	EMPNO, ENALE, SAL, DEPTNO	
FROM	EMP
WHERE	DEPTNO = 10
UNION ALL
SELECT	EMPNO, ENALE, SAL, DEPTNO
FROM	EMP
WHERE	DEPTNO = 20;

-- 3. MINUS
SELECT	EMPNO, ENALE, SAL, DEPTNO	
FROM	EMP
MINUS
SELECT	EMPNO, ENALE, SAL, DEPTNO
FROM	EMP
WHERE	DEPTNO = 20;

-- 4. INTERSECT
SELECT	EMPNO, ENALE, SAL, DEPTNO	
FROM 	EMP
INTERSECT
SELECT	EMPNO, ENALE, SAL, DEPTNO
FROM	EMP
WHERE	DEPTNO = 20;

  (1) UNION : ์—ฐ๊ฒฐ๋œ SELECT๋ฌธ์˜ ๋‘ ๊ฒฐ๊ณผ ๊ฐ’์„ ํ•ฉ์ง‘ํ•ฉ ์ฒ˜๋ฆฌ. ๊ฒฐ๊ณผ ๊ฐ’์˜ ์ค‘๋ณต์€ ์ œ๊ฑฐ

  (2) UNION ALL : ์—ฐ๊ฒฐ๋œ SELECT๋ฌธ์˜ ๋‘ ๊ฒฐ๊ณผ ๊ฐ’์„ ํ•ฉ์ง‘ํ•ฉ ์ฒ˜๋ฆฌ. ๊ฒฐ๊ณผ ๊ฐ’์˜ ์ค‘๋ณต๋„ ๋ชจ๋‘ ์ถœ๋ ฅ

  (3) MINUS : ๋จผ์ € ์ž‘์„ฑํ•œ SELECT๋ฌธ์˜ ๊ฒฐ๊ณผ ๊ฐ’์—์„œ ๋‹ค์Œ SELECT๋ฌธ์˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ์ฐจ์ง‘ํ•ฉ ์ฒ˜๋ฆฌ. ๋จผ์ € ์ž‘์„ฑ๋œ SELECT๋ฌธ์˜ ๊ฒฐ๊ณผ ๊ฐ’ ์ค‘ ๋‹ค์Œ SELECT๋ฌธ์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ถœ๋ ฅ

  (4) INTERSECT : ์—ฐ๊ฒฐ๋œ SELECT๋ฌธ์˜ ๋‘ ๊ฒฐ๊ณผ ๊ฐ’์˜ ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋งŒ ์ถœ๋ ฅ. ๊ต์ง‘ํ•ฉ๊ณผ ๊ฐ™์€ ์˜๋ฏธ

 

13. ์—ฐ์‚ฐ์ž ์šฐ์„ ์ˆœ์œ„



๋†’์Œ






๋‚ฎ์Œ


* , / ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž : ๊ณฑํ•˜๊ธฐ , ๋‚˜๋ˆ„๊ธฐ
+ , - ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž : ๋”ํ•˜๊ธฐ , ๋นผ๊ธฐ
= , != , ^= , <> , > , >= , < , <= ๋Œ€์†Œ ๋น„๊ต ์—ฐ์‚ฐ์ž
IS (NOT) NULL , (NOT) LIKE , (NOT) IN (๊ทธ ์™ธ) ๋น„๊ต ์—ฐ์‚ฐ์ž
BETWEEN A AND B BETWEEN ์—ฐ์‚ฐ์ž
NOT ๋…ผ๋ฆฌ ๋ถ€์ • ์—ฐ์‚ฐ์ž NOT
AND ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž AND
OR ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž OR