# SQL JOIN๋ฌธ

written by sohyeon, hyemin ๐Ÿ’ก


# 1. JOIN๋ฌธ

๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์„ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.
์—ฐ์‚ฌ์ž, From์ ˆ์˜ JOIN ํ˜•ํƒœ์— ๋”ฐ๋ผ 4๊ฐ€์ง€๋กœ ๋‚˜๋‰˜์–ด์ง„๋‹ค.

  • ์—ฐ์‚ฐ์ž์— ๋”ฐ๋ฅธ ๋ถ„๋ฅ˜

    • EQUI JOIN
    • Non-EQUI JOIN
  • From์ ˆ์˜ JOIN ํ˜•ํƒœ์— ๋”ฐ๋ผ

    • INNER JOIN
    • OUTER JOIN

# 2. INNER JOIN

# 1) EQUI JOIN

  • ์กฐ์ธ ๋Œ€์ƒ ํ…Œ์ด๋ธ”์˜ ์นผ๋Ÿผ ๊ฐ’๋“ค์ด ์„œ๋กœ ์ •ํ™•ํ•˜๊ฒŒ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ
  • WHERE์ ˆ์— =(Equality Condition)์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ์กฐ๊ฑด์„ ๋ช…์‹œ
  • EQUI JOIN์˜ ์„ฑ๋Šฅ์„ ๋†’์ด๋ ค๋ฉด INDEX ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
  • ๋‘ ํ…Œ์ด๋ธ”์„ ์—ฐ๊ฒฐํ•  ๋•Œ Hash JOIN์„ ํ™œ์šฉํ•˜๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.
SELECT [ํ…Œ์ด๋ธ”๋ช…1.]์†์„ฑ๋ช…, [ํ…Œ์ด๋ธ”๋ช…2.]์†์„ฑ๋ช… ....
FROM ํ…Œ์ด๋ธ”๋ช…1, ํ…Œ์ด๋ธ”๋ช…2
WHERE ํ…Œ์ด๋ธ”๋ช…1.์†์„ฑ๋ช… = ํ…Œ์ด๋ธ”๋ช…2.์†์„ฑ๋ช…

# INNER JOIN ๊ตฌ๋ฌธ

SELECT [ํ…Œ์ด๋ธ”๋ช…1.]์†์„ฑ๋ช…, [ํ…Œ์ด๋ธ”๋ช…2.]์†์„ฑ๋ช… ....
 FROM ํ…Œ์ด๋ธ”๋ช…1
INNER JOIN ํ…Œ์ด๋ธ”๋ช…2
 ON ํ…Œ์ด๋ธ”๋ช…1.์†์„ฑ๋ช… = ํ…Œ์ด๋ธ”๋ช…2.์†์„ฑ๋ช…

# 2) NATURAL JOIN

  • EQUI JOIN์—์„œ =์กฐ๊ฑด์ด ์„ฑ๋ฆฝํ•  ๋•Œ ๋™์ผํ•œ ์†์„ฑ์ด ๋‘๋ฒˆ ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ค‘๋ณต์„ ์ œ๊ฑฐํ•ด ํ•œ๋ฒˆ๋งŒ ํ‘œ๊ธฐํ•˜๊ฒŒ ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
  • ๋‘ ํ…Œ์ด๋ธ”์˜ ๋™์ผํ•œ ์ด๋ฆ„์„ ๊ฐ€์ง€๋Š” ์นผ๋Ÿผ์ด ๋ชจ๋‘ JOIN
  • ๋™์ผํ•œ ์นผ๋Ÿผ์„ ๋‚ด๋ถ€์ ์œผ๋กœ ์ฐพ๊ฒŒ ๋˜๋ฏ€๋กœ ํ…Œ์ด๋ธ” ๋ณ„์นญ์„ ์ฃผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ
SELECT [ํ…Œ์ด๋ธ”๋ช…1.]์†์„ฑ๋ช…, [ํ…Œ์ด๋ธ”๋ช…2.]์†์„ฑ๋ช… ....
FROM ํ…Œ์ด๋ธ”๋ช…1  NATURAL JOIN  ํ…Œ์ด๋ธ”๋ช…2

NATURAL JOIN๋งŒ ์‹คํ–‰ํ•˜๋ฉด ๋™์ผํ•œ ์ด๋ฆ„์„ ๊ฐ€์ง„ ์นผ๋Ÿผ์€ ๋ชจ๋‘ ์กฐ์ธ๋˜๋Š”๋ฐ,
USING์ ˆ์„ ํ™œ์šฉํ•˜๋ฉด ์นผ๋Ÿผ์„ ์„ ํƒํ•ด ์กฐ์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
USING์ ˆ ์•ˆ์— ํฌํ•จ๋˜๋Š” ์นผ๋Ÿผ์— ๋ณ„์นญ์„ ์ง€์ •ํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

SELECT [ํ…Œ์ด๋ธ”๋ช…1.]์†์„ฑ๋ช…, [ํ…Œ์ด๋ธ”๋ช…2.]์†์„ฑ๋ช… ....
FROM ํ…Œ์ด๋ธ”๋ช…1  
   JOIN ํ…Œ์ด๋ธ”๋ช…2
 USING(์†์„ฑ๋ช…)

# 3) NON EQUI JOIN

  • BETWEEN AND, IS NULL, IS NOT NULL, NOT IN, <, >, >=, <= ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
SELECT [ํ…Œ์ด๋ธ”๋ช…1.]์†์„ฑ๋ช…, [ํ…Œ์ด๋ธ”๋ช…2.]์†์„ฑ๋ช… ....
FROM ํ…Œ์ด๋ธ”๋ช…1, ํ…Œ์ด๋ธ”๋ช…2..
WHERE (NON EQUI JOIN ์กฐ๊ฑด)

# 4) SELF JOIN

  • ๊ฐ™์€ ํ…Œ์ด๋ธ”์—์„œ 2๊ฐœ์˜ ์†์„ฑ์„ ์—ฐ๊ฒฐํ•˜์—ฌ EQUI JOIN์„ ํ•œ๋‹ค.
-- ๋ฐฉ๋ฒ•1
SELECT [๋ณ„์นญ1.]์†์„ฑ๋ช…, [๋ณ„์นญ2.]์†์„ฑ๋ช… ....
FROM ํ…Œ์ด๋ธ”๋ช…1 AS ๋ณ„์นญ1,  ํ…Œ์ด๋ธ”๋ช…2 AS ๋ณ„์นญ2 
WHERE ๋ณ„์นญ1.์†์„ฑ๋ช… = ๋ณ„์นญ2.์†์„ฑ๋ช…
 
-- ๋ฐฉ๋ฒ•2
SELECT [๋ณ„์นญ1.]์†์„ฑ๋ช…, [๋ณ„์นญ2.]์†์„ฑ๋ช… ....
FROM ํ…Œ์ด๋ธ”๋ช…1 AS ๋ณ„์นญ1 
  JOIN  ํ…Œ์ด๋ธ”๋ช…2 AS ๋ณ„์นญ2 
    ON ๋ณ„์นญ1.์†์„ฑ๋ช… = ๋ณ„์นญ2.์†์„ฑ๋ช…

# 5) CROSS JOIN

ํ…Œ์ด๋ธ” A์—์„œ ์กฐํšŒ๋˜๋Š” ํ•˜๋‚˜์˜ ํ–‰์— ํ…Œ์ด๋ธ” B์—์„œ ์กฐํšŒ๋˜๋Š” ๋ชจ๋“  ํ–‰์„ ์—ฐ๊ฒฐํ•˜์—ฌ ์กฐ์ธํ•œ๋‹ค.

SELECT [ํ…Œ์ด๋ธ”๋ช…1.]์†์„ฑ๋ช…, [ํ…Œ์ด๋ธ”๋ช…2.]์†์„ฑ๋ช… ....
FROM ํ…Œ์ด๋ธ”๋ช…1 CROSS JOIN ํ…Œ์ด๋ธ”๋ช…2

# 3. OUTER JOIN

์ผ๋ฐ˜์ ์ธ INNER JOIN๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ํ•œ ์ชฝ์˜ ๋ฐ์ดํ„ฐ์—์„œ JOIN์กฐ๊ฑด์— ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฐ’๊นŒ์ง€ ๋ชจ๋‘ ๊ฐ€์ ธ์™€ ์กฐํšŒํ•œ๋‹ค.

# 1) LEFT OUTER JOIN

JOIN ์ˆ˜ํ–‰ ์‹œ ์™ผ์ชฝ์— ํ‘œ๊ธฐ๋œ ํ…Œ์ด๋ธ”์€ JOIN์กฐ๊ฑด์— ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฐ’๊นŒ์ง€ ๋ชจ๋‘ ์กฐํšŒ๋œ๋‹ค.

-- ๋ฐฉ๋ฒ•1
SELECT [ํ…Œ์ด๋ธ”๋ช…1.]์†์„ฑ๋ช…, [ํ…Œ์ด๋ธ”๋ช…2.]์†์„ฑ๋ช… ....
FROM ํ…Œ์ด๋ธ”๋ช…1  LEFT OUTER JOIN   ํ…Œ์ด๋ธ”๋ช…2..
ON ํ…Œ์ด๋ธ”๋ช…1.์†์„ฑ๋ช… = ํ…Œ์ด๋ธ”๋ช…2.์†์„ฑ๋ช…
 
-- ๋ฐฉ๋ฒ•2
SELECT [ํ…Œ์ด๋ธ”๋ช…1.]์†์„ฑ๋ช…, [ํ…Œ์ด๋ธ”๋ช…2.]์†์„ฑ๋ช… ....
FROM ํ…Œ์ด๋ธ”๋ช…1, ํ…Œ์ด๋ธ”๋ช…2
WHERE ํ…Œ์ด๋ธ”๋ช…1.์†์„ฑ๋ช… = ํ…Œ์ด๋ธ”๋ช…2.์†์„ฑ๋ช…(+)

# 2) RIGHT OUTER JOIN

JOIN ์ˆ˜ํ–‰ ์‹œ ์˜ค๋ฅธ์ชฝ์— ํ‘œ๊ธฐ๋œ ๋ฐ์ด๋ธ”์€ JOIN์กฐ๊ฑด์— ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฐ’๊นŒ์ง€ ๋ชจ๋‘ ์กฐํšŒ๋œ๋‹ค.

-- ๋ฐฉ๋ฒ•1
SELECT [ํ…Œ์ด๋ธ”๋ช…1.]์†์„ฑ๋ช…, [ํ…Œ์ด๋ธ”๋ช…2.]์†์„ฑ๋ช… ....
FROM ํ…Œ์ด๋ธ”๋ช…1  RIGHT  OUTER JOIN   ํ…Œ์ด๋ธ”๋ช…2..
ON ํ…Œ์ด๋ธ”๋ช…1.์†์„ฑ๋ช… = ํ…Œ์ด๋ธ”๋ช…2.์†์„ฑ๋ช…
 
-- ๋ฐฉ๋ฒ•2
SELECT [ํ…Œ์ด๋ธ”๋ช…1.]์†์„ฑ๋ช…, [ํ…Œ์ด๋ธ”๋ช…2.]์†์„ฑ๋ช… ....
FROM ํ…Œ์ด๋ธ”๋ช…1, ํ…Œ์ด๋ธ”๋ช…2
WHERE ํ…Œ์ด๋ธ”๋ช…1.์†์„ฑ๋ช…(+) = ํ…Œ์ด๋ธ”๋ช…2.์†์„ฑ๋ช…

# 3) FULL OUTER JOIN

์กฐ์ธ ์ˆ˜ํ–‰ ์‹œ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๊ฐ’์„ ์ฝ์–ด JOIN์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. LEFT OUTER JOIN๊ณผ RIGHT OUTER JOIN์˜ ๊ฒฐ๊ณผ๋ฅผ ํ•ฉ์ง‘ํ•ฉ์œผ๋กœ ์ฒ˜๋ฆฌํ•œ ๊ฒฐ๊ณผ์™€ ๋™์ผํ•˜๋‹ค.

SELECT [ํ…Œ์ด๋ธ”๋ช…1.]์†์„ฑ๋ช…, [ํ…Œ์ด๋ธ”๋ช…2.]์†์„ฑ๋ช… ....
FROM ํ…Œ์ด๋ธ”๋ช…1  FULL  OUTER JOIN   ํ…Œ์ด๋ธ”๋ช…2..
ON ํ…Œ์ด๋ธ”๋ช…1.์†์„ฑ๋ช… = ํ…Œ์ด๋ธ”๋ช…2.์†์„ฑ๋ช…
Last Updated: 12/5/2020, 11:36:44 AM