JOIN

์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์„ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ๋…ผ๋ฆฌ์ ์œผ๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

์ผ๋ฐ˜์ ์œผ๋กœ, ๋‘˜ ์ด์ƒ ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ JOIN์„ ์‹œ๋„ํ•œ๋‹ค.

Various Joins

JOIN๊ณผ ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž์˜ ์ฐจ์ด

  • ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž : ๋‘ ๊ฐœ ์ด์ƒ์˜ SELECT๋ฌธ์˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ์„ธ๋กœ๋กœ ์—ฐ๊ฒฐํ•œ ๊ฒƒ
  • JOIN : ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๋กœ๋กœ ์—ฐ๊ฒฐํ•œ ๊ฒƒ

EQUI JOIN

๋‘ ํ…Œ์ด๋ธ”์˜ ํŠน์ • ์—ด์˜ ๊ฐ’๋“ค์ด ์ •ํ™•ํ•˜๊ฒŒ ์ผ์น˜ํ•  ๋•Œ ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•

  • Default Join ๋ฐฉ๋ฒ•
  • ์ผ๋ฐ˜์ ์œผ๋กœ PK, FK ๊ด€๊ณ„์— ์˜ํ•ด JOIN์„ ์‹œ๋„ํ•˜์ง€๋งŒ ์ผ๋ฐ˜ ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ JOIN์„ ์‹œ๋„ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ
  • JOINํ•˜๋ ค๋Š” ๋‘๊ฐœ ์ด์ƒ ํ…Œ์ด๋ธ”์ด ๋™์ผํ•œ ์ด๋ฆ„์˜ ์ปฌ๋Ÿผ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด SELECT ์‹œ ๋ฐ˜๋“œ์‹œ ํ…Œ์ด๋ธ” ๋ช…์„ ๋ฐํ˜€์•ผํ•จ
  • N๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ JOINํ•  ๊ฒฝ์šฐ ์ตœ์†Œ N-1๊ฐœ์˜ JOIN ์กฐ๊ฑด์ด ํ•„์š”ํ•จ

SQL ํ˜•ํƒœ

Q. ํšŒ์‚ฌ ์ง์›๋“ค์˜ ์‚ฌ๋ฒˆ, ์ด๋ฆ„๊ณผ ํ•จ๊ป˜ ํ•ด๋‹น ์ง์›๋“ค์ด ์†ํ•œ ๋ถ€์„œ๋ฒˆํ˜ธ์™€ ๋ถ€์„œ๋ช…์„ ์กฐํšŒํ•œ๋‹ค.

  • ์ง์› ํ…Œ์ด๋ธ”์˜ ์ง์›์ด ์†ํ•œ ๋ถ€์„œ๋ฒˆํ˜ธ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ์—ด๊ณผ ๋ถ€์„œ ํ…Œ์ด๋ธ”์˜ ๋ถ€์„œ๋ฒˆํ˜ธ ์—ด์„ ๊ธฐ์ค€์œผ๋กœ JOINํ•˜๋ฉด ๋‘ ํ…Œ์ด๋ธ”์˜ ๋ถ€์„œ๋ฒˆํ˜ธ๊ฐ€ ๋™์ผํ•  ๋•Œ ์ง์› ์ •๋ณด์™€ ๊ฐ ๋ถ€์„œ๋ช…์„ ๋ฐ”๋กœ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.
SELECT e.empno, e.ename, e.deptno, d.dname
FROM emp e, dept d
WHERE e.deptno = d.deptno;
  • ์œ„์˜ ์ฟผ๋ฆฌ๋ฌธ๊ณผ ๊ฐ™์ด = ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ์™„์ „ํžˆ ์ผ์น˜ํ•˜๋Š” ํŠน์ • ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์กฐ์ธ
  • JOIN ์กฐ๊ฑด์„ ON ์ ˆ์ด๋‚˜ WHERE ์ ˆ์— ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ์Œ

    • ๋‹จ, OUTER JOIN์„ ์‹œ๋„ํ•  ๊ฒฝ์šฐ ์—ฌ๋Ÿฌ ์กฐ๊ฑด๊ณผ ํ•จ๊ป˜ JOIN ์กฐ๊ฑด์„ ON ์ ˆ๋กœ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ๊ณผ WHERE ์ ˆ๋กœ ๋ถ€์—ฌํ•  ๋•Œ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์ง€๋ฏ€๋กœ ์ฃผ์˜

INNER JOIN

  • EQUI JOIN๊ณผ ํ•จ๊ป˜ Default Join ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉ๋จ
  • SQL ํ˜•ํƒœ๋Š” EQUI JOIN๊ณผ ๊ฐ™์Œ

INNER JOIN๊ณผ EQUI JOIN

  • EQUI JOIN : = ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ON ์ ˆ์ด๋‚˜ WHERE ์ ˆ์— ์กฐ๊ฑด์„ ๋ถ€์—ฌํ•˜์—ฌ JOINํ•˜๋Š” ๋ฐฉ๋ฒ•
  • INNER JOIN : JOIN ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํ–‰์— ๋Œ€ํ•ด์„œ๋งŒ ๊ฒฐ๊ณผ ๊ฐ’์ด ๋‚˜์˜ค๋Š” JOIN

INNER JOIN๊ณผ OUTER JOIN

  • INNER JOIN : ๋‘ ํ…Œ์ด๋ธ”์˜ ๊ต์ง‘ํ•ฉ์œผ๋กœ JOINํ•˜๋Š” ๊ฐœ๋…
  • OUTER JOIN : ๋‘ ํ…Œ์ด๋ธ”์˜ ํ•ฉ์ง‘ํ•ฉ์œผ๋กœ JOINํ•˜๋Š” ๊ฐœ๋…
for x in A : 
    for x in B : 
        if (A.x = B.x) JOIN

์œ„์™€ ๊ฐ™์ด A ํ…Œ์ด๋ธ”๊ณผ B ํ…Œ์ด๋ธ”์˜ x ์—ด์„ ๊ธฐ์ค€์œผ๋กœ JOIN์„ ์‹œ๋„ํ•˜๋Š” ๊ฒฝ์šฐ

INNER JOIN์€ ์•„๋ฌด๋ฆฌ A ํ…Œ์ด๋ธ”์— x ์—ด์ด ์กด์žฌํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ B ํ…Œ์ด๋ธ”์˜ x์—ด์˜ ๊ฐ’๊ณผ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด ๊ทธ ๊ฐ’์€ ๊ฒฐ๊ณผ์— ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ๋‚ด๋ถ€ for๋ฌธ์— ํ•ด๋‹นํ•˜๋Š” B ํ…Œ์ด๋ธ” ์กฐ๊ฑด์ด ๊ฒฐ๊ณผ์˜ ํ•ต์‹ฌ์ด๋‹ค.

๋ฐ˜๋ฉด OUTER JOIN์€ A ํ…Œ์ด๋ธ”๊ณผ B ํ…Œ์ด๋ธ”์˜ ๊ฐ๊ฐ์˜ x ์—ด์˜ ๊ฐ’์ด ๊ฐ™์ง€ ์•Š๋‹ค๊ณ  ํ• ์ง€๋ผ๋„ ์™ธ๋ถ€ for๋ฌธ์— ํ•ด๋‹นํ•˜๋Š” A ํ…Œ์ด๋ธ”์— ๊ฐ’์ด ์กด์žฌํ•œ๋‹ค๋ฉด ๊ฒฐ๊ณผ์— ํฌํ•จ์‹œํ‚จ๋‹ค. ๋”ฐ๋ผ์„œ ์™ธ๋ถ€ for๋ฌธ์— ํ•ด๋‹นํ•˜๋Š” A ํ…Œ์ด๋ธ” ์กฐ๊ฑด์ด ๊ฒฐ๊ณผ์˜ ํ•ต์‹ฌ์ด๋‹ค. ๋งŒ์•ฝ ์™ธ๋ถ€ for๋ฌธ์— B ํ…Œ์ด๋ธ”์„, ๋‚ด๋ถ€ for๋ฌธ์— A ํ…Œ์ด๋ธ”์„ ์กฐ๊ฑด์œผ๋กœ ๋‘”๋‹ค๋ฉด ๊ฒฐ๊ณผ๋Š” ์œ„ ๊ฒฝ์šฐ์™€ ๋ฐ˜๋Œ€๋กœ ์™ธ๋ถ€ for๋ฌธ์— ํ•ด๋‹นํ•˜๋Š” B ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๊ฒฐ๊ณผ์— ํฌํ•จ์‹œํ‚จ๋‹ค.


NON-EQUI JOIN

ํ•œ ํ…Œ์ด๋ธ”์˜ ์—ด์˜ ๊ฐ’์ด ๋‹ค๋ฅธ ํ…Œ์ด๋ธ” ์—ด์˜ ๊ฐ’๊ณผ ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜์ง€ ์•Š์ง€๋งŒ JOIN์„ ์‹œ๋„ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

  • EQUI JOIN๊ณผ ๋‹ฌ๋ฆฌ = ์—ฐ์‚ฐ์ž๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ณ (๊ฐ’์ด ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜์ง€ ์•Š์Œ), Between, >, >=, <, <= ๋“ฑ ๋‹ค๋ฅธ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด JOIN ์กฐ๊ฑด์„ ๋ถ€์—ฌํ•ด์•ผ ํ•จ.

SQL ํ˜•ํƒœ

Q. ํšŒ์‚ฌ ์ง์›๋“ค์˜ ์‚ฌ๋ฒˆ, ์ด๋ฆ„๊ณผ ํ•จ๊ป˜ ํ•ด๋‹น ์ง์›๋“ค์˜ ๊ธ‰์—ฌ ๋“ฑ๊ธ‰์„ ์กฐํšŒํ•œ๋‹ค.

  • ์ง์› ํ…Œ์ด๋ธ”์˜ ์ง์› ๊ธ‰์—ฌ ์—ด๊ณผ ๊ธ‰์—ฌ๋“ฑ๊ธ‰ ํ…Œ์ด๋ธ”์˜ ๊ธ‰์—ฌ ํ•˜ํ•œ์„ ๊ณผ ์ƒํ•œ์„  ์—ด๋“ค์„ ๋น„๊ตํ•˜์—ฌ JOINํ•˜๋ฉด ์ง์›์˜ ๊ธ‰์—ฌ์™€ ๊ธ‰์—ฌ๊ฐ€ ์†ํ•˜๋Š” ๋ฒ”์œ„์˜ ๋“ฑ๊ธ‰์„ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.
SELECT e.empno, e.ename, e.salary, s.grade
FROM emp e, salgrade s
WHERE e.salary BETWEEN s.losal AND s.hisal;
  • ์œ„์˜ ์ฟผ๋ฆฌ๋ฌธ๊ณผ ๊ฐ™์ด =๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ์—ฐ์‚ฐ์ž ๊ฒฐ๊ณผ ๋ฒ”์œ„์— ํฌํ•จ๋˜๋Š” ๊ฐ’์„ ๊ฒฐ๊ณผ๋กœ ๋„์ถœ

OUTER JOIN

JOIN ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜์ง€ ์•Š๋Š” ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฒฐ๊ณผ์— ํฌํ•จํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” JOIN ๋ฐฉ๋ฒ•

์–ด๋Š ํ•œ์ชฝ ํ…Œ์ด๋ธ”์—๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ ๋‹ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์—๋Š” ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ INNER JOIN์„ ํ•œ๋‹ค๋ฉด ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์— ๋ˆ„๋ฝ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ

๋”ฐ๋ผ์„œ ํ•ฉ์ง‘ํ•ฉ์„ ๊ตฌํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

OUTER JOIN ๋ฐฉ๋ฒ•

  • LEFT [OUTER] JOIN : JOIN ํ‚ค์›Œ๋“œ์˜ ์™ผ์ชฝ ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€์œผ๋กœ ํ•ด๋‹น ํ…Œ์ด๋ธ”์— ์†ํ•œ ๋ฐ์ดํ„ฐ๋Š” ๋ชจ๋‘ ํฌํ•จ
  • RIGHT [OUTER] JOIN : JOIN ํ‚ค์›Œ๋“œ์˜ ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€์œผ๋กœ ํ•ด๋‹น ํ…Œ์ด๋ธ”์— ์†ํ•œ ๋ฐ์ดํ„ฐ๋Š” ๋ชจ๋‘ ํฌํ•จ
  • FULL [OUTER] JOIN : JOIN ํ‚ค์›Œ๋“œ์˜ ์–‘์ชฝ ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€์œผ๋กœ ๋ชจ๋“  ํ…Œ์ด๋ธ”์— ์†ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จ

SQL ํ˜•ํƒœ

Q. ํšŒ์‚ฌ ๋ชจ๋“  ์ง์›๋“ค์˜ ์‚ฌ๋ฒˆ, ์ด๋ฆ„๊ณผ ํ•จ๊ป˜ ํ•ด๋‹น ์ง์›๋“ค์˜ ๋ถ€์„œ๋ช…์„ ์กฐํšŒํ•œ๋‹ค. ๋‹จ, ์•„์ง ๋ถ€์„œ๋ฅผ ๋ฐฐ์น˜๋ฐ›์ง€ ๋ชปํ•œ ์ง์›๊นŒ์ง€ ํฌํ•จํ•˜์—ฌ ํšŒ์‚ฌ์˜ ๋ชจ๋“  ์ง์›๋“ค์ด ์กฐํšŒ๋˜์–ด์•ผ ํ•œ๋‹ค.

  • ์ง์› ํ…Œ์ด๋ธ”์˜ ๋ถ€์„œ ๋ฒˆํ˜ธ ์—ด๊ณผ ๋ถ€์„œ ํ…Œ์ด๋ธ”์˜ ๋ถ€์„œ ๋ฒˆํ˜ธ ์—ด์˜ ๊ฐ’์ด ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ JOINํ•œ๋‹ค. ๋‹จ, ์ง์› ๋ˆ„๋ฝ์ด ๋ฐœ์ƒํ•˜๋ฉด ์•ˆ๋˜๋ฏ€๋กœ ์ง์› ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋˜๋Š” OUTER JOIN์„ ํ•œ๋‹ค.
SELECT e.employee_id, e.last_name, d.department_name
FROM employees e LEFT JOIN departments d
ON e.department_id = d.department_id;

outer join

  • ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋˜์–ด์•ผํ•˜๋Š” ํ…Œ์ด๋ธ”์˜ ์œ„์น˜์— ๋”ฐ๋ผ LEFT์™€ RIGHT ์กฐ๊ฑด์„ ๋ถ€์—ฌํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

SELF JOIN

ํ•œ ํ…Œ์ด๋ธ”์˜ ์—ด์„ ๊ฐ™์€ ํ…Œ์ด๋ธ” ๋‚ด ๋‹ค๋ฅธ ์—ด๊ณผ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ, ๊ฐ™์€ ํ…Œ์ด๋ธ”์„ 2๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์ธ ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

  • ์ฃผ๋กœ ๋ฐ์ดํ„ฐ ๊ฐ„ ๊ณ„์ธตํ˜• ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์„ ๋•Œ ์‚ฌ์šฉ

    • ex) ์„ ํ›„๋ฐฐ ๊ด€๊ณ„, ์ƒ์‚ฌ์™€ ๋ถ€ํ•˜ ์ง์› ๊ด€๊ณ„, ๊ฒŒ์‹œ๊ธ€๊ณผ ๋‹ต๊ธ€ ๊ด€๊ณ„ ๋“ฑ
  • FROM ์ ˆ ๋’ค์— ๋™์ผํ•œ ํ…Œ์ด๋ธ” ๋ช…์„ 2๋ฒˆ ํ‘œํ˜„ํ•˜๋˜, ๋‘˜์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜๋“œ์‹œ ๋ณ„์นญ์„ ๊ธฐ์žฌํ•ด์•ผ ํ•จ
  • ์ปฌ๋Ÿผ ์—ญ์‹œ ์–ด๋–ค ๊ธฐ์ค€ ํ…Œ์ด๋ธ”์ธ์ง€ ํ…Œ์ด๋ธ” ๋ณ„์นญ์— ์—ฐ๊ฒฐํ•˜์—ฌ ํ‘œํ˜„ํ•ด์•ผ ํ•จ

SQL ํ˜•ํƒœ

Q. ํšŒ์‚ฌ ์ง์›๋“ค์˜ ์ด๋ฆ„๊ณผ ํ•จ๊ป˜ ์ž์‹ ์˜ ์ƒ์‚ฌ ๋ฒˆํ˜ธ, ์ƒ์‚ฌ ์ด๋ฆ„, ์ƒ์‚ฌ์˜ ์ง์› ๋ฒˆํ˜ธ๋ฅผ ์กฐํšŒํ•œ๋‹ค.

  • ์ง์› ํ…Œ์ด๋ธ”์˜ ์ƒ์‚ฌ ๋ฒˆํ˜ธ ์—ด๊ณผ ์ง์› ํ…Œ์ด๋ธ”์˜ ์ง์› ๋ฒˆํ˜ธ ์—ด์˜ ๊ฐ’์ด ๋™์ผํ•œ ๊ฒฝ์šฐ๋ฅผ JOINํ•œ๋‹ค.
SELECT e1.last_name, e1.manager_id, e2.last_name, e2.employee_id
FROM employees e1, employees e2
WHERE e1.manager_id = e2.employee_id;

self join

  • ๋™์ผํ•˜์ง€๋งŒ ์„œ๋กœ ๋‹ค๋ฅธ ๋ชฉ์ ์„ ์œ„ํ•ด ์ค‘๋ณต๋œ ๋‘ ํ…Œ์ด๋ธ”์„ ๊ตฌ๋ถ„ํ•ด์•ผ ํ•จ. ๋”ฐ๋ผ์„œ ๋ณ„์นญ์„ ๊ผญ ์ง€์–ด์ค˜์•ผ ํ•จ.

Cartesian JOIN(Cross JOIN)

  • JOIN ์กฐ๊ฑด์˜ ์˜ค๋ฅ˜๋กœ ์ธํ•ด ํ•œ ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๊ฐ€ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์˜ ๋ ˆ์ฝ”๋“œ์™€ JOIN์ด ๋˜๋Š” ๊ฒฝ์šฐ
  • A ํ…Œ์ด๋ธ” ๋ ˆ์ฝ”๋“œ ์ˆ˜๊ฐ€ a๊ฐœ, A ํ…Œ์ด๋ธ” ๋ ˆ์ฝ”๋“œ ์ˆ˜๊ฐ€ b๊ฐœ๋ผ๋ฉด ์นดํ‹ฐ์‹œ์•ˆ ์กฐ์ธ ๊ฒฐ๊ณผ a*b๊ฐœ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋„์ถœ๋จ.
  • ์„œ๋กœ ๊ด€๊ณ„๊ฐ€ ์—†๋Š” ๋ ˆ์ฝ”๋“œ๋„ ํ•จ๊ป˜ ๋ฌถ์—ฌ ๊ฒฐ๊ณผ๋กœ ์ถœ๋ ฅ๋˜๋ฏ€๋กœ ํ•„์š”์—†๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์ค‘๋ณต๋˜๊ฑฐ๋‚˜ ์ง€๋‚˜์น˜๊ฒŒ ๋งŽ์€ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋  ์ˆ˜ ์žˆ์Œ

SQL ์˜ˆ์ œ

Q. ๋„์‹œ๋ช…๊ณผ ๋‚˜๋ผ๋ช…์„ ์กฐํšŒํ•œ๋‹ค.

  • ๋‘ ํ…Œ์ด๋ธ”์˜ JOIN ๊ธฐ์ค€ ์—†์ด JOINํ•˜๋ฏ€๋กœ ๋ ˆ์ฝ”๋“œ์˜ ์ปฌ๋Ÿผ ๊ฐ„ ์˜๋ฏธ๊ฐ€ ์—†๋Š” ๋ ˆ์ฝ”๋“œ๊ฐ€ ์ €์žฅ๋จ.
SELECT country_name, city
FROM countries, locations;

cartesian join

cartesian join rows

  • ์œ„์˜ ๊ฒฐ๊ณผ 23๊ฐœ์˜ city ๋ ˆ์ฝ”๋“œ ์ˆ˜์™€ 25๊ฐœ์˜ country_name ๋ ˆ์ฝ”๋“œ ์ˆ˜๊ฐ€ ๊ณฑํ•ด์ ธ ์ด 575๊ฐœ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋„์ถœ๋จ.
  • ์˜๋„์ ์œผ๋กœ ์œ„์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•˜๋ ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ๋ฉด, WHERE์ ˆ ํ˜น์€ ON ์ ˆ์— JOIN ์กฐ๊ฑด์„ ๋ช…์‹œํ•ด์•ผํ•จ.

Q. ๊ฐ ๋‚˜๋ผ ๋ณ„ ๋„์‹œ๋ฅผ ์กฐํšŒํ•œ๋‹ค.

SELECT c.country_name, l.city
FROM countries c, locations l
WHERE c.country_id = l.country_id;

cartesian join sol1

cartesian join sol1

Q. ๋ชจ๋“  ๋‚˜๋ผ์˜ ๋„์‹œ๋ฅผ ์กฐํšŒํ•œ๋‹ค.

SELECT c.country_name, l.city
FROM locations l RIGHT JOIN countries c
ON c.country_id = l.country_id;

cartesian join sol2

cartesian join sol2


ANSI JOIN

๋ฏธ๊ตญ ๊ตญ๋ฆฝ ํ‘œ์ค€ ํ˜‘ํšŒ(American National Standards Institute,ANSI)์—์„œ ์ง€์ •ํ•œ SQL ๋ฌธ๋ฒ•

NATURAL JOIN

๋‘ ํ…Œ์ด๋ธ”์˜ JOINํ•  ์—ด๋“ค์ด ์™„์ „ํžˆ ๋™์ผํ•œ ํ•„๋“œ๋ช…(์ปฌ๋Ÿผ๋ช…)์„ ๊ฐ€์งˆ ๊ฒฝ์šฐ ํ•ด๋‹น ์—ด๋“ค์„ JOIN

  • ๋‹จ, ๋‘ ํ…Œ์ด๋ธ”์˜ ์—ด์ด ๊ฐ™์€ ํ•„๋“œ๋ช…์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ํ• ์ง€๋ผ๋„, ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•œ ์ฑ„๋กœ ํ•„๋“œ๋ช…๋งŒ ๋™์ผํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ ์ฃผ์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

SQL ํ˜•ํƒœ

Q. ํšŒ์‚ฌ ์ง์› ๋ณ„ ์ง๋ฌด๋ฅผ ์กฐํšŒํ•œ๋‹ค.

  • ์ง์› ํ…Œ์ด๋ธ”์˜ ์ง๋ฌด ๋ฒˆํ˜ธ์™€ ์ง๋ฌด ํ…Œ์ด๋ธ”์˜ ์ง๋ฌด ๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ JOIN
SELECT last_name, job_title
FROM employees NATURAL JOIN jobs;

JOIN ~ USING

๋‘ ํ…Œ์ด๋ธ”์˜ JOIN ๊ธฐ์ค€ ์—ด์„ USING์— ๋ช…์‹œํ•˜์—ฌ JOINํ•˜๋Š” ๋ฐฉ๋ฒ•

SQL ํ˜•ํƒœ

Q. ํšŒ์‚ฌ ์ง์› ๋ณ„ ์ง๋ฌด๋ฅผ ์กฐํšŒํ•œ๋‹ค.

  • ์ง์› ํ…Œ์ด๋ธ”์˜ ์ง๋ฌด ๋ฒˆํ˜ธ์™€ ์ง๋ฌด ํ…Œ์ด๋ธ”์˜ ์ง๋ฌด ๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ JOIN
SELECT last_name, job_title
FROM employees JOIN jobs
USING(job_id);

JOIN ~ ON

๋‘ ํ…Œ์ด๋ธ” ๊ฐ„ ๊ณตํ†ต๋œ ์ด๋ฆ„์˜ ์—ด์ด ์กด์žฌํ•˜์ง€ ์•Š๊ฑฐ๋‚˜, ์ผ๋ฐ˜ ์ฟผ๋ฆฌ ์กฐ๊ฑด์ธ WHERE ์ ˆ๊ณผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด ON์ ˆ์— ๊ธฐ์ค€์„ ๋ช…์‹œํ•˜์—ฌ JOINํ•˜๋Š” ๋ฐฉ๋ฒ•

OUTER JOIN์—์„œ์˜ WHERE๊ณผ ON

Q. IT ๋ถ€์„œ์—์„œ ์ผํ•˜๋Š” ํšŒ์‚ฌ ์ง์›๋“ค์˜ ์ด๋ฆ„์„ ์กฐํšŒํ•œ๋‹ค.

  • ์ง์› ํ…Œ์ด๋ธ”์˜ ๋ถ€์„œ ๋ฒˆํ˜ธ ์—ด๊ณผ ๋ถ€์„œ ํ…Œ์ด๋ธ”์˜ ๋ถ€์„œ ๋ฒˆํ˜ธ ์—ด์˜ ๊ฐ’์ด ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ JOINํ•œ๋‹ค. ๋‹จ, ์ง์› ๋ˆ„๋ฝ์ด ๋ฐœ์ƒํ•˜๋ฉด ์•ˆ๋˜๋ฏ€๋กœ ์ง์› ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋˜๋Š” OUTER JOIN์„ ํ•œ๋‹ค.
SELECT e.last_name, d.department_name
FROM employees e LEFT JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_name = 'IT';

outer join on where

  • ์œ„์˜ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ WHERE์ ˆ๋กœ IT๋ถ€์„œ ์ง์›๋“ค์„ ํ•„ํ„ฐ๋ง ํ•œ ๋’ค, JOIN์„ ํ•œ๋‹ค.
  • ๋”ฐ๋ผ์„œ IT๋ถ€์„œ ์ง์›๋“ค์ด ์•„๋‹ˆ๋ผ๋ฉด ๊ฒฐ๊ณผ์— ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค.
SELECT e.last_name, d.department_name
FROM employees e LEFT JOIN departments d
ON e.department_id = d.department_id
AND d.department_name = 'IT';

outer join on and

  • ์œ„์˜ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ON์ ˆ์„ ํ†ต์งธ๋กœ ๊ธฐ์ค€์œผ๋กœ ์‚ผ์•„ JOIN์„ ํ•œ๋‹ค.
  • ๋”ฐ๋ผ์„œ ๋ฐ”๊นฅ for๋ฌธ์— ํ•ด๋‹นํ•˜๋Š” employees ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์•ˆ์ชฝ for๋ฌธ์— ํ•ด๋‹นํ•˜๋Š” departments ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ํ™•์ธํ•˜๋ฉฐ ๋ถ€์„œ ๋ฒˆํ˜ธ๊ฐ€ ๋™์ผํ•˜๊ณ  IT๋ถ€์„œ๋ผ๋ฉด JOIN์œผ๋กœ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•˜์ง€๋งŒ, ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋„ employees ํ…Œ์ด๋ธ”์— ์†ํ•œ ๋ฐ์ดํ„ฐ๋ฉด ๊ฒฐ๊ณผ์— ํฌํ•จํ•œ๋‹ค.

OUTER JOIN ์‹œ ON๊ณผ WHERE ์‚ฌ์šฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ ํ•„ํ„ฐ๋ง์œผ๋กœ ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ•์†Œํ•ด์•ผ ํ•œ๋‹ค๋ฉด, 1๋ฒˆ ์˜ˆ์‹œ์ฒ˜๋Ÿผ JOIN ์กฐ๊ฑด์€ ON์—, ํ•„ํ„ฐ๋ง ์กฐ๊ฑด์€ WHERE์— ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.