๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ด๋ก /DB

[๋ฌด์กฐ๊ฑด ๋”ฐ๋ผํ•ด๋ณด๊ธฐ] DML ๊ทธ๋ฃนํ•จ์ˆ˜, HAVING, JOIN, SET, ์„œ๋ธŒ์ฟผ๋ฆฌ

by 6161990 2021. 5. 25.

๐Ÿ“Œ DML(Data Manipulation Language(๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด))

  • SELECT : ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ช…๋ น๋ฌธ , ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์ด๋‚˜ ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ณ  ์‰ฝ๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ


๐Ÿ“ ๊ทธ๋ฃนํ•จ์ˆ˜, SELECT ๋ฌธ์„ ํ†ตํ•ด ๊ฐ€์ ธ์˜ฌ ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋ฃน์œผ๋กœ ๋ฌต๊ณ  ๊ทธ๋ฃน ๋‚ด์—์„œ ์ง€์ •๋œ ์ปฌ๋Ÿผ์˜ ์ดํ•ฉ, ํ‰๊ท  ๋“ฑ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜

  • SUM : ์ดํ•ฉ
  • AVG : ํ‰๊ท 
  • COUNT : ๋กœ์šฐ์˜ ์ˆ˜
  • MAX : ์ตœ๋Œ€๊ฐ’
  • MIN : ์ตœ์†Œ๊ฐ’

 

--์‚ฌ์›๋“ค์˜ ๊ธ‰์—ฌ ์ดํ•ฉ์„ ๊ตฌํ•œ๋‹ค.

SELECT SUM(SAL) FROM EMP;

--๊ทธ๋ฃนํ•จ์ˆ˜(1์ค„) ๋‹ค๋ฅธ ์ปฌ๋Ÿผ(1์ค„์ด์ƒ ๋‚˜์˜ด) ์„ ๋ฌต๋Š” ๊ฒƒ์€ ERROR 'NOT A SINGLE-GROUP FUNCTION'

SELECT EMPNO, SUM(SAL) FROM EMP; --ERROR

 

 

--๊ธ‰์—ฌ๊ฐ€ 500 ์ด์ƒ์ธ ์‚ฌ์›๋“ค์˜ ๊ธ‰์—ฌ ์ดํ•ฉ์„ ๊ตฌํ•œ๋‹ค.

SELECT SUM(SAL) FROM EMP WHERE SAL >= 500; --520

--์—ฐ๊ทนํŒ€์— ๊ทผ๋ฌดํ•˜๊ณ  ์žˆ๋Š” ์‚ฌ์›๋“ค์˜ ๊ธ‰์—ฌ ์ดํ•ฉ์„ ๊ตฌํ•œ๋‹ค.

SELECT SUM(SAL) FROM EMP WHERE JOB='์—ฐ๊ทนํŒ€';

 

 

--์ „ ์‚ฌ์›์˜ ๊ธ‰์—ฌ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค.

SELECT AVG(SAL) FROM EMP; SELECT TRUNC(AVG(SAL)) FROM EMP; --์†Œ์ˆ˜์ ๋ฒ„๋ฆฌ๊ธฐ

 

--์ปค๋ฏธ์…˜์„ ๋ฐ›๋Š” ์‚ฌ์›๋“ค์˜ ์ปค๋ฏธ์…˜ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค.

--( ์ปค๋ฏธ์…˜ ๋ฐ›๋Š” ์‚ฌ์› 9๋ช…, ๊ทธ๋ฃนํ•จ์ˆ˜์—์„œ NULL(4๋ช…)์€ ๋น ์ง€๊ณ  ๊ณ„์‚ฐ๋œ๋‹ค)

SELECT COMM FROM EMP; SELECT TRUNC(AVG(COMM)) FROM EMP;

 

 

-- ์ „ ์‚ฌ์›์˜ ์ปค๋ฏธ์…˜์˜ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค. ์ „ ์‚ฌ์› 13๋ช…

SELECT TRUNC(AVG(NVL(COMM,0))) FROM EMP;

 

-- ์ปค๋ฏธ์…˜์„ ๋ฐ›๋Š” ์‚ฌ์›๋“ค์˜ ๊ธ‰์—ฌ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค.

SELECT TRUNC(AVG(SAL)) FROM EMP WHERE COMM IS NOT NULL;

 

 

--์‚ฌ์›๋“ค์˜ ์ด ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค. ๋‘ ๊ฒฐ๊ณผ๋Š” ๊ฐ™์Œ

SELECT COUNT(EMPNO) FROM EMP; SELECT COUNT(*) FROM EMP;

 

--์‚ฌ์›๋“ค์˜ ๊ธ‰์—ฌ ์ตœ๋Œ€, ์ตœ์†Œ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.

SELECT MAX(SAL), MIN(sAL) FROM EMP;


๐Ÿ“ GROUP BY, ๊ทธ๋ฃนํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, SELECT ~ FROM ~ WHERE ์ ˆ๊นŒ์ง€ ๋ชจ๋‘ ์ˆ˜ํ–‰ํ•˜์—ฌ ๊ฐ€์ ธ์˜จ ๊ฒฐ๊ณผ๋ฅผ ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์–ด ์ดํ•ฉ, ํ‰๊ท  ๋“ฑ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

  • GROUP BY ์ ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด SELECT๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ๊ฐ€์ ธ์˜จ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์—ฌ๋Ÿฌ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆ  ๊ทธ๋ฃน ๊ฐ๊ฐ์˜ ์ดํ•ฉ๊ณผ ํ‰๊ท  ๋“ฑ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์กฐ๊ฑด์ ˆ GROUP BY ๊ทธ๋ฃน๊ธฐ์ค€ ORDER BY ์ •๋ ฌ๊ธฐ์ค€

 

-- ๊ฐ ๋ถ€์„œ๋ณ„ ์‚ฌ์›๋“ค์˜ ๊ธ‰์—ฌ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค.

SELECT JOB, SUM(SAL) FROM EMP GROUP BY JOB;

 

 

 

--300 ์ด์ƒ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์‚ฌ์›๋“ค์˜ ๋ถ€์„œ๋ณ„ ๊ธ‰์—ฌ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค.

SELECT DEPTNO, AVG(SAL) FROM EMP WHERE SAL >= 300 GROUP BY DEPTNO;


๐Ÿ“ HAVING

  • GROUP BY๋กœ ๋ฌถ์ธ ๊ฐ ๊ทธ๋ฃน๋“ค ์ค‘์— ์‹ค์ œ ๊ฐ€์ ธ์˜ฌ ๊ทธ๋ฃน์„ ์„ ํƒํ•  ์กฐ๊ฑด์„ HAVING ์ ˆ์— ์ž‘์„ฑํ•œ๋‹ค.
  • HAVING์€ GROUP BY ์ ˆ์˜ ์กฐ๊ฑด์ด ๋œ๋‹ค.
  • WHERE์€ ROW์˜ ์กฐ๊ฑด, HAVING์€ GROUP BY์˜ ์กฐ๊ฑด

 

--๋ถ€์„œ๋ณ„ ํ‰๊ท  ๊ธ‰์—ฌ๊ฐ€ 400์ด์ƒ์ธ ๋ถ€์„œ์˜ ๊ธ‰์—ฌ ํ‰๊ท ์„ ๊ฐ€์ ธ์˜จ๋‹ค.

SELECT DEPTNO, AVG(SAL) FROM EMP GROUP BY DEPTNO HAVING AVG(SAL) >= 400;

 

 

--๋ถ€์„œ๋ณ„ ์ตœ๋Œ€ ๊ธ‰์—ฌ์•ก์ด 300์ดํ•˜์ธ ๋ถ€์„œ์˜ ๊ธ‰์—ฌ ์ดํ•ฉ์„ ๊ฐ€์ ธ์˜จ๋‹ค.

SELECT SUM(SAL) FROM EMP GROUP BY DEPTNO HAVING MAX(SAL) >= 300;

 

 

--๋ถ€์„œ๋ณ„ ์ตœ์†Œ ๊ธ‰์—ฌ์•ก์ด 300์ดํ•˜์ธ ๋ถ€์„œ์—์„œ ์ง๋ฌด๊ฐ€ ๋งˆ์ผ€ํŒ…ํŒ€์ธ ์‚ฌ์›๋“ค์˜ ๊ธ‰์—ฌ ์ดํ•ฉ์„ ๊ตฌํ•œ๋‹ค.

SELECT SUM(SAL) FROM EMP WHERE JOB='๋งˆ์ผ€ํŒ…ํŒ€' GROUP BY DEPTNO HAVING MIN(SAL) <=300;


๐Ÿ“ JOIN

  • ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ์ปฌ๋Ÿผ์˜ ๊ฐ’์„ ํ•œ๋ฒˆ์— ๊ฐ€์ ธ์˜ค๊ธฐ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์กฐ์ธ์ด๋‹ค.
  • SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ” 1, ํ…Œ์ด๋ธ” 2 ;
  • ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•˜๊ฒŒ ๋˜๋ฉด ๋‹ค๋Œ€๋‹ค์˜ ๊ด€๊ณ„๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ํ…Œ์ด๋ธ” 1์˜ ๋กœ์šฐ์˜ ์ˆ˜ X ํ…Œ์ด๋ธ” 2์˜ ๋กœ์šฐ์ด ์ˆ˜ ๋งŒํผ ๋กœ์šฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋œ๋‹ค.
  • ์กฐ๊ฑด : ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์—์„œ ๊ฐ€์ ธ์˜จ ๊ฒฐ๊ณผ ์ค‘์— ์ •ํ™•ํ•œ ๊ฒฐ๊ณผ๋ฉด ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด ๊ณตํ†ต๋ถ€ ๋ถ€๋ถ„์„ ์ด์šฉํ•œ ์กฐ๊ฑด๋ฌธ์ด ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•˜๋‹ค

 

--๊ณตํ†ต๋ถ€ DEPTNO

SELECT * FROM EMP, DEPT WHERE EMP.DEPTNO = DEPT.DEPTNO;

SELECT * FROM EMP A1, DEPT A2 WHERE A1.DEPTNO = A2.DEPTNO; --๋ณ„์นญ์‚ฌ์šฉ

 

 

--์‚ฌ์›์˜ ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„, ๊ทผ๋ฌด๋ถ€์„œ ์ด๋ฆ„์„ ๊ฐ€์ ธ์˜จ๋‹ค.

SELECT A1.EMPNO, AL.ENAME, A2.DNAME FROM EMP A1, DEPT A2 WHERE A1.DEPTNO = A2.DEPTNO;

 

--DALLAS์— ๊ทผ๋ฌดํ•˜๊ณ  ์žˆ๋Š” ์‚ฌ์›๋“ค์˜ ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„, ์ง๋ฌด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

SELECT A1.EMPNO, AL.ENAME, A1.JOB FROM EMP A1, DEPT A2 WHERE A1.DEPTNO = A2.DEPTNO AND A2.LOC = 'DALLAS';

 

--๊ฐ ์‚ฌ์›๋“ค์˜ ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„, ๊ธ‰์—ฌ, ๊ธ‰์—ฌ๋“ฑ๊ธ‰์„ ๊ฐ€์ ธ์˜จ๋‹ค.

SELECT A1.EMPNO, A1.ENAME, A1.SAL, A2.GRADE FROM EMP A1, SALGRADE A2 WHERE A1.SAL BETWEEN A2.LOSAL AND A2.HISAL;

 

--SALES ๋ถ€์„œ์— ๊ทผ๋ฌดํ•˜๊ณ  ์žˆ๋Š” ์‚ฌ์›์˜ ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„, ๊ธ‰์—ฌ๋“ฑ๊ธ‰์„ ๊ฐ€์ ธ์˜จ๋‹ค.

SELECT A1.EMPNO, A1.ENAME, A2.GRADE FROM EMP A1, SALGRADE A2, DEPT A3 WHERE A1.SAL BETWEEN A2.LOSAL AND A2.HISAL AND A1.DEPTNO = A3.DEPTNO AND A3.DNAME ='SALES';


๐Ÿ“ SELF JOIN , ๊ฐ™์€ ํ…Œ์ด๋ธ”์„ ๋‘ ๋ฒˆ ์ด์ƒ ์กฐ์ธํ•˜๋Š” ๊ฒƒ.

--SMITH ์‚ฌ์›์˜ ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„, ์ง์†์ƒ๊ด€ ์ด๋ฆ„์„ ๊ฐ€์ ธ์˜จ๋‹ค.

-- A1 ; SMITH ์‚ฌ์›์˜ ์ •๋ณด

-- A2 ; ์ง์†์ƒ๊ด€์˜ ์ •๋ณด

SELECT A1.EMPNO, A1.ENAME, A2.ENAME FROM EMP A1, EMP A2 WHERE A1.MGR = A2.EMPNO AND A1.ENAME='SMITH';

 

 

--FORD ์‚ฌ์› ๋ฐ‘์—์„œ ์ผํ•˜๋Š” ์‚ฌ์›๋“ค์˜ ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„ , ์ง๋ฌด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

-- A1 ; FORD ์˜ ์ •๋ณด

-- A2 ; ๋ถ€ํ•˜ ์ง์›์˜ ์ •๋ณด SELECT A2.EMPNO, A2.ENAME, A2.JOB FROM EMP A1, EMP A2 WHERE A1.EMPNO = A2.MGR AND A1.ENAME='FORD';


๐Ÿ“ OUTER JOIN : ์กฐ์ธ ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ๋กœ์šฐ๊นŒ์ง€ ๊ฐ€์ ธ์˜ค๋Š” ์กฐ์ธ

--๊ฐ ์‚ฌ์›์˜ ์ด๋ฆ„, ์‚ฌ์›๋ฒˆํ˜ธ, ์ง์žฅ์ƒ์‚ฌ ์ด๋ฆ„์„ ๊ฐ€์ ธ์˜จ๋‹ค. ๋‹จ ์ง์† ์ƒ๊ด€์ด ์—†๋Š” ์‚ฌ์›๋„ ๊ฐ€์ ธ์˜จ๋‹ค.

-- A1 ; ๊ฐ ์‚ฌ์›์˜ ์ •๋ณด

-- A2 ; ์ง์žฅ์ƒ์‚ฌ์˜ ์ •๋ณด

SELECT A1.ENAME, A1.EMPNO, A2.ENAME FROM EMP A1, EMP A2 WHERE A1.MGR = A2.EMPNO(+);

--์ง์†์ƒ๊ด€์ด ์—†๋Š” ์ชฝ(NULL)์— +


๐Ÿ“ ์„œ๋ธŒ์ฟผ๋ฆฌ, ์ฟผ๋ฆฌ๋ฌธ ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” ์ฟผ๋ฆฌ๋ฌธ. ์ฟผ๋ฆฌ๋ฌธ ์ž‘์„ฑ์‹œ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ’์„ ๋‹ค๋ฅธ ์ฟผ๋ฆฌ๋ฌธ์„ ํ†ตํ•ด ๊ตฌํ•ด์•ผ ํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

--SCOTT ์‚ฌ์›์ด ๊ทผ๋ฌดํ•˜๊ณ  ์žˆ๋Š” ๋ถ€์„œ์˜ ์ด๋ฆ„์„ ๊ฐ€์ ธ์˜จ๋‹ค.

SELECT DNAME FROM DEPT WHERE DEPTNO = (SELECT DEPTNO FROM EMP WHERE ENAME = 'SCOTT');

SELECT A2.DNAME FROM AMP A1, DEPT A2 WHERE A1.DEPTNO = A2.DEPTNO AND A1.ENAME = 'SCOTT';

--JOIN๋ฌธ์„ ์ด์šฉํ•ด๋„๋จ

 

 

--MARTIN๊ณผ ๊ฐ™์€ ์ง๋ฌด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์‚ฌ์›๋“ค์˜ ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„, ์ง๋ฌด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

SELECT EMPNO, ENAME, JOB FROM EMP WHERE JOB =(SELECT JOB FROM EMP WHERE ENAME ='MARTIN');

 

 

--CHICAGO ์ง€์—ญ์— ๊ทผ๋ฌดํ•˜๋Š” ์‚ฌ์›๋“ค ์ค‘ BLAKE๊ฐ€ ์ง์†์ƒ๊ด€์ธ ์‚ฌ์›๋“ค์˜ ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„, ์ง๋ฌด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

SELECT EMPNO, ENAME, JOB FROM EMP WHERE DEPTNO = (SELECT DEPTNO FROM DEPT WHERE LOC='CHICAGO') AND MGR = (SELECT EMPNO FROM EMP WHERE ENAME ='BLAKE');

'


๐Ÿ“ ๊ฒฐ๊ณผ๊ฐ€ ํ•˜๋‚˜ ์ด์ƒ์ธ ์„œ๋ธŒ์ฟผ๋ฆฌ ์—ฐ์‚ฐ์ž ์ด์šฉ : ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ํ†ตํ•ด ๊ฐ€์ ธ์˜จ ๊ฒฐ๊ณผ๊ฐ€ ํ•˜๋‚˜ ์ด์ƒ์ธ ๊ฒฝ์šฐ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ ๋งŒ์กฑํ•˜๊ฑฐ๋‚˜ ๊ฒฐ๊ณผ ์ค‘ ํ•˜๋‚˜๋งŒ ๋งŒ์กฑํ•˜๊ฑฐ๋‚˜ ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ

  • IN : ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ผ์น˜ํ•˜๋ฉด ์กฐ๊ฑด์€ ์ฐธ์ด ๋œ๋‹ค.
  • ANY, SOME : ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ์™€ ํ•˜๋‚˜์ด์ƒ ์ผ์น˜ํ•˜๋ฉด ์กฐ๊ฑด์€ ์ฐธ์ด๋œ๋‹ค.
  • ALL : ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ์™€ ๋ชจ๋‘ ์ผ์น˜ํ•ด์•ผ ์กฐ๊ฑด์€ ์ฐธ์ด๋œ๋‹ค.

 

-- 3000 ์ด์ƒ์˜ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์‚ฌ์›๋“ค๊ณผ ๊ฐ™์€ ๋ถ€์„œ์— ๊ทผ๋ฌดํ•˜๊ณ  ์žˆ๋Š” ์‚ฌ์›์˜ ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„, ๊ธ‰์—ฌ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

--ERROR QUERY. WHY? ๋’ค์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ๊ฒฐ๊ณผ๊ฐ€ 2๊ฐœ ์ด์ƒ์ด ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ์ค‘ ์–ด๋–ค ํ•˜๋‚˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ WHEREํ•ด์•ผํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์—†๊ธฐ์—

SELECT EMPNO, ENAME, SAL FROM EMP WHERE DEPTNO =( SELECT DEPTNO FROM EMP WHERE SAL >= 3000);

--์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ด์•ผํ•จ IN

SELECT EMPNO, ENAME, SAL FROM EMP WHERE DEPTNO IN ( SELECT DEPTNO FROM EMP WHERE SAL >= 3000);

 

 

 

--๊ฐ ๋ถ€์„œ๋ณ„ ๊ธ‰์—ฌ ํ‰๊ท ๋ณด๋‹ค ๋” ๋งŽ์ด ๋ฐ›๋Š” ์‚ฌ์›์˜ ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„, ๊ธ‰์—ฌ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

--๊ฐ DEPTNO์˜ ํ‰๊ท  ๊ธ‰์—ฌ๋ฅผ ๊ตฌํ•œ ํ›„ , ๋ชจ๋“  ๋ถ€์„œ ํ‰๊ท ๋ณด๋‹ค ๊ธ‰์—ฌ๊ฐ€ ๋†’์€ ์‚ฌ์›์„ ๊ฐ€์ ธ์™€์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ALL

SELECT EMPNO, ENAME, SAL FROM EMP WHERE SAL > ALL (SELECT AVG(SAL) FROM EMP GROUP BY DEPTNO);

-- ์ฆ‰, ๋ชจ๋“  ๋ถ€์„œ์—์„œ ์ตœ๊ณ  ๊ธ‰์—ฌ ํ‰๊ท  ๊ฐ’๋ณด๋‹ค ๋†’์€ ์‚ฌ์›์„ ๊ตฌํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ MAX

SELECT EMPNO, ENAME, SAL FROM EMP WHERE SAL > (SELECT MAX(AVG(SAL)) FROM EMP GROUP BY DEPTNO);

-- ๊ฐ ๋ถ€์„œ๋ณ„ ๊ธ‰์—ฌ ํ‰๊ท ๋ณด๋‹ค ๋” ์ ๊ฒŒ ๋ฐ›๋Š” ์‚ฌ์›์˜ ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„, ๊ธ‰์—ฌ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค. MIN

SELECT EMPNO, ENAME, SAL FROM EMP WHERE SAL < (SELECT MIN(AVG(SAL)) FROM EMP GROUP BY DEPTNO);

 

 

 

-- DALLAS์— ๊ทผ๋ฌดํ•˜๊ณ  ์žˆ๋Š” ์‚ฌ์›๋“ค ์ค‘ ๊ฐ€์žฅ ๋‚˜์ค‘์— ์ž…์‚ฌํ•œ ์‚ฌ์›์˜ ์ž…์‚ฌ ๋‚ ์งœ๋ณด๋‹ค ๋” ๋จผ์ € ์ž…์‚ฌํ•œ ์‚ฌ์›๋“ค์˜ ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„, ์ž…์‚ฌ์ผ์„ ๊ฐ€์ ธ์˜จ๋‹ค.

SELECT EMPNO, ENAME, HIREDATE FROM EMP WHERE HIREDATE < ANY (SELECT HIREDATE FROM EMP WHERE DEPTNO = (SELECT DEPTNO FROM DEPT WHERE LOC='DALLAS'));

 

 


๐Ÿ“ SET, ๋‘ SELECT ๋ฌธ์„ ํ†ตํ•ด ์–ป์–ด์˜จ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด ์ง‘ํ•ฉ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น๋ฌธ

  • ๋‘ SELECT ๋ฌธ์„ ํ†ตํ•ด ๊ฐ€์ ธ์˜จ ์ปฌ๋Ÿผ์˜ ํ˜•ํƒœ๊ฐ€ ์™„์ „ํžˆ ์ผ์น˜ํ•ด์•ผํ•œ๋‹ค.
  • UNION : ํ•ฉ์ง‘ํ•ฉ
  • UNION ALL : ํ•ฉ์ง‘ํ•ฉ, ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๊นŒ์ง€ ๋ชจ๋‘ ๊ฐ€์ ธ์˜จ๋‹ค.
  • INTERSECT : ๊ต์ง‘ํ•ฉ
  • MINUS : ์ฐจ์ง‘ํ•ฉ

 

--10๋ฒˆ ๋ถ€์„œ์— ๊ทผ๋ฌดํ•˜๊ณ  ์žˆ๋Š” ์‚ฌ์›์˜ ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„, ์ง๋ฌด, ๊ทผ๋ฌด๋ถ€์„œ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

SELECT EMPNO, ENAME, JOB, DEPTNO FROM EMP WHERE DEPTNO=10;

SELECT EMPNO, ENAME, JOB, DEPTNO FROM EMP WHERE JOB='CLERK';

 

--SET ์ด์šฉ

--UNION (์ค‘๋ณต X)

SELECT EMPNO, ENAME, JOB, DEPTNO FROM EMP WHERE DEPTNO=10 UNION SELECT EMPNO, ENAME, JOB, DEPTNO FROM EMP WHERE JOB='CLERK';

 

--UNION ALL (์ค‘๋ณต ํ—ˆ์šฉ)

SELECT EMPNO, ENAME, JOB, DEPTNO FROM EMP WHERE DEPTNO=10 UNION ALL SELECT EMPNO, ENAME, JOB, DEPTNO FROM EMP WHERE JOB='CLERK';

 

--INTERSECT (๊ต์ง‘ํ•ฉ)

SELECT EMPNO, ENAME, JOB, DEPTNO FROM EMP WHERE DEPTNO=10 INTERSECT SELECT EMPNO, ENAME, JOB, DEPTNO FROM EMP WHERE JOB='CLERK';

 

--MINUS (์ฐจ์ง‘ํ•ฉ) SELECT EMPNO, ENAME, JOB, DEPTNO FROM EMP WHERE DEPTNO=10 MINUS SELECT EMPNO, ENAME, JOB, DEPTNO FROM EMP WHERE JOB='CLERK';