Database

[Database] JOIN

quedevel 2023. 3. 25. 18:44
728x90
๋ฐ˜์‘ํ˜•

๐ŸŽฏ JOIN


์กฐ์ธ(Join)์€ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•

โญ๏ธ ํ•™์ƒ(student) ํ…Œ์ด๋ธ”

id name major
1 Alice English
2 Bob Math
3 Claire Science
4 David History

โญ๏ธ ์ˆ˜์—…(course) ํ…Œ์ด๋ธ”

id course_name instructor
1 English 101 Smith
2 Math 101 Johnson
3 Science 101 Lee
4 History 101 Kim

INNER JOIN


INNER JOIN์€ ๋‘ ํ…Œ์ด๋ธ”์—์„œ ์ผ์น˜ํ•˜๋Š” ๊ฐ’๋งŒ์„ ๊ฐ€์ ธ์™€์„œ ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

SELECT student.name, course.course_name
FROM student
INNER JOIN course ON student.major = course.course_name;

๊ฒฐ๊ณผ:

name course_name
Alice English 101
Bob Math 101
Claire Science 101
David History 101

LEFT OUTER JOIN


LEFT OUTER JOIN์€ ์™ผ์ชฝ ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€์œผ๋กœ, ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฐ’๋งŒ ๊ฐ€์ ธ์˜ค๋ฉฐ, ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ์—๋Š” NULL ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

SELECT student.name, course.course_name
FROM student
LEFT OUTER JOIN course ON student.major = course.course_name;

๊ฒฐ๊ณผ:

name course_name
Alice English 101
Bob Math 101
Claire Science 101
David History 101
Emma NULL

RIGHT OUTER JOIN


RIGHT OUTER JOIN์€ ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€์œผ๋กœ, ์™ผ์ชฝ ํ…Œ์ด๋ธ”๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฐ’๋งŒ ๊ฐ€์ ธ์˜ค๋ฉฐ, ์™ผ์ชฝ ํ…Œ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ์—๋Š” NULL ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

SELECT student.name, course.course_name
FROM student
RIGHT OUTER JOIN course ON student.major = course.course_name;

๊ฒฐ๊ณผ:

name course_name
Alice English 101
Bob Math 101
Claire Science 101
David History 101
NULL Art 101

FULL OUTER JOIN


FULL OUTER JOIN์€ ์™ผ์ชฝ ํ…Œ์ด๋ธ”๊ณผ ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์—์„œ ๋ชจ๋“  ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋ฉฐ, ์–‘์ชฝ ํ…Œ์ด๋ธ”์—์„œ ์ผ์น˜ํ•˜๋Š” ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ์—๋Š” NULL ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

SELECT student.name, course.course_name
FROM student
FULL OUTER JOIN course ON student.major = course.course_name;

๊ฒฐ๊ณผ:

name course_name
Alice English 101
Bob Math 101
Claire Science 101
David History 101
Emma NULL
NULL Art 101

CROSS JOIN


CROSS JOIN์€ ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๋ชจ๋“  ์กฐํ•ฉ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ•™์ƒ ํ…Œ์ด๋ธ”๊ณผ ์ˆ˜์—… ํ…Œ์ด๋ธ”์—์„œ ๋ชจ๋“  ํ•™์ƒ๊ณผ ๋ชจ๋“  ์ˆ˜์—…์„ ์กฐํ•ฉํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ฟผ๋ฆฌ

SELECT student.name, course.course_name
FROM student
CROSS JOIN course;

๊ฒฐ๊ณผ:

name course_name
Alice English 101
Bob English 101
Claire English 101
David English 101
Alice Math 101
Bob Math 101
Claire Math 101
David Math 101
Alice Science 101
Bob Science 101
Claire Science 101
David Science 101
Alice History 101
Bob History 101
Claire History 101
David History 101
Alice Art 101
Bob Art 101
Claire Art 101
David Art 101

SELF JOIN


SELF JOIN์€ ๋™์ผํ•œ ํ…Œ์ด๋ธ”์„ ๋‘ ๋ฒˆ ์กฐ์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ•™์ƒ ํ…Œ์ด๋ธ”์—์„œ ๊ฐ™์€ ์ „๊ณต์„ ๊ฐ€์ง„ ํ•™์ƒ์„ ์ฐพ๋Š” ์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

SELECT s1.name, s2.name
FROM student s1
JOIN student s2 ON s1.major = s2.major AND s1.id <> s2.id;

๊ฒฐ๊ณผ:

name name
Alice Bob
Bob Alice
Claire David
David Claire
728x90
๋ฐ˜์‘ํ˜•

'Database' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Database] SQL vs NoSQL  (0) 2023.03.26
[Database] SQL Injection  (0) 2023.03.25
[Database] Key  (0) 2023.03.25
[Database] MySQL to Oracle  (0) 2021.07.31
[Database] Oracle 12  (0) 2020.02.27