SQL JOIN์ ํ๋ ์ด์์ ํ ์ด๋ธ์์ ๋ฐ์ดํฐ๋ฅผ ๊ฒฐํฉํ๋ ๋ฐ ์ฌ์ฉ๋๋ SQL ๋ช ๋ น๋ฌธ์ ๋๋ค. ์ด๋ฅผ ํตํด ๊ด๋ จ๋ ์ ๋ณด๊ฐ ์ฌ๋ฌ ํ ์ด๋ธ์ ๋ถ์ฐ๋์ด ์๋ ๊ฒฝ์ฐ์๋ ๋ฐ์ดํฐ๋ฅผ ํจ๊ณผ์ ์ผ๋ก ๊ฒ์ํ ์ ์์ต๋๋ค.
Implicit join
from์ ์๋ ํ ์ด๋ธ๋ค๋ง ๋์ดํ๊ณ where ์ ์์ ๋ ํ ์ด๋ธ์ ์์ฑ์ JOINํ ์กฐ๊ฑด์ ๋ช ์ํ๋ ๋ฐฉ์์ผ๋ก ์ค๋๋ ์คํ์ผ์ ๋ช ๋ น์ด์ด๋ค.
๋ณต์กํ Join ์ฟผ๋ฆฌ๋ค์ ์์ฑํ๋ค๋ณด๋ฉด ์๋ชป๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ ๊ฐ๋ฅ์ฑ์ด ํฌ๋ค.
Explicit join
implicit join ๋ฐฉ์์ ๋๋ฌด ํท๊ฐ๋ฆฌ๊ธฐ ์ฝ๋ค. ์ฐจ๋ผ๋ฆฌ FROM ์ ์ JOIN, ON ํค์๋๋ฅผ ๋ช ์ํ๋ ๋ฐฉ์
1. INNER JOIN
๋ ํ ์ด๋ธ ๊ฐ์ join condition์ ๋ง์กฑํ๋ tuple๋ก ์ด๋ฃจ์ด์ง ํ ์ด๋ธ (+ ์กฐ๊ฑด์ ๋ง๋ ํํ์ ๊ธฐ์ค์ผ๋ก JOIN ํค์๋ ๊ธฐ์ค ์ผ์ชฝ๊ณผ ์ค๋ฅธ์ชฝ์ผ๋ก ํ ์ด๋ธ์ด ํฉ์ณ์ง๋ค)
2. OUTER JOIN
๋ ํ ์ด๋ธ์์ join condition์ ๋ง์กฑํ์ง ์๋ tuple๋ค๋ ํฌํจํ๋ ํ ์ด๋ธ (LEFT, RIGHT, FULL) ์ ํ
PS) FULL OUTER JOIN์ MySQL์์๋ ๋ฏธ์ง
3. USING
๋ ํ ์ด๋ธ ๊ฐ์ ๋์ผํ ์ด ์ด๋ฆ์ ๊ฐ์ง ์ด์ ๊ธฐ์ค์ผ๋ก JOINํ ์ ์์ต๋๋ค.
์ INNER JOIN์ ์์๋ฅผ ๋ณด๋ฉด dept_id์ department์ id๊ฐ์ด ์ค๋ณต๋๊ณ ์์ต๋๋ค.
์ด๋ฐ ๊ฒฝ์ฐ์ id๊ฐ์ dept_id๋ก ๋ฐ๊พผ ํ์,
SELECT * FROM employee E INNER JOIN department D USING (dept_id);
4. Natural join
Join condition์ ๋ช ์ ์์ด, ๋ ํ ์ด๋ธ์ '๊ฐ์ ์ด๋ฆ์ ์์ฑ'์ด ์ ๋๋ก '์์ฑ ์์ ๊ฐ์ ๊ฐ'์ผ๋ก ์ด๋ฃจ์ด์ง ํ ์ด๋ธ
5. Cross join
๋ ํ ์ด๋ธ ๊ฐ์ ๊ฐ๋ฅํ ๋ชจ๋ ์กฐํฉ์ ๋ฐํํฉ๋๋ค. ์ฆ, ์ฒซ ๋ฒ์งธ ํ ์ด๋ธ์ ๊ฐ ํ์ด ๋ ๋ฒ์งธ ํ ์ด๋ธ์ ๋ชจ๋ ํ๊ณผ ๊ฒฐํฉ
MySQL์์๋ cross join = inner join = join ๋ง์ฝ ON, USING์ด ์ฌ์ฉ๋๋ฉด INNER JOIN, ์ฌ์ฉ ์๋๋ฉด CROSS
- implicit cross join: FROM table1, table2
ex) SELECT * FROM employee, department
- explicit cross join: FROM table1 CROSS JOIN table2 (์ง์ ํ์ํ๋ค๋ ๋ป)
JOIN EXAMPLE
1) ID๊ฐ 1003์ธ ๋ถ์์ ์ํ๋ ์์ง์ ์ค์ ๋ฆฌ๋๋ฅผ ์ ์ธํ ๋ถ์์์ ID, ์ด๋ฆ, ์ฐ๋ด์ ์๊ณ ์ถ๋ค
2) ID๊ฐ 2001์ธ ํ๋ก์ ํธ์ ์ฐธ์ฌํ ์์ง์๋ค์ ์ด๋ฆ, ์ง๊ตฐ, ์์ ๋ถ์๋ฅผ ์๊ณ ์ถ๋ค
Implicit Join
An implicit join refers to joining tables based on conditions specified in the WHERE clause of a SQL query.
It relies on matching values in columns of the joined tables without explicitly using the JOIN keyword.
Explicit Join
An explicit join involves using the JOIN keyword in a SQL query to combine rows from two or more tables based on a related column.
Inner Join
An inner join returns only the rows that have matching values in both joined tables. Rows without matching values in both tables are excluded from the result set.
Outer Join
An outer join returns all rows from one table and the matched rows from another table.
Rows without matches are still included, and the missing values are filled with NULLs.
Natural Join
A natural join automatically matches columns with the same name in the joined tables.
It simplifies the join process by avoiding the need to specify the columns for joining explicitly.
Cross Join
A cross join returns the Cartesian product of the two tables involved, meaning all possible combinations of rows from both tables. It results in a large result set and is less commonly used than other types of joins.
'Computer Science > Database' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
12 Trigger (0) | 2024.01.13 |
---|---|
9 ORDER BY, GROUP BY (feat. HAVING) (0) | 2024.01.12 |
7 READ (Feat. Subquery) (0) | 2024.01.11 |
6 Create(Insert), Update, Delete (1) | 2024.01.11 |
5 CRUD (Create, Read, Update and Delete) + Practice tables (0) | 2024.01.08 |
๋๊ธ