1 ๋ถ„ ์†Œ์š”

JOIN์ด๋ž€

JOIN(์กฐ์ธ) ์ด๋ž€, ํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด์˜ ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ์กฐํ•ฉํ•ด ํ•˜๋‚˜์˜ ์—ด๋กœ ํ‘œํ˜„ ํ•œ ๊ฒƒ์ด๋‹ค.
๋”ฐ๋ผ์„œ ์กฐ์ธ์€ ํ…Œ์ด๋ธ”๋กœ์„œ ์ €์žฅ๋˜๊ฑฐ๋‚˜, ๊ทธ ์ž์ฒด๋กœ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฐ๊ณผ ์…‹์„ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค.
์กฐ์ธ์€ ๋ฐฉ์‹์— ๋”ฐ๋ผ INNER JOIN, OUTER JOIN, LEFT JOIN, RIGHT JOIN ๋“ฑ์œผ๋กœ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.

JOIN์˜ ํ•„์š”์„ฑ

๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(RDB)์˜ ๊ตฌ์กฐ์  ํŠน์ง•์œผ๋กœ ์ •๊ทœํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ์˜๋ฏธ์žˆ๋Š” ๋ฐ์ดํ„ฐ์˜ ์ง‘ํ•ฉ์œผ๋กœ ํ…Œ์ด๋ธ”์ด ๊ตฌ์„ฑ๋˜๊ณ , ๊ฐ ํ…Œ์ด๋ธ”๋ผ๋ฆฌ๋Š” ๊ด€๊ณ„(Relationship)์„ ๊ฐ–๊ฒŒ๋œ๋‹ค.
์ด๋Ÿฌํ•œ ํŠน์ง•์œผ๋กœ ์ธํ•ด ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ์ €์žฅ๊ณต๊ฐ„์˜ ํšจ์œจ์„ฑ๊ณผ ํ™•์žฅ์„ฑ์ด ํ–ฅ์ƒ๋œ๋‹ค.
ํ•˜์ง€๋งŒ, ๋ฐ˜๋Œ€๋กœ ์ƒ๊ฐํ•ด๋ณด๋ฉด ์„œ๋กœ ๊ด€๊ณ„์žˆ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”๋กœ ๋‚˜๋‰˜์–ด ์ €์žฅ๋˜๋ฏ€๋กœ ๊ฐ ํ…Œ์ด๋ธ”์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์กฐ์ธ์ด ํ•„์š”ํ•ด์ง„๋‹ค.

๋‚ด๋ถ€์กฐ์ธ: INNER JOIN

INNER JOIN ์€ ๊ฐ€์žฅ ํ”ํ•œ ๊ฒฐํ•ฉ ๋ฐฉ์‹์ด๋ฉฐ, ๊ธฐ๋ณธ ์กฐ์ธ ํ˜•์‹์œผ๋กœ ๊ฐ„์ฃผ๋œ๋‹ค.
๋‚ด๋ถ€์กฐ์ธ์€ ์กฐ์ธ ๊ตฌ๋ฌธ์— ๊ธฐ๋ฐ˜ํ•œ 2๊ฐœ์˜ ํ…Œ์ด๋ธ”(A, B)์˜ ์ปฌ๋Ÿผ ๊ฐ’์„ ๊ฒฐํ•ฉํ•จ์œผ๋กœ์จ ์ƒˆ๋กœ์šด ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•œ๋‹ค.
๋ช…์‹œ์  ์กฐ์ธ ํ‘œํ˜„๊ณผ ์•”์‹œ์  ์กฐ์ธ ํ‘œํ˜„์˜ 2๊ฐ€์ง€ ๋ฐฉ์‹์˜ ์กฐ์ธ ๊ตฌ๋ฌธ์ด ์žˆ๋‹ค.

[๋ช…์‹œ์  ์กฐ์ธ ํ‘œํ˜„]

SELECT * FROM employee INNER JOIN department ON employee.DepartmentID = department.DepartmentID;

[์•”์‹œ์  ์กฐ์ธ ํ‘œํ˜„]

SELECT * FROM employee, department WHERE employee.DepartmentID = department.DepartmentID;

์™ธ๋ถ€์กฐ์ธ: OUTER JOIN

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

์™ผ์ชฝ ์™ธ๋ถ€์กฐ์ธ: LEFT OUTER JOIN

์กฐ์ธ ์ˆ˜ํ–‰ ์‹œ, ๋จผ์ € ํ‘œ๊ธฐ๋œ ์ขŒ์ธก ํ…Œ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋จผ์ € ์ฝ์€ ํ›„, ๋‹ค์Œ์— ์˜ค๋Š” ํ…Œ์ด๋ธ”์—์„œ ์กฐ์ธ ๋Œ€์ƒ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜จ๋‹ค.
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-08-18 แ„‹แ…ฉแ„’แ…ฎ 4 52 31

[์˜ˆ์‹œ]

SELECT * FROM employee LEFT OUTER JOIN department ON employee.DepartmentID = department.DepartmentID;

์ฆ‰ A LEFT OUTER JOIN B์ธ ๊ฒฝ์šฐ A, B ํ…Œ์ด๋ธ” ๊ฐ„ ์กฐ์ธ ์กฐ๊ฑด์ด ๋งž์ง€ ์•Š๋Š” A์™€ B ํ…Œ์ด๋ธ”์˜ ๋ ˆ์ฝ”๋“œ ์ค‘ A๋Š” ๋ฌด์กฐ๊ฑด ๊ฒฐ๊ณผ์…‹์— ๋ชจ๋‘ ํฌํ•จ๋œ๋‹ค.

์˜ค๋ฅธ์ชฝ ์™ธ๋ถ€์กฐ์ธ: RIGHT OUTER JOIN

์กฐ์ธ ์ˆ˜ํ–‰ ์‹œ LEFT JOIN๊ณผ ๋ฐ˜๋Œ€์˜ ๊ฐœ๋…์ด๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค.
LEFT JOIN๊ณผ ๋ฐ˜๋Œ€๋กœ ์šฐ์ธก ํ…Œ์ด๋ธ”์ด ๊ธฐ์ค€์ด ๋˜์–ด ๊ฒฐ๊ณผ ์…‹์„ ์ƒ์„ฑํ•œ๋‹ค.
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-08-18 แ„‹แ…ฉแ„’แ…ฎ 4 55 04

[์˜ˆ์‹œ]

SELECT * FROM employee RIGHT OUTER JOIN department ON employee.DepartmentID = department.DepartmentID;

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ A RIGHT OUTER JOIN B์ผ ๊ฒฝ์šฐ A, B ํ…Œ์ด๋ธ” ๊ฐ„ ์กฐ์ธ ์กฐ๊ฑด์ด ๋งž์ง€ ์•Š๋Š” A์™€ B ํ…Œ์ด๋ธ”์˜ ๋ ˆ์ฝ”๋“œ ์ค‘ B ํ…Œ์ด๋ธ”์˜ ๋ ˆ์ฝ”๋“œ๋Š” ๋ฌด์กฐ๊ฑด ๊ฒฐ๊ณผ์…‹์— ๋ชจ๋‘ ํฌํ•จ๋œ๋‹ค.

์ฐธ๊ณ ์ž๋ฃŒ

[DataBase] ์กฐ์ธ(Join)์ด๋ž€ ?

[DataBase] OUTER JOIN (์™ธ๋ถ€์กฐ์ธ) LEFT,RIGHT,FULL JOIN

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ