๐ฏ 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 |
'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 |