day5

์ƒ์€๐Ÿ‘ธยท2023๋…„ 12์›” 1์ผ
0

๋šœ๋ฒ…๋šœ๋ฒ… ์„ธ๋ฒˆ์งธ

๋ชฉ๋ก ๋ณด๊ธฐ
5/11
post-thumbnail

๐Ÿ“– JOIN

JOIN => ํ…Œ์ด๋ธ” ๊ฐ„์˜ ์—ฐ๊ฒฐ
๋””ํดํŠธ ๊ฐ’ : INNER JOIN

๊ฐ™๋‹ค -> ๋“ฑ๊ฐ€์กฐ์ธ Equal Join
๊ฐ™๋‹ค๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ -> ๋น„๋“ฑ๊ฐ€์กฐ์ธ Non-Equal Join

INNER JOIN : ์กฐ์ธ ๊ธฐ์ค€์œผ๋กœ ์‚ฌ์šฉํ•œ ์ปฌ๋Ÿผ์ด NULL์ธ ๊ฒฝ์šฐ, ํ•ด๋‹น ํ–‰์ด ์‚ฌ๋ผ์ง‘๋‹ˆ๋‹ค.
OUTER JOIN : ์กฐ์ธ ๊ธฐ์ค€ ์ปฌ๋Ÿผ์ด ์—†์–ด๋„, ํ•ด๋‹น ํ–‰ ์œ ์ง€
NULL๊นŒ์ง€ ๋‹ค ํ‘œ์‹œ๋จ! -> ๋‘ํ…Œ์ด๋ธ” ๋ชจ๋“  ํ–‰์ด ๋‹ค๋œจ๋Š”๊ฑฐ์ง€

(1) INNER JOIN

1) 1๋ฒˆ ๋ฐฉ์‹

    SELECT ๊ฐ€์ ธ์˜ฌ ์ปฌ๋Ÿผ๋ช… *
    FROM ํ…Œ์ด๋ธ”A ๋ณ„๋ช…A
    INNER JOIN ํ…Œ์ด๋ธ”B ๋ณ„๋ช…B 
    ON ๋ณ„๋ช…A.์ปฌ๋Ÿผ๋ช… = ๋ณ„๋ช…B.์ปฌ๋Ÿผ๋ช…; --๊ณตํ†ต์ ์ธ ์ปฌ๋Ÿผ

2) 2๋ฒˆ ๋ฐฉ์‹

    SELECT ๊ฐ€์ ธ์˜ฌ ์ปฌ๋Ÿผ๋ช… *
    FROM ํ…Œ์ด๋ธ”A ๋ณ„๋ช…A, ํ…Œ์ด๋ธ”B ๋ณ„๋ช…B
    WHERE ๋ณ„๋ช…A.์ปฌ๋Ÿผ๋ช… = ๋ณ„๋ช…B.์ปฌ๋Ÿผ๋ช…; --๊ณตํ†ต์ ์ธ ์ปฌ๋Ÿผ

(2) OUTER JOIN

1) 1๋ฒˆ ๋ฐฉ์‹

    SELECT ๊ฐ€์ ธ์˜ฌ ์ปฌ๋Ÿผ๋ช… *
    FROM ํ…Œ์ด๋ธ”A ๋ณ„๋ช…A
    LEFT OUTER JOIN ํ…Œ์ด๋ธ”B ๋ณ„๋ช…B 
    RIGHT OUTER JOIN ํ…Œ์ด๋ธ”B ๋ณ„๋ช…B 
    ON ๋ณ„๋ช…A.์ปฌ๋Ÿผ๋ช… = ๋ณ„๋ช…B.์ปฌ๋Ÿผ๋ช…; --๊ณตํ†ต์ ์ธ ์ปฌ๋Ÿผ

2) 2๋ฒˆ ๋ฐฉ์‹

	SELECT ๊ฐ€์ ธ์˜ฌ ์ปฌ๋Ÿผ๋ช… *
    FROM ํ…Œ์ด๋ธ”A ๋ณ„๋ช…A, ํ…Œ์ด๋ธ”B ๋ณ„๋ช…B
    WHERE ๋ณ„๋ช…A.์ปฌ๋Ÿผ๋ช… = ๋ณ„๋ช…B.์ปฌ๋Ÿผ๋ช…(+); --LEFT OUTER JOIN (A์—๋‹ค๊ฐ€ B๋ฅผ ์•„์šฐํ„ฐ์กฐ์ธ)
    
    WHERE ๋ณ„๋ช…A.์ปฌ๋Ÿผ๋ช…(+) = ๋ณ„๋ช…B.์ปฌ๋Ÿผ๋ช…; --RIGHT OUTER JOIN (B์—๋‹ค๊ฐ€ A๋ฅผ ์•„์šฐํ„ฐ์กฐ์ธ) -> ์ด๋ ‡๊ฒŒ ์“ฐ๋Š”๊ฑฐ๋ณด๋‹จ
    WHERE ๋ณ„๋ช…B.์ปฌ๋Ÿผ๋ช… = ๋ณ„๋ช…A.์ปฌ๋Ÿผ๋ช…(+); --์ด๋ ‡๊ฒŒ ์“ฐ๋Š”๊ฒŒ ๋”๋‚ซ๋‹ค!

โœ๏ธ ์—ฐ์Šต๋ฌธ์ œ

--ํ•™์ƒํ…Œ์ด๋ธ”๊ณผ ๊ต์ˆ˜ํ…Œ์ด๋ธ”์„ ๊ต์ˆ˜๋ฒˆํ˜ธ ๊ธฐ์ค€์œผ๋กœ joinํ•ด์„œ
--ํ•™๋ฒˆ ํ•™์ƒ์ด๋ฆ„ ๊ต์ˆ˜๋ฒˆํ˜ธ ๊ต์ˆ˜์ด๋ฆ„์„ ์„ ํƒํ•ด์„œ ์ถœ๋ ฅํ•˜์„ธ์š”
==> ๋“ฑ๊ฐ€์กฐ์ธ..!

--1๋ฒˆ๋ฐฉ์‹

SELECT 
    s.studno ํ•™๋ฒˆ,
    s.name ํ•™์ƒ์ด๋ฆ„,
    p.profno ๊ต์ˆ˜๋ฒˆํ˜ธ,
    p.name ๊ต์ˆ˜์ด๋ฆ„
FROM student s
INNER JOIN professor p
ON s.profno = p.profno; --๊ณตํ†ต์ ์ธ ์ปฌ๋Ÿผ

--<<<<< 2๋ฒˆ๋ฐฉ์‹ >>>>>>

SELECT 
    s.studno ํ•™๋ฒˆ,
    s.name ํ•™์ƒ์ด๋ฆ„,
    p.profno ๊ต์ˆ˜๋ฒˆํ˜ธ,
    p.name ๊ต์ˆ˜์ด๋ฆ„
FROM student s , professor p
WHERE s.profno = p.profno; --๊ณตํ†ต์ ์ธ ์ปฌ๋Ÿผ

๐Ÿ’ป

โœ๏ธ ์—ฐ์Šต๋ฌธ์ œ

--๋ช‡์ ์ผ๋•Œ ๋ช‡ํ•™์ ์ด๋‹ค!
--๊ฐ ์ ์ˆ˜๋ณ„ ํ•™์ ์„ ํ•จ๊ป˜ ํ‘œ์‹œ
--ํ•™๋ฒˆ ์ ์ˆ˜ ํ•™์ 
==> ๋น„๋“ฑ๊ฐ€์กฐ์ธ..!

SELECT *
FROM score; --์ ์ˆ˜ํ…Œ์ด๋ธ”

SELECT *
FROM hakjum; --ํ•™์ ํ…Œ์ด๋ธ”
SELECT
    s.studno,
    s.total,
    h.grade
FROM score s, hakjum h
WHERE s.total BETWEEN h.min_point AND h.max_point;
--์ ์ˆ˜๊ฐ€ ์ด ์‚ฌ์ด์— ๋“ค์–ด๊ฐ€๋Š”์ง€

๐Ÿ’ป

โœ๏ธ ์—ฐ์Šต๋ฌธ์ œ

--student, score, hakjum ํ…Œ์ด๋ธ”์„ ํ™œ์šฉํ•˜์—ฌ ๋‹ค์Œ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.
--๊ฐ ํ•™์ƒ ๋ณ„๋กœ ์ ์ˆ˜๊ฐ€ ๋ช‡์ ์ด๊ณ  ๊ทธ ์ ์ˆ˜๊ฐ€ ์–ด๋–ค ํ•™์ ์ธ์ง€๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.
--ํ•™๋ฒˆ ์ด๋ฆ„ ์ ์ˆ˜ ํ•™์  ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜์„ธ์š”.

SELECT 
	st.studno ํ•™๋ฒˆ,
    st.name ์ด๋ฆ„,
    sc.total ์ ์ˆ˜,
    h.grade ํ•™์ 
FROM student st, score sc, hakjum h
WHERE st.studno = sc.studno 
	  AND sc.total BETWEEN h.min_point AND h.max_point
ORDER BY sc.total desc;

๐Ÿ’ป


๐Ÿ“Œ

INNER JOIN : ๊ต์ง‘ํ•ฉ ๋ถ€๋ถ„๋งŒ!
OUTER JOIN -> LEFT : LEFT + ๊ต์ง‘ํ•ฉ // WHERE RIGHT IS NULL ์ธ ๊ฒฝ์šฐ๋Š” LEFT๋งŒ!!(๊ต์ง‘ํ•ฉ X)
           -> RIGHT : RIGHT + ๊ต์ง‘ํ•ฉ // WHERE LEFT IS NULL ์ธ ๊ฒฝ์šฐ๋Š” RIGHT๋งŒ!!(๊ต์ง‘ํ•ฉ X)
           LEFT OUTER JOIN: ์™ผ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๊ฐ’์ด ์ถœ๋ ฅ๋˜๋Š” ์กฐ์ธ
           RIGHT OUTER JOIN: ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๊ฐ’์ด ์ถœ๋ ฅ๋˜๋Š” ์กฐ์ธ
profile
๋’ค์ฃฝ๋ฐ•์ฃฝ ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€