๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค/SQL

[Oracle] Join์œผ๋กœ ๋ฐ์ดํ„ฐ ์—ฐ๊ฒฐ, Set ์—ฐ์‚ฐ์ž

by OR15A 2024. 1. 5.
์บก์ณ์— ๋‚˜์˜จ SQL๋ฌธ๋ฒ•์€ ANSI(๋ฏธ๊ตญํ‘œ์ค€ํ˜‘ํšŒ) SQL ํ‘œ์ค€๊ณผ๋Š” ๋‹ค๋ฅธ ์˜›๋‚  ๋ฌธ๋ฒ•์ด ์‚ฌ์šฉ๋œ ๊ฒƒ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 
์ฝ”๋“œ ๋ธ”๋Ÿญ์„ ์ค‘์‹ฌ์œผ๋กœ ๋ด์ฃผ์„ธ์š”

 

Join
  • 1๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”๋กœ๋ถ€ํ„ฐ ์—ฐ๊ด€๋œ ๋ฐ์ดํ„ฐ๋ฅผ
  • ๊ฒ€์ƒ‰ํ•ด์˜ค๋Š” ๋ฐฉ๋ฒ•. PK์™€ FK๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๋…ผ๋ฆฌ์ ์ธ ๊ฐ’๋“ค์˜ ์—ฐ๊ด€์œผ๋กœ ์„ฑ๋ฆฝ๋„ ๊ฐ€๋Šฅํ•จ.
  • ์—ฐ๊ด€๋œ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ.
  • ํ…Œ์ด๋ธ”๊ฐ„์˜ ์—ฐ๊ฒฐ๊ณ ๋ฆฌ๊ฐ€ ์žˆ์„ ๋•Œ ํ•ฉ์ณ์„œ ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ”์„ ๋ณด์—ฌ์คŒ
  • ์ •๊ทœํ™”: ํ…Œ์ด๋ธ”์„ ์ชผ๊ฐœ๋Š” ๊ฒƒ. ๋ฐ์ดํ„ฐ์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•จ. ์ค‘๋ณต์ด ๋งŽ์œผ๋ฉด ๋ณ€๊ฒฝ ์‹œ ๋ณ€๊ฒฝํšŸ์ˆ˜๊ฐ€ ๋งŽ์•„์ง€๋ฉฐ ์‹ค์ˆ˜์™€ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ์Œ.
  • Join(๋ถ™์ด๊ธฐ) ↔ ์ •๊ทœํ™”(์ชผ๊ฐœ๊ธฐ)

 

 

Join ๋ฐฉ๋ฒ• ์˜๋ฏธ ์˜ˆ์‹œ
EquiJoin ์นผ๋Ÿผ ๊ฐ„์˜ ๊ฐ’๋“ค์ด
์„œ๋กœ ์ •ํ™•ํžˆ ์ผ์น˜ํ•  ๋•Œ
์ง์›์ •๋ณด์˜ ๋ถ€์„œ๋ฒˆํ˜ธ
&
๋ถ€์„œ์ด๋ฆ„
&
๋ถ€์„œ์˜ ์ง€์—ญ๋ฒˆํ˜ธ์™€ ์ง€์—ญ์ด๋ฆ„
Non-EquiJoin ์นผ๋Ÿผ๊ฐ„์˜ ๊ฐ’์ด ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ.
=์—ฐ์‚ฐ์ž๋Š” ์‚ฌ์šฉ๋ถˆ๊ฐ€.
Between And๋“ฑ์˜
๋‹ค๋ฅธ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์กฐ๊ฑด ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ธ ๋ฐ์ดํ„ฐ ํ™•์ธํ•จ
๊ธ‰์—ฌ์˜
๋“ฑ๊ธ‰์„ ๋งค๊ธธ ๋•Œ
Outer Join Join์กฐ๊ฑด ๋งŒ์กฑ ์•ˆํ•ด๋„
๋ชจ๋“  ํ–‰์„ ๋ณด๊ณ ์‹ถ์„ ๋•Œ. (+)์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•จ. =์—ฐ์‚ฐ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ถ€์กฑํ•œ ์ชฝ์— ์œ„์น˜์‹œํ‚จ๋‹ค.
In X, Or X
์ •์ˆ˜๊ธฐ ๋งค๋‹ˆ์ €์™€ ๊ณ ๊ฐ ๊ฐ„์˜ ๋งค์นญ.
๋…ธ๋Š”๋งค๋‹ˆ์ € or ์‹ ๊ทœ๊ณ ๊ฐ ํ™•์ธ๊ฐ€๋Šฅํ•จ
Self Join ๊ฐ™์€ ํ…Œ์ด๋ธ”์˜ ํ–‰๋“ค์„
Joinํ•˜๊ณ ์‹ถ์„ ๋•Œ.
ํ•œ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ ์•ˆ์— ๊ณ„์ธต์ด ์žˆ์–ด์•ผํ•จ.
(์ƒ์‚ฌ-๋ถ€ํ•˜, ๋Œ€๋ถ„๋ฅ˜-์ค‘๋ถ„๋ฅ˜-์†Œ๋ถ„๋ฅ˜)
์ •์ˆ˜๊ธฐ ๋งค๋‹ˆ์ €์™€ ๊ณ ๊ฐ ๊ฐ„์˜ ๋งค์นญ.
๋…ธ๋Š”๋งค๋‹ˆ์ € or ์‹ ๊ทœ๊ณ ๊ฐ ํ™•์ธ๊ฐ€๋Šฅํ•จ

 

 

โ‘  EquiJoin
  • PK์™€ FK ์ผ์น˜

EquiJoin
EquiJoin

[EquiJoin (๋™๋“ฑ ์กฐ์ธ)]
SELECT *
FROM TableA
INNER JOIN TableB ON TableA.commonColumn = TableB.commonColumn;

 

 

 

โ‘ก Non-EquiJoin
  • ๋“ฑํ˜ธ ์ด์™ธ์˜ ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ.
  • ๋‘ ์ปฌ๋Ÿผ์ด ๊ฐ’์ด ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜์ง€ ์•Š์„ ๋•Œ.

Non-EquiJoin
Non-EquiJoin

[Non-EquiJoin (๋น„๋™๋“ฑ ์กฐ์ธ)]
SELECT *
FROM TableA
JOIN TableB ON TableA.commonColumn > TableB.commonColumn;

 

 

 

 

โ‘ข Outer Join
  • ๋‘ ํ…Œ์ด๋ธ” ์กฐ์ธํ•  ๋•Œ ์–ด๋Š ํ•œ์ชฝ์˜ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์–ด๋„ ๊ฒฐ๊ณผ ํ™•์ธ ํ•  ๋•Œ.

Outer Join
Outer Join

[Left Outer Join]
SELECT *
FROM TableA
LEFT JOIN TableB ON TableA.commonColumn = TableB.commonColumn;


[Right Outer Join]
SELECT *
FROM TableA
RIGHT JOIN TableB ON TableA.commonColumn = TableB.commonColumn;


[Full Outer Join]
SELECT *
FROM TableA
FULL OUTER JOIN TableB ON TableA.commonColumn = TableB.commonColumn;

 

 

 

 

โ‘ฃ Self Join 
  • ํ…Œ์ด๋ธ” ํ•˜๋‚˜๋ฅผ Alias๋ถ™์—ฌ์„œ ์—ฌ๋Ÿฌ๊ฐœ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•ด์„œ ์กฐ์ธํ•จ. ํšŸ์ˆ˜ ์ œํ•œ ์—†์Œ.

Self Join
Self Join

[Self Join (์ž๊ธฐ ์ž์‹  ์กฐ์ธ)]
SELECT A.*, B.*
FROM TableA A, TableA B
WHERE A.commonColumn = B.commonColumn AND A.id != B.id;

 

 


Set ์—ฐ์‚ฐ์ž
Union ๊ฐ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์˜ ํ•ฉ์ง‘ํ•ฉ
Union All ๊ฐ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์˜ ํ•ฉ์ง‘ํ•ฉ
+์ค‘๋ณต๋ฐ์ดํ„ฐ(ํ•ฉ์ง‘ํ•ฉ๋ถ€๋ถ„ํ•œ๋ฒˆ ๋”).
๊ต์ง‘ํ•ฉ์ด ์—†์œผ๋ฉด Union All ์“ฐ๊ธฐ.
Union๋ณด๋‹ค ์„ฑ๋Šฅ์ด ์ข‹๋‹ค.
๋‘ ํ…Œ์ด๋ธ” ๋ถ™์ด๋Š” 
Intersect ๊ฐ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์˜ ๊ต์ง‘ํ•ฉ
Minus ๋‘ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์˜ ์ฐจ์ง‘ํ•ฉ

Union,   Union All

  • ์ฒซ๋ฒˆ์งธ Select์˜ ์นผ๋Ÿผ ๊ฐœ์ˆ˜&ํƒ€์ž…๊ณผ ๋‘๋ฒˆ์งธ Select์˜ ์นผ๋Ÿผ ๊ฐœ์ˆ˜&ํƒ€์ž…์ด ์ผ์น˜ํ•ด์•ผํ•จ.
  • ์„œ๋กœ ๋‹ค๋ฅธํ…Œ์ด๋ธ”, ๊ฐ™์€ํ…Œ์ด๋ธ” Set ๊ฐ€๋Šฅ.
  • ์ถœ๋ ฅ์นผ๋Ÿผ์˜ Heading์€ ์ฒซ๋ฒˆ์งธ Select์˜ ์นผ๋Ÿผ๋ช….
  • Order By๋Š” ๋งˆ์ง€๋ง‰์— ํ•œ๋ฒˆ๋งŒ ๊ธฐ์ˆ ํ•จ.

Union All
Union All