Computer Science/๐Ÿ’พ Database

[DB] SQL ์กฐ์ธ(Join) ๊ฐœ๋…, ์ข…๋ฅ˜

J1Yun 2023. 3. 24. 14:59
728x90

์กฐ์ธ(Join)

  • ํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด์˜ ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ์—ด๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ\
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—๋Š” ์„œ๋กœ ๊ด€๊ณ„์žˆ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”๋กœ ๋‚˜๋‰˜์–ด ์ €์žฅ๋˜์žˆ์–ด ์กฐ์ธ์„ ํ†ตํ•ด ๊ฐ ํ…Œ์ด๋ธ”์„ ์—ฐ๊ฒฐํ•˜์—ฌ ํšจ๊ณผ์ ์œผ๋กœ ๊ฒ€์ƒ‰
  • ๋‘ ํ…Œ์ด๋ธ”์˜ ์กฐ์ธ์„ ์œ„ํ•ด์„œ๋Š”๊ธฐ๋ณธํ‚ค(PRIMARY KEY, PK)์™€ ์™ธ๋ž˜ํ‚ค(FOREIGN KEY, FK) ๊ด€๊ณ„๋กœ ๋งบ์–ด์ ธ์•ผ ํ•จ

 

์กฐ์ธ์˜ ์ข…๋ฅ˜

  • ๋‚ด๋ถ€ ์กฐ์ธ(INNER JOIN): ๋‘ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•  ๋•Œ, ๋‘ ํ…Œ์ด๋ธ”์— ๋ชจ๋‘ ์ง€์ •ํ•œ ์—ด์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์–ด์•ผ ํ•จ
  • ์™ธ๋ถ€ ์กฐ์ธ(OUTER JOIN): ๋‘ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•  ๋•Œ, 1๊ฐœ์˜ ํ…Œ์ด๋ธ”์—๋งŒ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์–ด๋„ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ด
  • ์ƒํ˜ธ ์กฐ์ธ(CROSS JOIN): ํ•œ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰๊ณผ ๋‹ค๋ฅธ ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰์„ ์กฐ์ธํ•˜๋Š” ๊ธฐ๋Šฅ
  • ์ž์ฒด ์กฐ์ธ(SELF JOIN): ์ž์‹ ์ด ์ž์‹ ๊ณผ ์กฐ์ธํ•œ๋‹ค๋Š” ์˜๋ฏธ๋กœ, 1๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉ

 

โ€ป ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Œ

 

 

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

  • ๊ฐ€์žฅ ํ”ํ•œ ๊ฒฐํ•ฉ ๋ฐฉ์‹์ด๋ฉฐ ๊ธฐ๋ณธ ์กฐ์ธ ํ˜•์‹์œผ๋กœ ๊ฐ„์ฃผ
  • ๊ณตํ†ต์ ์ธ ๋ถ€๋ถ„๋งŒ SELECT

๋ช…์‹œ์  ์กฐ์ธ ํ‘œํ˜„(explicit)

  • Join ํ‚ค์›Œ๋“œ์™€ ํ•จ๊ป˜ On ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ
SELECT *
FROM STUDENT_TABLE
INNER JOIN DEPARTMENT_TABLE
ON STUDENT_TABLE.DEPARTMENT_ID = DEPARTMENT_TABLE.DEPARTMENT_ID;

์•”์‹œ์  ์กฐ์ธ ํ‘œํ˜„(implicit)

  • From ์ ˆ์—์„œ ํ…Œ์ด๋ธ”์„ ๋ถ„๋ฆฌํ•˜๋Š” ์ปด๋งˆ(,)๋ฅผ ์‚ฌ์šฉ
SELECT *
FROM STUDENT_TABLE, DEPARTMENT_TABLE
WHERE STUDENT_TABLE.DEPARTMENT_ID = DEPARTMENT_TABLE.DEPARTMENT_ID;

 

 

์™ธ๋ถ€ ์กฐ์ธ(OUTER JOIN)

  • ์กฐ์ธ ๋Œ€์ƒ ํ…Œ์ด๋ธ”์—์„œ ํŠน์ • ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ชจ๋‘ ํ•„์š”ํ•œ ์ƒํ™ฉ์—์„œ ํšจ๊ณผ์ ์œผ๋กœ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ์ƒ์„ฑ
  • ๋‘ ํ…Œ์ด๋ธ”์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ „์ฒด ๋ถ€๋ถ„ SELECT

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

  • ์™ผ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ์ƒ์„ฑ
# ๊ณตํ†ต๋ถ€๋ถ„ ํฌํ•จ
SELECT *
FROM STUDENT_TABLE S LEFT OUTER JOIN DEPARTMENT_TABLE D
ON S.DEPARTMENT_ID = D.DEPARTMENT_ID;

# ๊ณตํ†ต๋ถ€๋ถ„ ๋ถˆํฌํ•จ
SELECT *
FROM STUDENT_TABLE S LEFT OUTER JOIN DEPARTMENT_TABLE D
ON S.DEPARTMENT_ID = D.DEPARTMENT_ID
WHERE D.DEPARTMENT_ID IS NULL;

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

  • ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ์ƒ์„ฑ
# ๊ณตํ†ต๋ถ€๋ถ„ ํฌํ•จ
SELECT *
FROM STUDENT_TABLE S RIGHT OUTER JOIN DEPARTMENT_TABLE D
ON S.DEPARTMENT_ID = D.DEPARTMENT_ID;

# ๊ณตํ†ต๋ถ€๋ถ„ ๋ถˆํฌํ•จ
SELECT *
FROM STUDENT_TABLE S RIGHT OUTER JOIN DEPARTMENT_TABLE D
ON S.DEPARTMENT_ID = D.DEPARTMENT_ID
WHERE S.DEPARTMENT_ID IS NULL;

์™„์ „ ์™ธ๋ถ€ ์กฐ์ธ(FULL OUTER JOIN)

  • ๋‘ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ์ƒ์„ฑ
# ๊ณตํ†ต๋ถ€๋ถ„ ํฌํ•จ
-- ORACLE
SELECT *
FROM STUDENT_TABLE S FULL OUTER JOIN DEPARTMENT_TABLE D
ON S.DEPARTMENT_ID = D.DEPARTMENT_ID;
-- MYSQL
SELECT *
FROM STUDENT_TABLE S LEFT OUTER JOIN DEPARTMENT_TABLE D
ON S.DEPARTMENT_ID = D.DEPARTMENT_ID
UNION
SELECT *
FROM STUDENT_TABLE S RIGHT OUTER JOIN DEPARTMENT_TABLE D
ON S.DEPARTMENT_ID = D.DEPARTMENT_ID;

# ๊ณตํ†ต๋ถ€๋ถ„ ๋ถˆํฌํ•จ
-- ORACLE
SELECT *
FROM STUDENT_TABLE S FULL OUTER JOIN DEPARTMENT_TABLE D
ON S.DEPARTMENT_ID = D.DEPARTMENT_ID
WHERE S.DEPARTMENT_ID IS NULL OR D.DEPARTMENT_ID IS NULL;
-- MYSQL
SELECT *
FROM STUDENT_TABLE S LEFT OUTER JOIN DEPARTMENT_TABLE D
ON S.DEPARTMENT_ID = D.DEPARTMENT_ID
WHERE D.DEPARTMENT_ID IS NULL
UNION
SELECT *
FROM STUDENT_TABLE S RIGHT OUTER JOIN DEPARTMENT_TABLE D
ON S.DEPARTMENT_ID = D.DEPARTMENT_ID
WHERE S.DEPARTMENT_ID IS NULL;

 

 

์ƒํ˜ธ ์กฐ์ธ(CROSS JOIN)

  • ํ•œ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰๊ณผ ๋‹ค๋ฅธ ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰์„ ๊ฒฐํ•ฉ์‹œ์ผœ ๋ชจ๋‘ SELECT
  • ์นดํ‹ฐ์…˜ ๊ณฑ(CARTESIAN PRODUCT)์ด๋ผ๊ณ ๋„ ํ•จ
SELECT *
FROM STUDENT_TABLE S CROSS JOIN DEPARTMENT_TABLE D

 

728x90