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

[SQL] ์„œ๋ธŒ์ฟผ๋ฆฌ subquery

NaNaRin๐Ÿ™ƒ 2021. 3. 11. 11:53

1. ์„œ๋ธŒ์ฟผ๋ฆฌ subquery

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

- ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์˜์—ญ์€ ๋ฉ”์ธ์ฟผ๋ฆฌ main query ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค

SELECT  ์กฐํšŒํ•  ์นผ๋Ÿผ
FROM	์กฐํšŒํ•  ํ…Œ์ด๋ธ”
WHERE 	์กฐ๊ฑด์‹ (	SELECT  ์กฐํšŒํ•  ์นผ๋Ÿผ
				  FROM	์กฐํšŒํ•  ํ…Œ์ด๋ธ”
				  WHERE 	์กฐ๊ฑด์‹ )
                  
-- 1. ์ด๋ฆ„์ด JONES์ธ ์‚ฌ์›์˜ ๊ธ‰์—ฌ ์ถœ๋ ฅํ•˜๊ธฐ
SELECT 	SAL
FROM 	EMP
WHERE	ENAME = 'JONES';

-- 2. ๊ธ‰์—ฌ๊ฐ€ 2975๋ณด๋‹ค ๋†’์€ ์‚ฌ์› ์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ
SELECT  *
FROM 	EMP
WHERE	SAL > 2975;

-- 3. ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ JONES๋ณด๋‹ค ๋†’์€ ๊ธ‰์—ฌ ๋ฐ›๋Š” ์‚ฌ์› ์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ
SELECT  *
FROM 	EMP
WHERE	SAL > ( SELECT 	SAL
				FROM	EMP
				WHERE	ENAME = 'JONES');

  (1) ์‚ฌ์› ์ด๋ฆ„์ด JONES ์ธ ์‚ฌ์›์˜ ๊ธ‰์—ฌ๋Š” 2975

  (2) ๊ธ‰์—ฌ๊ฐ€ 2975๋ณด๋‹ค ๋†’์€ ์‚ฌ์› ์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ

  (3) ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ JONES๋ณด๋‹ค ๋†’์€ ๊ธ‰์—ฌ ๋ฐ›๋Š” ์‚ฌ์› ์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ ( = (1) + (2) )

 

2. ๋‹จ์ผํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ single-row subquery

- ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ๋‹จ ํ•˜๋‚˜์˜ ํ–‰์œผ๋กœ ๋‚˜์˜ค๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ

- ์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ ์ถœ๋ ฅ๋˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ํ•˜๋‚˜์ด๋ฏ€๋กœ ๋ฉ”์ธ์ฟผ๋ฆฌ์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋Š” ๋‹จ์ผํ–‰ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๊ต

- ๋‹จ์ผํ–‰ ์—ฐ์‚ฐ์ž ( '>' : ์ดˆ๊ณผ  |  '>=' : ์ด์ƒ  |  '=' : ๊ฐ™์Œ  | '<=' : ์ดํ•˜  |  '<' : ๋ฏธ๋งŒ  |  '<>' '^=' '!=' : ๊ฐ™์ง€ ์•Š์Œ ) 

- ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์˜ˆ์‹œ๋กœ ์‚ฌ์šฉ๋œ JONES๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋‹จ์ผํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ด๋ฆ„์ด ์ค‘๋ณต์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜์ค‘์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค

-- 1. ๋‚ ์งœํ˜• ๋ฐ์ดํ„ฐ
SELECT	*
FROM	EMP
WHERE	HIREDATE < ( SELECT HIREDATE
					   FROM EMP
                   	  WHERE ENAME = 'SCOTT' );
                      
-- 2. ๋‹จ์ผํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ์™€ ํ•จ์ˆ˜
SELECT E.EMPNO, E.ENAME, E.JOB, E.SAL, D.DEPTNO, D.DNAME, D.LOC
  FROM EMP E, DEPT D
 WHERE E.DEPTNO = D.DEPTNO
   AND E.DEPTNO = 20
   AND E.SAL < ( SELECT AVG(SAL)
 				   FROM EMP );

  (1) ์„œ๋ธŒ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ๊ฐ’์ด ๋‚ ์งœ(DATE) ์ž๋ฃŒํ˜•์ผ ๋•Œ๋„ ๋‹จ์ผํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

  (2) ์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ ํŠน์ • ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฐ๊ณผ ๊ฐ’์ด ํ•˜๋‚˜์ผ ๋•Œ ๋‹จ์ผํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

 

3. ๋‹ค์ค‘ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ multiple-row subquery

- ์‹คํ–‰ ๊ฒฐ๊ณผ ํ–‰์ด ์—ฌ๋Ÿฌ ๊ฐœ๋กœ ๋‚˜์˜ค๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ

- ๋‹จ์ผํ–‰ ์—ฐ์‚ฐ์ž๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ณ  ๋‹ค์ค‘ํ–‰ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ๋ฉ”์ธ์ฟผ๋ฆฌ๋ž‘ ๋น„๊ต๊ฐ€ ๊ฐ€๋Šฅ

 

  3-1. ๋‹ค์ค‘ํ–‰ ์—ฐ์‚ฐ์ž IN

- ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ผ์น˜ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๋ฉด true

- WHERE ์ ˆ์˜ ์—ฐ์‚ฐ์ž IN๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค

-- 1.
SELECT *
  FROM EMP
 WHERE DEPTNO IN (20, 30);
 
 -- 2.
 SELECT *
  FROM EMP
 WHERE SAL IN ( SELECT MAX(SAL)
 				  FROM EMP
              GROUP BY DEPTNO );

  (1) DEPTNO ๊ฐ€ 20์ด๊ฑฐ๋‚˜ 30์ธ EMP ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰ ์ถœ๋ ฅ

  (2) SAL ์ด EMP ํ…Œ์ด๋ธ”์˜ ๊ฐ DEPTNO ๋งˆ๋‹ค์˜ MAX๊ฐ’๊ณผ ๊ฐ™์€ EMP ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰ ์ถœ๋ ฅ

 

  3-2. ๋‹ค์ค‘ํ–‰ ์—ฐ์‚ฐ์ž ANY, SOME

- ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ์กฐ๊ฑด์‹์„ ๋งŒ์กฑํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ํ•˜๋‚˜ ์ด์ƒ์ด๋ฉด true

-- 1. = ANY ์—ฐ์‚ฐ์ž ์‚ฌ์šฉํ•˜๊ธฐ
SELECT *
  FROM EMP
 WHERE SAL = ANY ( SELECT MAX(SAL)
 					 FROM EMP
                 GROUP BY DEPTNO );
                 
-- 2. = SOME ์—ฐ์‚ฐ์ž ์‚ฌ์šฉํ•˜๊ธฐ
SELECT *
  FROM EMP
 WHERE SAL = SOME ( SELECT MAX(SAL)
 			 		  FROM EMP
                  GROUP BY DEPTNO );

  (1)(2) ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ’ ์ค‘ ํ•˜๋‚˜๋งŒ ์กฐ๊ฑด์‹์„ ๋งŒ์กฑํ•˜๋ฉด ์ถœ๋ ฅ๋œ๋‹ค. ANY ์—ฐ์‚ฐ์ž์™€ SOME ์—ฐ์‚ฐ์ž๋ฅผ ๋“ฑ๊ฐ€ ๋น„๊ต ์—ฐ์‚ฐ์ž(=)์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด IN ์—ฐ์‚ฐ์ž์™€ ์ •ํ™•ํžˆ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰

-- 3. DEPTNO ๊ฐ€ 30์ธ ๋ถ€์„œ ์‚ฌ์›๋“ค์˜ ๊ธ‰์—ฌ๋ณด๋‹ค ์ ์€ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์‚ฌ์› ์ •๋ณด ์ถœ๋ ฅ 
SELECT	  *
  FROM	  EMP
 WHERE 	  SAL < ANY ( SELECT SAL
 				        FROM EMP
                       WHERE DEPTNO = 30 )
 ORDER BY SAL, DEPTNO;            

-- 4.
SELECT    *
  FROM    EMP
 WHERE    SAL < ( SELECT MAX(SAL)
 			     FROM EMP
             GROUP BY DEPTNO )
 ORDER BY SAL, DEPTNO;  
 
 -- 5. DEPTNO ๊ฐ€ 30์ธ ๋ถ€์„œ ์‚ฌ์›๋“ค์˜ ๊ธ‰์—ฌ๋ณด๋‹ค ๋งŽ์€ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์‚ฌ์› ์ •๋ณด ์ถœ๋ ฅ 
SELECT	  *
  FROM	  EMP
 WHERE 	  SAL > ANY ( SELECT SAL
 				        FROM EMP
                       WHERE DEPTNO = 30 )
 ORDER BY SAL, DEPTNO;            

  (3) < ANY ์—ฐ์‚ฐ์ž๋Š” ๋ฉ”์ธ ์ฟผ๋ฆฌ์˜ ์กฐ๊ฑด์‹์ธ SAL ๊ฐ’์ด ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ๊ฐ’์ธ DEPTNO๊ฐ€ 30์ธ ๋ถ€์„œ ์‚ฌ์›๋“ค์˜ ๊ธ‰์—ฌ๋“ค ๋ณด๋‹ค ์ ์œผ๋ฉด true ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅ๋œ๋‹ค. ์ฆ‰ SAL ๊ฐ’์ด ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ๊ฐ’ ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ์ž‘์œผ๋ฉด ์ถœ๋ ฅํ•œ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ 3000, 2800, 2500 ์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ 2500๋ณด๋‹ค ๋งŽ๋”๋ผ๋„ 3000๋ณด๋‹ค๋งŒ ์ž‘์œผ๋ฉด ANY ์—ฐ์‚ฐ์ž์˜ ์ •์˜์— ๋”ฐ๋ผ true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ

  (4) (3)๋ฒˆ์— ์˜ํ•ด < ANY ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ANY ์—ฐ์‚ฐ์ž ์—†์ด < ์—ฐ์‚ฐ์ž์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ MAX ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™๋‹ค

  (5) > ANY ์—ฐ์‚ฐ์ž๋Š” ๋ฉ”์ธ ์ฟผ๋ฆฌ์˜ ์กฐ๊ฑด์‹์ธ SAL ๊ฐ’์ด ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ๊ฐ’์ธ DEPTNO๊ฐ€ 30์ธ ๋ถ€์„œ ์‚ฌ์›๋“ค์˜ ๊ธ‰์—ฌ๋“ค ๋ณด๋‹ค ๋งŽ์œผ๋ฉด true ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅ๋œ๋‹ค. ์ฆ‰ SAL ๊ฐ’์ด ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ๊ฐ’ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ์ž‘์œผ๋ฉด ์ถœ๋ ฅํ•œ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ 3000, 2800, 2500 ์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ 3000๋ณด๋‹ค ์ž‘๋”๋ผ๋„ 2500๋ณด๋‹ค๋งŒ ํฌ๋ฉด ANY ์—ฐ์‚ฐ์ž์˜ ์ •์˜์— ๋”ฐ๋ผ true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ

 

  3-3. ๋‹ค์ค‘ํ–‰ ์—ฐ์‚ฐ์ž ALL

- ANY, SOME ์—ฐ์‚ฐ์ž์™€ ๋‹ฌ๋ฆฌ ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ์กฐ๊ฑด์‹์„ ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ๋ชจ๋‘๊ฐ€ ๋งŒ์กฑํ•˜๋ฉด true

-- 1. DEPTNO ๊ฐ€ 30์ธ ๋ถ€์„œ ์‚ฌ์›๋“ค์˜ ๊ธ‰์—ฌ๋ณด๋‹ค ์ ์€ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์‚ฌ์› ์ •๋ณด ์ถœ๋ ฅ 
SELECT	  *
  FROM	  EMP
 WHERE 	  SAL < ALL ( SELECT SAL
 				        FROM EMP
                       WHERE DEPTNO = 30 );    

-- 2. DEPTNO ๊ฐ€ 30์ธ ๋ถ€์„œ ์‚ฌ์›๋“ค์˜ ๊ธ‰์—ฌ๋ณด๋‹ค ๋งŽ์€ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์‚ฌ์› ์ •๋ณด ์ถœ๋ ฅ 
SELECT	  *
  FROM	  EMP
 WHERE 	  SAL > ALL ( SELECT SAL
 				        FROM EMP
                       WHERE DEPTNO = 30 );             

  (1) < ALL ์—ฐ์‚ฐ์ž๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๋ชจ๋“  ๊ฒฐ๊ณผ๊ฐ’๋ณด๋‹ค ์ž‘์€ ๊ฐ’์„ ๊ฐ€์ง„ ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ํ–‰๋งŒ ์ถœ๋ ฅ๋œ๋‹ค. ์ฆ‰ ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ์ค‘ ์ตœ์†Ÿ๊ฐ’๋ณด๋‹ค ์ž‘์€ ๊ฐ’์„ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ๋งŒ ์ถœ๋ ฅ๋œ๋‹ค

  (2)  > ALL ์—ฐ์‚ฐ์ž๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๋ชจ๋“  ๊ฒฐ๊ณผ๊ฐ’๋ณด๋‹ค ํฐ ๊ฐ’์„ ๊ฐ€์ง„ ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ํ–‰๋งŒ ์ถœ๋ ฅ๋œ๋‹ค. ์ฆ‰ ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ์ค‘ ์ตœ๋Œ“๊ฐ’๋ณด๋‹ค ํฐ ๊ฐ’์„ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ๋งŒ ์ถœ๋ ฅ๋œ๋‹ค

 

  3-4. ๋‹ค์ค‘ํ–‰ ์—ฐ์‚ฐ์ž EXISTS

- ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์กด์žฌํ•˜๋ฉด ( ์ฆ‰, ํ–‰์ด 1๊ฐœ ์ด์ƒ์ผ ๊ฒฝ์šฐ ) true

- ํŠน์ • ์„œ๋ธŒ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ๊ฐ’์˜ ์กด์žฌ ์œ ๋ฌด๋ฅผ ํ†ตํ•ด ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ๋ฐ์ดํ„ฐ ๋…ธ์ถœ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉ

 

4. ๋‹ค์ค‘์—ด ์„œ๋ธŒ์ฟผ๋ฆฌ multiple-column subquery

- ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ SELECT ์ ˆ์— ๋น„๊ตํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์ง€์ •ํ•˜๋Š” ๋ฐฉ์‹

- ๋ฉ”์ธ์ฟผ๋ฆฌ์— ๋น„๊ตํ•  ์—ด์„ ๊ด„ํ˜ธ๋กœ ๋ฌถ์–ด ๋ช…์‹œํ•˜๊ณ  ์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ๋Š” ๊ด„ํ˜ธ๋กœ ๋ฌถ์€ ๋ฐ์ดํ„ฐ์™€ ๊ฐ™์€ ์ž๋ฃŒํ˜• ๋ฐ์ดํ„ฐ๋ฅผ SELECT ์ ˆ์— ๋ช…์‹œํ•˜์—ฌ ์‚ฌ์šฉ

-- 1.
SELECT *
  FROM EMP
 WHERE (DEPTNO, SAL) IN (SELECT DEPTNO, MAX(SAL)
                           FROM EMP
					   GROUP BY DEPTNO );

  (1) ๋ฉ”์ธ์ฟผ๋ฆฌ ์กฐ๊ฑด์ ˆ์˜ (DEPTNO, SAL) ๊ณผ ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ SELECT ์ ˆ์˜ DEPTNO, MAX(SAL) ์ด ๊ฐ™์€ ์ž๋ฃŒํ˜•

 

5. FROM ์ ˆ์— ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ : ์ธ๋ผ์ธ ๋ทฐ inline view ์™€ WITH ์ ˆ

- FROM ์ ˆ์— ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ์ธ๋ผ์ธ ๋ทฐ inline view ๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค

- FROM ์ ˆ์— ์ง์ ‘ ํ…Œ์ด๋ธ”์„ ๋ช…์‹œํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ์—๋Š” ํ…Œ์ด๋ธ” ๋‚ด ๋ฐ์ดํ„ฐ ๊ทœ๋ชจ๊ฐ€ ๋„ˆ๋ฌด ํฌ๊ฑฐ๋‚˜ ํ˜„์žฌ ์ž‘์—…์— ๋ถˆํ•„์š”ํ•œ ์นผ๋Ÿผ์ด ๋งŽ์•„ ์ผ๋ถ€ ํ–‰๊ณผ ์นผ๋Ÿผ๋งŒ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•  ๋•Œ ์œ ์šฉ

- ์ธ๋ผ์ธ ๋ทฐ๋Š” ํŠน์ • ํ…Œ์ด๋ธ” ์ „์ฒด ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹Œ SELECT ๋ฌธ์„ ํ†ตํ•ด ์ผ๋ถ€ ๋ฐ์ดํ„ฐ๋ฅผ ๋จผ์ € ์ถ”์ถœํ•ด ์˜จ ํ›„ ๋ณ„์นญ์„ ์ฃผ์–ด ์‚ฌ์šฉ

- FROM ์ ˆ์— ๋„ˆ๋ฌด ๋งŽ์€ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ง€์ •ํ•˜๋ฉด ๊ฐ€๋…์„ฑ์ด๋‚˜ ์„ฑ๋Šฅ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฝ์šฐ์— ๋”ฐ๋ผ WITH ์ ˆ ์‚ฌ์šฉ

- WITH ์ ˆ์€ ๋ฉ”์ธ์ฟผ๋ฆฌ๊ฐ€ ๋  SELECT๋ฌธ ์•ˆ์—์„œ ์‚ฌ์šฉํ•  ์„œ๋ธŒ์ฟผ๋ฆฌ์™€ ๋ณ„์นญ์„ ๋จผ์ € ์ง€์ •ํ•œ ํ›„ ๋ฉ”์ธ์ฟผ๋ฆฌ์—์„œ ์‚ฌ์šฉ

WITH
[๋ณ„์นญ 1] AS (SELECT๋ฌธ 1),
[๋ณ„์นญ 2] AS (SELECT๋ฌธ 2),
…
[๋ณ„์นญ n] AS (SELECT๋ฌธ n),
SELECT
  FROM ๋ณ„์นญ 1, ๋ณ„์นญ 2, … , ๋ณ„์นญ n
…

-- 1. ์ธ๋ผ์ธ ๋ทฐ ์‚ฌ์šฉํ•˜๊ธฐ
SELECT E10.EMONO, E10.ENAME, E10.DEPTNO, D.DNAME, D.LOC
  FROM (SELECT * FROM EMP WHERE DEPTNO = 10) E10,
  	   (SELECT * FROM DEPT) D
 WHERE E10.DEPTNO = D.DEPTNO;

-- 2. WITH ์ ˆ ์‚ฌ์šฉํ•˜๊ธฐ
WITH
E10 AS (SELECT * FROM EMP WHERE DEPTNO = 10),
D   AS (SELECT * FROM DEPT)
SELECT E10.EMONO, E10.ENAME, E10.DEPTNO, D.DNAME, D.LOC
  FROM E10, D
 WHERE E10.DEPTNO = D.DEPTNO;

  (1) ์ธ๋ผ์ธ ๋ทฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ FROM ์ ˆ ์•ˆ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉ

  (2) WITH ์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”์ธ์ฟผ๋ฆฌ ์‹œ์ž‘ ์ „์— ์„œ๋ธŒ์ฟผ๋ฆฌ์™€ ๋ณ„์นญ์„ ๋ฏธ๋ฆฌ ์ง€์ •

 

6. SELECT ์ ˆ์— ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ : ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ scalar subquery

- SELECT ์ ˆ์— ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ scalar subquery ๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค

- SELECT ์ ˆ์— ํ•˜๋‚˜์˜ ์นผ๋Ÿผ ์˜์—ญ์œผ๋กœ์„œ ๊ฒฐ๊ณผ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅ

- ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋งŒ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค

SELECT EMPNO, ENAME, JOB, SAL,
	   ( SELECT GRADE
       	   FROM SALGRADE
          WHERE E.SAL BETWEEN LOSAL AND HISAL ) AS SALGRADE,
       DEPTNO,
       ( SELECT DNAME
           FROM DEPT
          WHERE E.DEPTNO = DEPT.DEPTNO ) AS DNAME
  FROM EMP E;