๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[๊ฐœ๋ฐœ] Practice/SQL Database

[SQL / Database] JOIN ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ด๋ธ” ํ•ฉ์น˜๊ธฐ

by Connecting-the-dots 2023. 5. 29.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’กJOIN ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ด๋ธ” ํ•ฉ์น˜๊ธฐ

๐Ÿ’œ JOIN ์—†์ด ํ…Œ์ด๋ธ” ํ•ฉ์น˜๊ธฐ

SELECT ํ…Œ์ด๋ธ”๋ช…1.์ปฌ๋Ÿผ๋ช…1, ํ…Œ์ด๋ธ”๋ช…1.์ปฌ๋Ÿผ๋ช…2, ํ…Œ์ด๋ธ”๋ช…2.์ปฌ๋Ÿผ๋ช…2, ํ…Œ์ด๋ธ”๋ช…2.์ปฌ๋Ÿผ๋ช…3 
FROM ํ…Œ์ด๋ธ”๋ช…1, ํ…Œ์ด๋ธ”๋ช…2 WHERE ํ…Œ์ด๋ธ”๋ช…1.์ปฌ๋Ÿผ๋ช…2 = ํ…Œ์ด๋ธ”๋ช…2.์ปฌ๋Ÿผ๋ช…1 ;
  • SELECT ๋ฌธ๋ฒ•์€ ์‚ฌ์‹ค FROM ๋’ค์— ํ…Œ์ด๋ธ”๋ช…์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ ๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ด ๊ฒฝ์šฐ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”๋กœ ํ•ฉ์ณ์„œ ์ถœ๋ ฅํ•œ๋‹ค.
  • ์ปฌ๋Ÿผ๋„ ์ž์œ ๋กญ๊ฒŒ ์„ ํƒ ๊ฐ€๋Šฅํ•œ๋ฐ ๋‹จ, ์ปฌ๋Ÿผ๋ช…์ด ๊ฒน์น˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋‹ˆ ์ปฌ๋Ÿผ๋ช…๋งŒ ์“ฐ๋Š” ๋Œ€์‹  "ํ…Œ์ด๋ธ”๋ช….์ปฌ๋Ÿผ๋ช…" ๊ณผ ๊ฐ™์ด ์ ์„ ์ฐ์€ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
    (ํŠน์ • ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ์ปฌ๋Ÿผ์„ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด "ํ…Œ์ด๋ธ”๋ช….*" ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.)
  • ๋˜ํ•œ, ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”๋กœ ํ•ฉ์ณ์„œ ๋ณด์—ฌ์ฃผ๋Š” ๋Œ€์‹  ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ํ–‰์˜ ์กฐํ•ฉ์„ ์ถœ๋ ฅํ•˜๋ฏ€๋กœ ํ•„ํ„ฐ๋ง์ด ํ•„์š”ํ•˜๋‹ค๋ฉด WHERE ์กฐ๊ฑด์‹์„ ํ†ตํ•ด ์กฐ๊ฑด์„ ์ฃผ๋ฉด ๋œ๋‹ค.

๐Ÿ’œ INNER JOIN ์œผ๋กœ ํ…Œ์ด๋ธ” ํ•ฉ์น˜๊ธฐ

// INNER JOIN ๋ฌธ๋ฒ•
SELECT ์ถœ๋ ฅํ• ์ปฌ๋Ÿผ๋ช…๋“ค FROM ํ…Œ์ด๋ธ”1 INNER JOIN ํ…Œ์ด๋ธ”2 ON ์กฐ๊ฑด์‹ 

// JOIN ์—†์ด ํ…Œ์ด๋ธ” ํ•ฉ์น˜๊ธฐ ์˜ˆ์‹œ๋ฅผ JOIN ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜์ •ํ•œ๋‹ค๋ฉด?
SELECT ํ…Œ์ด๋ธ”๋ช…1.์ปฌ๋Ÿผ๋ช…1, ํ…Œ์ด๋ธ”๋ช…1.์ปฌ๋Ÿผ๋ช…2, ํ…Œ์ด๋ธ”๋ช…2.์ปฌ๋Ÿผ๋ช…2, ํ…Œ์ด๋ธ”๋ช…2.์ปฌ๋Ÿผ๋ช…3 
FROM ํ…Œ์ด๋ธ”๋ช…1 INNER JOIN ํ…Œ์ด๋ธ”๋ช…2 ON ํ…Œ์ด๋ธ”๋ช…1.์ปฌ๋Ÿผ๋ช…2 = ํ…Œ์ด๋ธ”๋ช…2.์ปฌ๋Ÿผ๋ช…1 ;
  • INNER JOIN ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ณตํ†ต์ ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ…Œ์ด๋ธ”๋“ค์„ ๋ถ™์—ฌ์„œ ์ถœ๋ ฅํ•œ๋‹ค.
  • JOIN ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” ON ๋’ค์— ์กฐ๊ฑด์‹์„ ๋ถ™์—ฌ์„œ ํ•„ํ„ฐ๋ง์„ ํ•˜๋ฉฐ, ON ์„ ๋นผ๋จน์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
    (MySQL ์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฉฐ, ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ํ–‰์˜ ์กฐํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.)
// ํ…Œ์ด๋ธ” 3๊ฐœ ์ด์ƒ ํ•ฉ์น˜๋Š” ๋ฐฉ๋ฒ• 1
SELECT * FROM ํ…Œ์ด๋ธ”1, ํ…Œ์ด๋ธ”2, ํ…Œ์ด๋ธ”3 WHERE ์กฐ๊ฑด1 AND ์กฐ๊ฑด2 ...

// ํ…Œ์ด๋ธ” 3๊ฐœ ์ด์ƒ ํ•ฉ์น˜๋Š” ๋ฐฉ๋ฒ• 2
SELECT * FROM ํ…Œ์ด๋ธ”1 INNER JOIN ํ…Œ์ด๋ธ”2 ON ์กฐ๊ฑด1 INNER JOIN ํ…Œ์ด๋ธ”3 ON ์กฐ๊ฑด2
  • INNER JOIN ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ด๋ธ”์„ ์—ฌ๋Ÿฌ ๊ฐœ ํ•ฉ์น˜๊ณ  ์‹ถ๋‹ค๋ฉด "INNER JOIN ํ…Œ์ด๋ธ”๋ช… ON ์กฐ๊ฑด์‹" ์„ ์—ฌ๋Ÿฌ ๊ฐœ ๋ถ™์ด๋ฉด ๋œ๋‹ค.

๐Ÿ’œ CROSS JOIN ์œผ๋กœ ํ…Œ์ด๋ธ” ํ•ฉ์น˜๊ธฐ

  • ํ…Œ์ด๋ธ”์„ 2๊ฐœ ์ด์ƒ ํ•ฉ์ณ์„œ ์ถœ๋ ฅํ•  ๋•Œ WHERE ์กฐ๊ฑด์‹์ด๋‚˜ ON ์กฐ๊ฑด์‹์„ ๋„ฃ์ง€ ์•Š์œผ๋ฉด ์ž๋™์œผ๋กœ CROSS JOIN ์ด ๋˜์–ด ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”๋กœ ํ•ฉ์ณ์„œ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ํ–‰์˜ ์กฐํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•  ์ผ์€ ๊ฑฐ์˜ ์—†์œผ๋‚˜, ํ…Œ์ŠคํŠธ์šฉ ๋”๋ฏธ๋ฐ์ดํ„ฐ ์ƒ์„ฑํ•  ๋•Œ๋‚˜ ๋‹ค์–‘ํ•œ ์ƒ‰์กฐํ•ฉ์„ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ’œ LEFT (OUTER) JOIN ์œผ๋กœ ํ…Œ์ด๋ธ” ํ•ฉ์น˜๊ธฐ

  • INNER JOIN ๊ฒฐ๊ณผ์™€ ํ•จ๊ป˜, ์กฐ๊ฑด์ด ๋ถˆ์ผ์น˜ํ•˜์—ฌ JOIN ๋˜์ง€ ์•Š๊ณ  ๋‚จ์€ ์™ผ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰์„ ์ถœ๋ ฅํ•  ๋•Œ LEFT JOIN ์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • LEFT JOIN ์‚ฌ์šฉ ์‹œ JOIN ์˜ ์กฐ๊ฑด์ด ๋ถˆ์ผ์น˜ํ•˜์—ฌ ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์˜ ํ–‰์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ NULL ๋กœ ์ฑ„์›Œ์ง„๋‹ค.
  • ํ…Œ์ด๋ธ”๋ผ๋ฆฌ ํ•ฉ์น  ๋•Œ NULL ์ด ๋ฐœ์ƒํ•˜๋Š” ๋ถ€๋ถ„์„ ๋ฏธ๋ฆฌ ์ฐพ๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ’œ RIGHT(OUTER)  JOIN ์œผ๋กœ ํ…Œ์ด๋ธ” ํ•ฉ์น˜๊ธฐ

  • INNER JOIN ๊ฒฐ๊ณผ์™€ ํ•จ๊ป˜ ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰์„ ์ถœ๋ ฅํ•  ๋•Œ RIGHT JOIN ์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • RIGHT JOIN ์‚ฌ์šฉ ์‹œ JOIN ์˜ ์กฐ๊ฑด์ด ๋ถˆ์ผ์น˜ํ•˜์—ฌ ์™ผ์ชฝ ํ…Œ์ด๋ธ”์˜ ํ–‰์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ NULL ๋กœ ์ฑ„์›Œ์ง„๋‹ค.
  • ํ…Œ์ด๋ธ”๋ผ๋ฆฌ ํ•ฉ์น  ๋•Œ NULL ์ด ๋ฐœ์ƒํ•˜๋Š” ๋ถ€๋ถ„์„ ๋ฏธ๋ฆฌ ์ฐพ๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ’œ FULL(OUTER)  JOIN ์œผ๋กœ ํ…Œ์ด๋ธ” ํ•ฉ์น˜๊ธฐ

  • LEFT JOIN ๊ณผ RIGHT JOIN ๊ฒฐ๊ณผ๋ฅผ ํ•ฉํ•ด์„œ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ FULL JOIN ์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • FULL JOIN ์€ MySQL, MariaDB, SQLite ์—์„  ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€ํ•˜๋ฏ€๋กœ, LEFT JOIN ๋ฐ RIGHT JOIN ํ•œ ๊ฒฐ๊ณผ๋ฅผ UNION ์—ฐ์‚ฐ์ž๋กœ ๋ฌถ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋Œ€์ฒดํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.
728x90
๋ฐ˜์‘ํ˜•