(์ค๋ช ์ ์ฐ์ผ ํ ์ด๋ธ ์์)
TABLE_A
ID | ALPHABET |
1 | abc |
2 | abcd |
3 | abcde |
TABLE_B
ID | NUM |
2 | 10 |
3 | 20 |
4 | 30 |
๐ INNER JOIN
๊ต์งํฉ์ ์๋ฏธํ๊ณ , ๊ณตํต ๋ถ๋ถ๋ง SELECT๋๋ค.
SELECT A.ID, A.ALPHABET, A.NUM
FROM TABLE_A AS A
INNER JOIN TABLE_B AS B
ON A.ID = B.ID
ID | ALPHABET | NUM |
2 | abcd | 10 |
3 | abcde | 20 |
๐ LEFT OUTER JOIN
(1) ๊ณตํต ๋ถ๋ถ ํฌํจ, ์กฐ์ธ ๊ธฐ์ค ์ผ์ชฝ ํ ์ด๋ธ์ ์ ๋ถ๋ฅผ SELECT ํ๋ค.
SELECT A.ID, A.ALPHABET, A.NUM
FROM TABLE_A AS A
LEFT OUTER JOIN TABLE_B AS B
ON A.ID = B.ID
ID | ALPHABET | NUM |
1 | abc | NULL |
2 | abcd | 10 |
3 | abcde | 20 |
(2) ๊ณตํต ๋ถ๋ถ ์ ์ธ, ์กฐ์ธ ๊ธฐ์ค ์ผ์ชฝ ํ ์ด๋ธ์ ์ ๋ถ๋ฅผ SELECT ํ๋ค.
SELECT A.ID, A.ALPHABET, A.NUM
FROM TABLE_A AS A
LEFT OUTER JOIN TABLE_B AS B
ON A.ID = B.ID
WHERE B.ID IS NULL
ID | ALPHABET | NUM |
1 | abc | NULL |
๐ RIGHT OUTER JOIN
(1) ๊ณตํต ๋ถ๋ถ ํฌํจ, ์กฐ์ธ ๊ธฐ์ค ์ค๋ฅธ์ชฝ ํ ์ด๋ธ์ ์ ๋ถ๋ฅผ SELECT ํ๋ค.
SELECT A.ID, A.ALPHABET, A.NUM
FROM TABLE_A AS A
RIGHT OUTER JOIN TABLE_B AS B
ON A.ID = B.ID
ID | ALPHABET | NUM |
2 | abcd | 10 |
3 | abcde | 20 |
4 | NULL | 30 |
(2) ๊ณตํต ๋ถ๋ถ ์ ์ธ, ์กฐ์ธ ๊ธฐ์ค ์ค๋ฅธ์ชฝ ํ ์ด๋ธ์ ์ ๋ถ๋ฅผ SELECT ํ๋ค.
SELECT A.ID, A.ALPHABET, A.NUM
FROM TABLE_A AS A
RIGHT OUTER JOIN TABLE_B AS B
ON A.ID = B.ID
WHERE A.ID IS NULL
ID | ALPHABET | NUM |
4 | NULL | 30 |
๐ FULL OUTER JOIN
(1) ์ ๋ถ SELECT
SELECT A.ID, A.ALPHABET, A.NUM
FROM TABLE_A AS A
FULL OUTER JOIN TABLE_B AS B
ON A.ID = B.ID
ID | ALPHABET | NUM |
1 | abc | NULL |
2 | abcd | 10 |
3 | abcde | 20 |
4 | NULL | 30 |
(2) ๊ณตํต ๋ถ๋ถ๋ง ์ ์ธํ ์ ๋ถ๋ฅผ SELECT
SELECT A.ID, A.ALPHABET, A.NUM
FROM TABLE_A AS A
FULL OUTER JOIN TABLE_B AS B
ON A.ID = B.ID
WHERE A.ID IS NULL OR B.ID IS NULL
ID | ALPHABET | NUM |
1 | abc | NULL |
4 | NULL | 30 |
๐ ํต์ฌ ํฌ์ธํธ
- INNER๋ ๊ณตํต๋ ๋ถ๋ถ๋ง ๊ฐ์ง๋ฏ๋ก, NULL์ ๊ฐ์ง ์ ์๋ค.
- LEFT๋ ์ค๋ฅธ์ชฝ ํ
์ด๋ธ์๋ง NULL์ด ์์ ์ ์๊ณ ,
RIGHT๋ ์ผ์ชฝ ํ ์ด๋ธ์๋ง NULL์ด ์์ ์ ์์ผ๋ฉฐ,
FULL์ ์์ชฝ ํ ์ด๋ธ ๋ชจ๋์ NULL์ด ์์ ์ ์๋ค.
๋ฐ๋ผ์, ํ ํ ์ด๋ธ์๋ง ์๊ณ ํ ํ ์ด๋ธ์๋ ์๋ ๊ฒ์ ์ฐพ์์ผ ํ ๋๋
LEFT๋ RIGHT์ ์จ์, NULL์ ์ฐพ์ผ๋ฉด ๋๋ค. - LEFT์ RIGHT๋ ๋ฐฉํฅ๋ง ๋ฐ๋ ๊ฒ์ด๋, ํ ๊ฐ์ง๋ง ๊ณต๋ถํด์ ๋๋ ค์ฐ์.
'6๏ธโฃ SQL > SQL ์ ๋ฆฌ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SQL] DDL, DML, DCL, TCL ์์ฝ (0) | 2022.09.15 |
---|---|
[MySQL] ์ฝ๋ฉํ ์คํธ๋ฅผ ์ํ SQL ๋ฌธ๋ฒ (0) | 2022.02.04 |
๋๊ธ