๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Computer Science/Database

8 JOIN

by Dowon Kang 2024. 1. 12.

SQL JOIN์€ ํ•˜๋‚˜ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” SQL ๋ช…๋ น๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ด€๋ จ๋œ ์ •๋ณด๊ฐ€ ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์— ๋ถ„์‚ฐ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋„ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Implicit join 

from์ ˆ์—๋Š” ํ…Œ์ด๋ธ”๋“ค๋งŒ ๋‚˜์—ดํ•˜๊ณ  where ์ ˆ์—์„œ ๋‘ ํ…Œ์ด๋ธ”์˜ ์†์„ฑ์„ JOINํ•œ ์กฐ๊ฑด์„ ๋ช…์‹œํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์˜ค๋ž˜๋œ ์Šคํƒ€์ผ์˜ ๋ช…๋ น์–ด์ด๋‹ค.

๋ณต์žกํ•œ Join ์ฟผ๋ฆฌ๋“ค์„ ์ž‘์„ฑํ•˜๋‹ค๋ณด๋ฉด ์ž˜๋ชป๋œ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ๊ฐ€๋Šฅ์„ฑ์ด ํฌ๋‹ค.

 

 

Explicit join

implicit join ๋ฐฉ์‹์€ ๋„ˆ๋ฌด ํ—ท๊ฐˆ๋ฆฌ๊ธฐ ์‰ฝ๋‹ค. ์ฐจ๋ผ๋ฆฌ FROM ์ ˆ์— JOIN, ON ํ‚ค์›Œ๋“œ๋ฅผ ๋ช…์‹œํ•˜๋Š” ๋ฐฉ์‹ 

 

 

   1. INNER JOIN

      ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์— join condition์„ ๋งŒ์กฑํ•˜๋Š” tuple๋กœ ์ด๋ฃจ์–ด์ง„ ํ…Œ์ด๋ธ” (+ ์กฐ๊ฑด์— ๋งž๋Š” ํŠœํ”Œ์„ ๊ธฐ์ค€์œผ๋กœ JOIN ํ‚ค์›Œ๋“œ ๊ธฐ์ค€        ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ…Œ์ด๋ธ”์ด ํ•ฉ์ณ์ง„๋‹ค)

์กฐ๊ฑด์ด ๋งž์•„์•ผ JOIN ๋˜๊ธฐ ๋•Œ๋ฌธ์— NULL๊ฐ’์„ ๊ฐ€์ง„ 15๋ฒˆ DOWON์€ ์ œ์™ธ

 

   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์˜ ๋ช…์‹œ ์—†์ด, ๋‘ ํ…Œ์ด๋ธ”์— '๊ฐ™์€ ์ด๋ฆ„์˜ ์†์„ฑ'์ด ์„ ๋‘๋กœ '์†์„ฑ ์•ˆ์— ๊ฐ™์€ ๊ฐ’'์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ํ…Œ์ด๋ธ”

์„ ๋‘๋Š” NATURAL ์†์„ฑ, INNER JOIN์ด๊ธฐ์— ์™ผ์ชฝ์—๋Š” employee ๊ทธ๋ฆฌ๊ณ  ์˜ค๋ฅธ์ชฝ์—๋Š” department

 

 

   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

๋Œ“๊ธ€