[STARTERS 4๊ธฐ TIL] #9์ผ์ฐจ(23.02.16)

 

๐Ÿ“š ์˜ค๋Š˜ ๋‚ด๊ฐ€ ๋ฐฐ์šด ๊ฒƒ

๐Ÿ“ ORDER BY ์ ˆ 

  • ์ •๋ ฌ ๊ธฐ์ค€์ด ๋  ์นผ๋Ÿผ ๋˜๋Š” ํ‘œํ˜„์‹์„ ์ฝค๋งˆ(,)๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ˆœ์„œ๋Œ€๋กœ ๊ธฐ์ˆ ํ•œ๋‹ค.
SELECT ์นผ๋Ÿผ, ์นผ๋Ÿผ, ...
  FROM ํ…Œ์ด๋ธ”
 WHERE ์กฐ๊ฑด
ORDER BY ์นผ๋Ÿผ [ASC/DESC], ... ;
  • SELECT ์ ˆ์˜ ์นผ๋Ÿผ ๋ณ„์นญ์ด๋‚˜ ์นผ๋Ÿผ ์œ„์น˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
  • ORDER BY ์ ˆ์— ์‚ฌ์šฉ๋˜๋Š” ํ‚ค์›Œ๋“œ
  • - ASC(๊ธฐ๋ณธ ๊ฐ’), DESC - NULL FIRST, NULLS LAST
  • NULL์€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ์‹œ ๋’ค์ชฝ์œผ๋กœ, ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ ์‹œ ์•ž์ชฝ์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค.
SELECT ์นผ๋Ÿผ, ์นผ๋Ÿผ, ...
  FROM ํ…Œ์ด๋ธ”
 WHERE ์กฐ๊ฑด
ORDER BY ์นผ๋Ÿผ [ASC/DESC] NULLS LAST ;

 

 

๐Ÿ“ JOIN 

  • ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์ •๋ณด๋ฅผ ํ•จ๊ป˜ ์กฐํšŒ
  • ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์— ํŠน์ • ์นผ๋Ÿผ์˜ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ, ๋น„๊ต ๊ฒฐ๊ณผ๊ฐ€ TRUE์ธ ํ–‰๋“ค์„ ์—ฐ๊ฒฐ์‹œํ‚จ๋‹ค.
SELECT ์นผ๋Ÿผ, ์นผ๋Ÿผ, ...
  FROM ํ…Œ์ด๋ธ” a, ํ…Œ์ด๋ธ” b, ...
 WHERE ์กฐ์ธ ์กฐ๊ฑด 
   AND ์ผ๋ฐ˜ ์กฐ๊ฑด;

 

  • ๋“ฑ๊ฐ€ ์กฐ์ธ : ์กฐ์ธ ์กฐ๊ฑด์ด ๋“ฑํ˜ธ(=)์ธ ์กฐ์ธ
SELECT *
  FROM emp a, dept b
 WHERE b.deptno = a.deptno ;
  • ๋น„๋“ฑ๊ฐ€ ์กฐ์ธ : ๋“ฑํ˜ธ ์™ธ ๋‹ค๋ฅธ ๋น„๊ต ์กฐ๊ฑด ์‚ฌ์šฉ
SELECT *
  FROM emp a, salgrade b
 WHERE a.sal BETWEEN b.lowsal AND b.highsal ; 
  • ์…€ํ”„ ์กฐ์ธ : ๋™์ผํ•œ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•˜๋Š” ๊ฒƒ
SELECT *
  FROM emp a, -- ์‚ฌ์›
       emp b  -- ๊ด€๋ฆฌ์ž
 WHERE a.empno = b.mgr;   
  • ์กฐ์ธํ•˜๋ฉด์„œ 1:m ๊ด€๊ณ„์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ ๊ฐ’์ด ๋Š˜์–ด๋‚  ์ˆ˜ ์žˆ์Œ !! ์ด ๊ฒฝ์šฐ๋ฅผ ์œ ์˜ํ•ด์„œ ์กฐํšŒํ•˜์ž !

 

๐Ÿ“ ์•„์šฐํ„ฐ ์กฐ์ธ๊ณผ ํฌ๋กœ์Šค ์กฐ์ธ 

  • Inner Join
    • ์กฐ์ธ์— ์„ฑ๊ณตํ•œ ํ–‰๋“ค๋งŒ ๋ฆฌํ„ดํ•˜๋Š” ์กฐ์ธ
  • Outer Join
    • Inner Join ์˜ ๊ฒฐ๊ณผ์™€ ์กฐ์ธ์— ์‹คํŒจํ•œ ๊ธฐ์ค€ ์ง‘ํ•ฉ์˜ ํ–‰๋“ค์„ ํ•จ๊ป˜ ๋ฆฌํ„ดํ•˜๋Š” ์กฐ์ธ
    • ์˜ค๋ผํด์—์„œ๋Š” ์กฐ์ธ ์กฐ๊ฑด์— (+) ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ์ค€ ์ง‘ํ•ฉ์„ ์ง€์ •ํ•œ๋‹ค.
    • (+) ๊ธฐํ˜ธ๊ฐ€ ์‚ฌ์šฉ๋œ ์ชฝ์˜ ๋ฐ˜๋Œ€์ชฝ ์ง‘ํ•ฉ์ด ๊ธฐ์ค€ ์ง‘ํ•ฉ์ด ๋œ๋‹ค.
  • Cross Join
    • ์–‘์ชฝ ์ง‘ํ•ฉ์˜ ์นดํ‹ฐ์…˜ ๊ณฑ(M*N ๊ฑด)์„ ๋ฆฌํ„ดํ•˜๋Š” ์กฐ์ธ
    • ์–‘์ชฝ ์ง‘ํ•ฉ ๊ฐ„์— ์กฐ์ธ ์กฐ๊ฑด์ด ์—†์„ ๋•Œ Cross Join์ด ์ˆ˜ํ–‰๋œ๋‹ค.
  • ์˜ˆ์‹œ

EMP (b)

EMPNO ENAME JOB DEPTNO
7369 ๋ฏผ์ง€ ์ ์› 20
7499 ๋ฏผ์•„ ํŒ๋งค์› 30
7521 ์ˆ˜์› ํŒ๋งค์› 30
7566 ๋™์šฐ ๋งค๋‹ˆ์ € 20
7654 ์ฃผํ˜• ๋ถ„์„๊ฐ€ 10
7698 ํ•˜๋ฆฐ ์ ์› 20
7782 ์‘์ผ ์ ์› 10
7839 ์ฑ„์› ๋ถ„์„๊ฐ€ 30

DEPT (a)

DEPTNO DNAME LOC
10 ํšŒ๊ณ„ ๋‰ด์š•
20 ์—ฐ๊ตฌ ์‹œ์นด๊ณ 
30 ํŒ๋งค ๋ณด์Šคํ„ด
40 ์šด์˜ ์›Œ์‹ฑํ„ด

 

  • Inner Join
SELECT b.empno, b.ename, b.job, b.deptno, a,dname, a.loc
  FROM dept a, emp b
 WHERE b.deptno = a.deptno ;

 

EMPNO ENAME JOB DEPTNO DEPTNO DNAME LOC
7369 ๋ฏผ์ง€ ์ ์› 20 20 ์—ฐ๊ตฌ ์‹œ์นด๊ณ 
7499 ๋ฏผ์•„ ํŒ๋งค์› 30 30 ํŒ๋งค ๋ณด์Šคํ„ด
7521 ์ˆ˜์› ํŒ๋งค์› 30 30 ํŒ๋งค ๋ณด์Šคํ„ด
7566 ๋™์šฐ ๋งค๋‹ˆ์ € 20 20 ์—ฐ๊ตฌ ์‹œ์นด๊ณ 
7654 ์ฃผํ˜• ๋ถ„์„๊ฐ€ 10 10 ํšŒ๊ณ„ ๋‰ด์š•
7698 ํ•˜๋ฆฐ ์ ์› 20 20 ์—ฐ๊ตฌ ์‹œ์นด๊ณ 
7782 ์‘์ผ ์ ์› 10 10 ํšŒ๊ณ„ ๋‰ด์š•
7839 ์ฑ„์› ๋ถ„์„๊ฐ€ 30 30 ํŒ๋งค ๋ณด์Šคํ„ด

 

  • DEPT ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€ ์ง‘ํ•ฉ์œผ๋กœ ํ•œ Outer Join
SELECT b.empno, b.ename, b.job, b.deptno, a.deptno, a.dname, a.loc
  FROM dept a, emp b
 WHERE b.deptno(+) = a.deptno ;
  • EMP (b) DEPT (a) - ๊ธฐ์ค€ ์ง‘ํ•ฉ
    EMPNO ENAME JOB DEPTNO DEPTNO DNAME LOC
    7369 ๋ฏผ์ง€ ์ ์› 20 20 ์—ฐ๊ตฌ ์‹œ์นด๊ณ 
    7499 ๋ฏผ์•„ ํŒ๋งค์› 30 30 ํŒ๋งค ๋ณด์Šคํ„ด
    7521 ์ˆ˜์› ํŒ๋งค์› 30 30 ํŒ๋งค ๋ณด์Šคํ„ด
    7566 ๋™์šฐ ๋งค๋‹ˆ์ € 20 20 ์—ฐ๊ตฌ ์‹œ์นด๊ณ 
    7654 ์ฃผํ˜• ๋ถ„์„๊ฐ€ 10 10 ํšŒ๊ณ„ ๋‰ด์š•
    7698 ํ•˜๋ฆฐ ์ ์› 20 20 ์—ฐ๊ตฌ ์‹œ์นด๊ณ 
    7782 ์‘์ผ ์ ์› 10 10 ํšŒ๊ณ„ ๋‰ด์š•
    7839 ์ฑ„์› ๋ถ„์„๊ฐ€ 30 30 ํŒ๋งค ๋ณด์Šคํ„ด
            40 ์šด์˜ ์›Œ์‹ฑํ„ด
  • EMP ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€ ์ง‘ํ•ฉ์œผ๋กœ ํ•œ Outer Join
SELECT b.empno, b.ename, b.job, b.deptno, a.deptno, a.dname, a.loc
  FROM dept a, emp b
 WHERE b.deptno = a.deptno(+) ;
  • EMP (b) - ๊ธฐ์ค€ ์ง‘ํ•ฉ DEPT (a)
    EMPNO ENAME JOB DEPTNO DEPTNO DNAME LOC
    7369 ๋ฏผ์ง€ ์ ์› 20 20 ์—ฐ๊ตฌ ์‹œ์นด๊ณ 
    7499 ๋ฏผ์•„ ํŒ๋งค์› 30 30 ํŒ๋งค ๋ณด์Šคํ„ด
    7521 ์ˆ˜์› ํŒ๋งค์› 30 30 ํŒ๋งค ๋ณด์Šคํ„ด
    7566 ๋™์šฐ ๋งค๋‹ˆ์ € 20 20 ์—ฐ๊ตฌ ์‹œ์นด๊ณ 
    7654 ์ฃผํ˜• ๋ถ„์„๊ฐ€ 10 10 ํšŒ๊ณ„ ๋‰ด์š•
    7698 ํ•˜๋ฆฐ ์ ์› 20 20 ์—ฐ๊ตฌ ์‹œ์นด๊ณ 
    7782 ์‘์ผ ์ ์› 10 10 ํšŒ๊ณ„ ๋‰ด์š•
    7839 ์ฑ„์› ๋ถ„์„๊ฐ€ 30 30 ํŒ๋งค ๋ณด์Šคํ„ด
  • Outer Join ๋ฌธ๋ฒ• ์‚ฌ์šฉ ์‹œ ๊ณ ๋ ค์‚ฌํ•ญ
    • ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ด€๊ณ„์—์„œ ์„ ํƒ์„ฑ(ํ•„์ˆ˜/์„ ํƒ ์ฐธ์—ฌ) ํ‘œ๊ธฐ๊ฐ€ ๊ฐ–๋Š” ์˜๋ฏธ
  • Outer Join ๋น„๊ธฐ์ค€ ์ง‘ํ•ฉ ์ชฝ ์ผ๋ฐ˜ ์กฐ๊ฑด ์‚ฌ์šฉ ์‹œ ์ฃผ์˜์‚ฌํ•ญ
    • ๋น„๊ธฐ์ค€ ์ง‘ํ•ฉ ์ชฝ ์กฐ๊ฑด ์ค‘ ํ•˜๋‚˜๋ผ๋„ (+) ๊ธฐํ˜ธ๊ฐ€ ๋ˆ„๋ฝ๋˜๋ฉด Inner join์ด ์ˆ˜ํ–‰๋œ๋‹ค
    SELECT b.empno, b.ename, b.job, b.deptno, a.deptno, a.dname, a.loc
      FROM dept a, emp b
     WHERE b.deptno(+) = a.deptno
    	 AND b.deptno <> 20 ;
    
  • Cross Join ์˜ˆ์‹œ
    • ์กฐ์ธ ์กฐ๊ฑด์ด ์—†์œผ๋ฉด ์นดํ‹ฐ์…˜ ๊ณฑ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค
    • → ์–‘์ชฝ ์ง‘ํ•ฉ์˜ ๋ชจ๋“  ํ–‰์„ ์„œ๋กœ ์—ฐ๊ฒฐํ•œ๋‹ค (๊ฒฐ๊ณผ๋Š” M*N ๊ฑด)
    SELECT b.empno, b.ename, b.job, b.deptno, a.deptno, a.dname, a.loc
      FROM dept a, emp b
     WHERE a.loc IN ('๋‰ด์š•', '๋ณด์Šคํ„ด')              -- ์ผ๋ฐ˜ ์กฐ๊ฑด
    	 AND b.job IN ('๋ถ„์„๊ฐ€', '๋งค๋‹ˆ์ €', '์ ์›') ;    -- ์ผ๋ฐ˜ ์กฐ๊ฑด
    
  • ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž
    • ๊ฐœ๋ณ„๋กœ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ์ฟผ๋ฆฌ๋“ค์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ ๊ฐ„์— ์—ฐ์‚ฐ ์ˆ˜ํ–‰
    SELECT ์นผ๋Ÿผ1, ์นผ๋Ÿผ2, ...
      FROM ํ…Œ์ด๋ธ”
    [WHERE / GROUP BY / HAVING]
    
    ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž
    
    SELECT ์นผ๋Ÿผ1, ์นผ๋Ÿผ2, ...
      FROM ํ…Œ์ด๋ธ”
    [WHERE / GROUP BY / HAVING]
    ORDER BY ์นผ๋Ÿผ [ASC/DESC], ...;
    
    • ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž ์ œ์•ฝ ์‚ฌํ•ญ
      • ๋Œ€์ƒ ์ฟผ๋ฆฌ๋“ค์˜ SELECT ์ ˆ์— ๊ธฐ์ˆ ๋œ ์นผ๋Ÿผ/ํ‘œํ˜„์‹์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค.
      • ๋Œ€์ƒ ์ฟผ๋ฆฌ๋“ค์˜ SELECT ์ ˆ์— ๊ธฐ์ˆ ๋œ ์นผ๋Ÿผ/ํ‘œํ˜„์‹์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ์ˆœ์„œ๋Œ€๋กœ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค.
      • ORDER BY ์ ˆ์€ ์ „ํ…Œ ์ฟผ๋ฆฌ์˜ ๋งจ ๋์—๋งŒ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
    • ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž์˜ ์ข…๋ฅ˜ (์œ„์—์„œ๋ถ€ํ„ฐ ์•„๋ž˜๋กœ ์—ฐ์‚ฐ)
      • UNION ALL : ํ•ฉ์ง‘ํ•ฉ (์ค‘๋ณต ๊ฐ’ ์ œ๊ฑฐX)
      • UNION : ํ•ฉ์ง‘ํ•ฉ (์ค‘๋ณต ๊ฐ’ ์ œ๊ฑฐO)
      • INTERSECT : ๊ต์ง‘ํ•ฉ (์ค‘๋ณต ๊ฐ’ ์ œ๊ฑฐO)
      • MINUS : ์ฐจ์ง‘ํ•ฉ (์ค‘๋ณต ๊ฐ’ ์ œ๊ฑฐO)

 

๐Ÿ“์„œ๋ธŒ ์ฟผ๋ฆฌ

  • ์„œ๋ธŒ ์ฟผ๋ฆฌ : ํ•˜๋‚˜์˜ SQL๋ฌธ ๋‚ด์— ํฌํ•จ๋œ ๋˜ ๋‹ค๋ฅธ SQL๋ฌธ
  • ์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ์ข…๋ฅ˜
    ์ค‘์ฒฉ ์„œ๋ธŒ ์ฟผ๋ฆฌ WHERE ์ ˆ์ด๋‚˜ HAVING ์ ˆ์—์„œ ์กฐ๊ฑด์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ
    ์Šค์นผ๋ผ ์„œ๋ธŒ ์ฟผ๋ฆฌ SELECT ์ ˆ์—์„œ ์‚ฌ์šฉ๋˜๋ฉฐ ๋‹จ์ผ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ
    ์ธ๋ผ์ธ ๋ทฐ FROM ์ ˆ์—์„œ ์กฐํšŒ ๋Œ€์ƒ ์ง‘ํ•ฉ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ
  • ์ค‘์ฒฉ ์„œ๋ธŒ ์ฟผ๋ฆฌ
    • ๋น„์ƒ๊ด€ ์„œ๋ธŒ ์ฟผ๋ฆฌ
      • ๋ฉ”์ธ ์ฟผ๋ฆฌ์˜ ์นผ๋Ÿผ์„ ์ฐธ์กฐํ•˜์ง€ ์•Š๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ
      • ๋ฉ”์ธ ์ฟผ๋ฆฌ์˜ ๊ฐ ํ–‰์„ ํ‰๊ฐ€ํ•  ๋•Œ ์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์ง€์ง€ ์•Š๋Š”๋‹ค.
      SELECT a.ename, a.deptno, a.sal
        FROM emp a
       WHERE a.deptno = 20
         AND a.sal > (SELECT AVG(x.sal)
      								  FROM emp x);
      
    • ๋‹จ์ผ ํ–‰ ๋น„์ƒ๊ด€ ์„œ๋ธŒ ์ฟผ๋ฆฌ
      • ์„œ๋ธŒ ์ฟผ๋ฆฌ๊ฐ€ ๋‹จ์ผ ํ–‰์„ ๋ฆฌํ„ดํ•˜๋Š” ๋น„์ƒ๊ด€ ์„œ๋ธŒ ์ฟผ๋ฆฌ (์„œ๋ธŒ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๊ฐ€ 2๊ฑด ์ด์ƒ์ด๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ)
      • ๋‹จ์ผ ๊ฐ’ ๋น„๊ต ์กฐ๊ฑด ( =, <, >, ≤, ≥ <> ๋“ฑ)๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ
      SELECT a.ename, a.deptno, a.sal
        FROM emp a
       WHERE a.deptno = (SELECT x.deptno
      										 FROM emp x
      								     WHERE x.empno = 7369);
      
    • ๋‹ค์ค‘ ํ–‰ ๋น„์ƒ๊ด€ ์„œ๋ธŒ ์ฟผ๋ฆฌ
      • ์„œ๋ธŒ ์ฟผ๋ฆฌ๊ฐ€ ๋‹ค์ค‘ ํ–‰์„ ๋ฆฌํ„ดํ•˜๋Š” ๋น„์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ
      • ๋‹ค์ค‘ ๊ฐ’ ๋น„๊ต ์กฐ๊ฑด์ธ IN ์กฐ๊ฑด ๋˜๋Š” SOME/ANY, ALL ์กฐ๊ฑด ๋“ฑ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ
      SELECT a.ename, a.deptno, a.sal
        FROM emp a
       WHERE a.deptno IN (10, 30)
         AND a.sal > ALL (SELECT x.sal
      											FROM emp x
      										 WHERE x.job = 'CLERK');
      
    • ๋‹ค์ค‘ ํ–‰ ๋น„์ƒ๊ด€ ์„œ๋ธŒ ์ฟผ๋ฆฌ (IN ์กฐ๊ฑด)
      • EXISTS ์กฐ๊ฑด๊ณผ ์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ์žฌ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ๋‹ค.
      SELECT a.ename, a,ename, a.sal, a.job
        FROM emp a
       WHERE a.sal BETWEEN 1300 AND 2850
         AND a.job IN (SELECT x.job
      											FROM emp x
      										 WHERE x.deptno = 20);
      
    • ๋‹ค์ค‘ ํ–‰ ๋น„์ƒ๊ด€ ์„œ๋ธŒ ์ฟผ๋ฆฌ (NOT IN ์กฐ๊ฑด)
      • NOT IN ์กฐ๊ฑด๊ณผ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ์— NULL์ด ํฌํ•จ๋  ๊ฒฝ์šฐ, ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์ด ๊ณต์ง‘ํ•ฉ(0๊ฑด)์ด ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ฃผ์˜๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค.
      SELECT a.ename, a,ename, a.sal, a.job
        FROM emp a
       WHERE a.sal BETWEEN 1300 AND 2850
         AND a.job NOT IN (SELECT NULLIF(x.job, 'ANALYST')
      											FROM emp x
      										 WHERE x.deptno = 20);
      
      • NULLIF ํ•จ์ˆ˜ (NVL๊ณผ ๋ฐ˜๋Œ€)
        • ํŠน์ • ๊ฐ’์„ NULL๋กœ ์น˜ํ™˜ : NULLIF(Column, ‘๊ฐ’’)
        • ๋‘ ์นผ๋Ÿผ์„ ๋น„๊ตํ•˜์—ฌ ๊ฐ™์„ ๊ฒฝ์šฐ NULL๋กœ ์น˜ํ™˜ : NULLIF(Column, Column)
    • ์ƒ๊ด€ ์„œ๋ธŒ ์ฟผ๋ฆฌ
      • ๋ฉ”์ธ ์ฟผ๋ฆฌ์˜ ์นผ๋Ÿผ์„ ์ฐธ์กฐํ•˜์—ฌ ์ˆ˜ํ–‰๋˜๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ
      • ๋ฉ”์ธ ์ฟผ๋ฆฌ์˜ ๊ฐ ํ–‰์„ ํ‰๊ฐ€ํ•  ๋•Œ๋งˆ๋‹ค ์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋‹ค.
      SELECT a.ename, a.deptno, a.sal
        FROM emp a
       WHERE a.deptno = 20
         AND a.sal > (SELECT AVG(x.sal)
      									FROM emp x
      								 WHERE x.job = a.job);
      
    • EXISTS ์กฐ๊ฑด๊ณผ ์ƒ๊ด€ ์„œ๋ธŒ ์ฟผ๋ฆฌ
      • EXISTS ์กฐ๊ฑด์€ ๊ฐ ํ–‰๋งˆ๋‹ค ์ˆ˜ํ–‰๋œ ์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ 1๊ฑด ์ด์ƒ์ด๋ฉด TRUE๋กœ ํ‰๊ฐ€๋œ๋‹ค.
      • ์„œ๋ธŒ ์ฟผ๋ฆฌ์—์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํ–‰์ด 1๊ฑด๋งŒ ๋ฆฌํ„ด๋˜๋ฉด ์„œ๋ธŒ ์ฟผ๋ฆฌ ์‹คํ–‰์„ ๋ฉˆ์ถ”๊ณ  TRUE๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
      SELECT a.ename, a,ename, a.sal, a.job
        FROM emp a
       WHERE a.sal BETWEEN 1300 AND 2850
         AND EXISTS (SELECT 'x'
      											FROM emp x
      										 WHERE x.deptno = 20
      											 AND x.job = a.job);
      
  • ์Šค์นผ๋ผ ์„œ๋ธŒ ์ฟผ๋ฆฌ
    • SELECT ์ ˆ์—์„œ ์‚ฌ์šฉ๋˜๋ฉฐ ๋‹จ์ผ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ
      • ๋ฉ”์ธ ์ฟผ๋ฆฌ ์ง‘ํ•ฉ์˜ ๊ฐ ํ–‰๋งˆ๋‹ค ์‹คํ–‰๋˜์–ด ๋‹จ์ผ ๊ฐ’(1ํ–‰ 1์—ด)์„ ๋ฆฌํ„ดํ•˜๋Š” ์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ → ์•„๋‹ˆ๋ฉด ์—๋Ÿฌ ๋ฐœ์ƒ(์ง‘๊ณ„ ํ•จ์ˆ˜ ์‚ฌ์šฉ
      • ๋ฉ”์ธ ์ฟผ๋ฆฌ ์ง‘ํ•ฉ์˜ ๊ฒฐ๊ณผ ๊ฑด์ˆ˜๋Š” ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ์— ์˜ํ•ด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์Œ.
    • ์Šค์นผ๋ผ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋กœ ๊ตฌํ•˜๋Š” ์—ด์ด 2๊ฐœ ์ด์ƒ์ธ ๊ฒฝ์šฐ, ๊ฐ๊ฐ์˜ ์Šค์นผ๋ผ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋กœ ๋‚˜๋ˆ  ์‚ฌ์šฉํ•œ๋‹ค.
    • ์Šค์นผ๋ผ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ๊ณผ๋‹คํ•˜๊ฒŒ ๋‚จ์šฉํ•˜๋ฉด ์กฐํšŒ ์„ฑ๋Šฅ์ด ๋Š๋ ค์งˆ ์ˆ˜ ์žˆ๋‹ค.
    • → ์ธ๋ผ์ธ ๋ทฐ์™€ ์•„์šฐํ„ฐ ์กฐ์ธ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ์กฐํšŒ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์Œ
  • ์ธ๋ผ์ธ ๋ทฐ
    • ์ธ๋ผ์ธ ๋ทฐ
      • FROM ์ ˆ์—์„œ ์กฐํšŒ ๋Œ€์ƒ ์ง‘ํ•ฉ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ
    • ๋ทฐ(View)
      • ์ฟผ๋ฆฌ๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•˜์—ฌ ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์˜ค๋ธŒ์ ํŠธ
      CREATE OR REPLACE VIEW VW_DEPT_SUM AS
      SELECT v.deptno
      		 , SUM(v.sal) AS SUM_SAL
        FROM emp v
      GROUP BY v.deptno ;
      
      SELECT a.dname
      		 , b.sum_sal
        FROM dept a,
      		   vw_dept_sum b
       WHERE b.deptno = a.deptno ;
      
    • WITH ์ ˆ
      • ์„œ๋ธŒ ์ฟผ๋ฆฌ์— ์ด๋ฆ„์„ ํ• ๋‹นํ•˜๊ณ , ๋ฉ”์ธ ์ฟผ๋ฆฌ์—์„œ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.
WITH w1 AS
(SELECT a.deptno
		  , MAX(b.grade) AS MAX_GRADE
		  , MIN(b.grade) AS MIN_GRADE
   FROM emp a,
				salgrade b
 WHERE a.sal BETWEEN b.losal AND b.hisal
 GROUP BY a.deptno)

SELECT b.dname, a.maw_grade, a.min_grade
  FROM W1 a,
			 dept b
 WHERE b.deptno = a.deptno(+);

๐Ÿ’ญ ์˜ค๋Š˜ ๊ธฐ์–ตํ•ด์•ผ ํ•  ๋‚ด์šฉ

· ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉ

 ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด ๋‹ค์–‘ํ•œ๋ฐ, ์–ด๋–ค ์ƒํ™ฉ์— ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€์ง€

 ์˜ˆ์ œ๋ฅผ ํ’€์–ด๋ณด๋ฉด์„œ ๋ฐฐ์›Œ์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค !

 

 

โœ๐Ÿป ํšŒ๊ณ 

์˜ค๋Š˜์€ SQL์—์„œ ์กฐ์ธ, ์„œ๋ธŒ์ฟผ๋ฆฌ ๋“ฑ์— ๊ด€ํ•ด ๋ฐฐ์› ๋‹ค.

์กฐ์ธ์ด๋ž‘ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ์กฐ๊ธˆ ์–ด๋ ค์› ๋‹ค ๐Ÿ˜ข

์–ด๋–ค ์ƒํ™ฉ์— ์–ด๋–ค ์ข…๋ฅ˜์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ• ์ง€ ํ—ท๊ฐˆ๋ ธ๋Š”๋ฐ

์•„๋ฌด๋ž˜๋„ ์˜ˆ์ œ๋ฅผ ํ’€๋ฉด์„œ ์—ฐ์Šตํ•ด๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค..!