๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
DB/Oracle

[Oracle] ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์ข…๋ฅ˜ - ( ์Šค์นผ๋ผ, ์ธ๋ผ์ธ๋ทฐ, ์ค‘์ฒฉ )

by yunamom 2023. 6. 6.
๋ฐ˜์‘ํ˜•

์„œ๋ธŒ์ฟผ๋ฆฌ๋ž€  ๋ฌด์—‡์ธ๊ฐ€?

 

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

 

*์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ์นผ๋Ÿผ์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ฉ”์ธ์ฟผ๋ฆฌ๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์นผ๋Ÿผ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

๋ฉ”์ธ์ฟผ๋ฆฌ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์นผ๋Ÿผ์„ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” (์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ, ์ธ๋ผ์ธ ๋ทฐ)

์งˆ์˜ ๊ฒฐ๊ณผ์— ์„œ๋ธŒ์ฟผ๋ฆฌ ์นผ๋Ÿผ์„ ํ‘œ์‹œํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์กฐ์ธ ๋ฐฉ์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ ํ•จ์ˆ˜, ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ ๋“ฑ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

์ธ๋ผ์ธ ๋ทฐ๋Š” ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋œ ํ…Œ์ด๋ธ”์ด๋ฏ€๋กœ ์ธ๋ผ์ธ ๋ทฐ์˜ ์ปฌ๋Ÿผ(์ฆ‰, ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์ปฌ๋Ÿผ)์€ SQL๋ฌธ์—์„œ ์ฐธ์กฐ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์ข…๋ฅ˜๋Š” ํฌ๊ฒŒ 3๊ฐ€์ง€(์Šค์นผ๋ผ, ์ธ๋ผ์ธ ๋ทฐ, ์ค‘์ฒฉ) ๋กœ ์–ด๋Š ์œ„์น˜์—์„œ ์‚ฌ์šฉํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ ๋ถ„๋ฅ˜๋œ๋‹ค.

์ข…๋ฅ˜ ์„ค๋ช…
์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ
(Scalar Suquery)
SELECT ์ ˆ์— ์œ„์น˜ํ•˜๋ฉฐ ํ•œ ๋ ˆ์ฝ”๋“œ๋‹น ์ •ํ™•ํžˆ ํ•˜๋‚˜์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ (๋‹จ์ผํ–‰, ๋‹จ์ผ์นผ๋Ÿผ์„ ๋ฐ˜ํ™˜)
์ธ๋ผ์ธ ๋ทฐ
(Inline View)
FROM ์ ˆ์— ์œ„์น˜ํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ๊ฒฐ๊ณผ๋Š” ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”๋กœ ๋ฆฌํ„ด๋˜์–ด์•ผ ํ•œ๋‹ค.
์ค‘์ฒฉ ์„œ๋ธŒ์ฟผ๋ฆฌ
(Nested Subquery)
WHERE ์ ˆ์— ์œ„์น˜ํ•˜๋ฉฐ ๊ฒฐ๊ณผ์ง‘ํ•ฉ์„ ํ•œ์ •ํ•˜๊ธฐ ์œ„ํ•œ ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ, ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ๋ฉ”์ธ์ฟผ๋ฆฌ ์ปฌ๋Ÿผ์„ ์ฐธ์กฐํ• ๋•Œ ์ƒ๊ด€๊ด€๊ณ„ ์žˆ๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ๋ผ๊ณ  ํ•œ๋‹ค. (๋‹จ์ผํ–‰, ๋‹ค์ค‘ํ–‰์„ ๋ฐ˜ํ™˜)

 

 

1. ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ (Scalar Subquery)

SELECT ์ ˆ์— ์œ„์น˜ํ•˜๋ฉฐ ์นผ๋Ÿผ์˜ ์—ญํ• ์„ ํ•˜๋ฏ€๋กœ ํ•ด๋‹น ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋Š” ๋ฐ˜๋“œ์‹œ ๋‹จ์ผ ํ–‰ or ๋‹จ์ผ ๊ฐ’์œผ๋กœ ๋ฆฌํ„ด๋˜์–ด์•ผ ํ•œ๋‹ค.

*๋‹ค์ค‘ ํ–‰ ๊ฐ’์ด ์กฐํšŒ๋˜๋ฉด "ORA-01427: single-row subquery returns more than one row" ๋ผ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ์ฃผ์˜

# ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ (Scalar Subquery)
# ์˜ˆ) ๊น€ํ•™์ƒ์˜ ํ•™์  ์กฐํšŒ

SELECT ์ด๋ฆ„,(
           SELECT ํ•™์ƒํ•™์ .ํ•™์ 
           FROM ํ•™์ƒํ•™์ 
           WHERE ํ•™์ƒํ•™์ .ํ•™์ƒID = ํ•™์ƒ.ํ•™์ƒID
           )AS ํ•™์ 
FROM ํ•™์ƒ WHERE ์ด๋ฆ„ = '๊น€ํ•™์ƒ';

 

2. ์ธ๋ผ์ธ ๋ทฐ (Inline View)

FROM ์ ˆ์— ์œ„์น˜ํ•˜๋ฉฐ ํ…Œ์ด๋ธ”์˜ ์ผ๋ถ€ ๋ฐ์ดํ„ฐ๋งŒ์„ ๋ถˆ๋Ÿฌ์™€ ๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋ฅผ ๋‹จ์ˆœํ™” ํ• ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ฟผ๋ฆฌ์˜ ๊ฐ€๋…์„ฑ์ด ๋†’๋‹ค.

ํ•ด๋‹น ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋Š” ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”๋กœ ๋ฆฌํ„ด๋˜์–ด์•ผ ํ•œ๋‹ค.

์ธ๋ผ์ธ ๋ทฐ๋Š” SQL๋ฌธ์ด ์‹คํ–‰๋ ๋•Œ ์ž„์‹œ์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ๋™์ ์ธ ๋ทฐ ๋กœ ์ด์™€ ๊ฐ™์€ ๋ทฐ๋ฅผ Dynamic View ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.(↔ ์ •์  ๋ทฐ(Static VIew))

 

๋‹จ์ : SQL์„ ์ข€๋” ๋น ๋ฅธ ์†๋„๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ๊ฒƒ์ด ์ธ๋ผ์ธ ๋ทฐ์ด์ง€๋งŒ, SQL์„ ๊ตฌ์„ฑํ• ๋•Œ ์ž˜๋ชป ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์•ก์„ธ์Šค ์†๋„๋ฅผ ์ €ํ•˜ ์‹œํ‚ค๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์œผ๋ฏ€๋กœ ์‚ฌ์šฉ์— ์ฃผ์˜

# ์ธ๋ผ์ธ ๋ทฐ (Inline View)
# ์˜ˆ) ํ•™์ ์ด A์ธ ํ•™์ƒ๋“ค์„ ์กฐํšŒ

SELECT ์ด๋ฆ„, ํ•™์ 
FROM (
     SELECT ํ•™์ƒ.ํ•™์ƒ์ด๋ฆ„, ํ•™์ƒํ•™์ .ํ•™์ 
     FROM ํ•™์ƒ, ํ•™์ƒํ•™์ 
     WHERE ํ•™์ƒ.ํ•™์ƒ๋ฒˆํ˜ธ = ํ•™์ƒํ•™์ .ํ•™์ƒ๋ฒˆํ˜ธ
     AND ํ•™์ƒํ•™์ .ํ•™์  = 'A'
     );

 

3. ์ค‘์ฒฉ ์„œ๋ธŒ์ฟผ๋ฆฌ (Nested Subquery)

WHERE ์ ˆ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ๋ฉ”์ธ์ฟผ๋ฆฌ ํ…Œ์ด๋ธ”์˜ ํŠน์ • ์ปฌ๋Ÿผ ๊ฐ’๊ณผ ๋น„๊ตํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

๋‹จ์ผํ–‰ / ๋‹ค์ค‘ํ–‰ ๋‘˜ ๋‹ค ๋ฆฌํ„ด์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

# ์ค‘์ฒฉ ์„œ๋ธŒ์ฟผ๋ฆฌ (Nested Subquery)
# ์˜ˆ) ๊น€ํ•™์ƒ์˜ ํ•™์ ์„ ์กฐํšŒ

SELECT ํ•™์ƒํ•™์ .ํ•™์ 
FROM ํ•™์ƒํ•™์ 
WHERE ํ•™์ƒํ•™์ .ํ•™์ƒ๋ฒˆํ˜ธ = (
                      SELECT ํ•™์ƒ.ํ•™์ƒ๋ฒˆํ˜ธ
                      FROM ํ•™์ƒ
                      WHERE ํ•™์ƒ.์ด๋ฆ„ = '๊น€ํ•™์ƒ'
                      )
300x250

์ฝ”๋“œ