Notice
Recent Posts
Recent Comments
Today
Total
11-11 06:37
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[DB] Correlation subquery ์˜ ์‹คํ–‰ ์ˆœ์„œ ๋ณธ๋ฌธ

๐Ÿ’ป Study !/Database System

[DB] Correlation subquery ์˜ ์‹คํ–‰ ์ˆœ์„œ

WonderJay 2023. 3. 30. 21:17
728x90
๋ฐ˜์‘ํ˜•
SMALL

Q.

select course_id

from section as S

where semester = ’Fall’ and year= 2017 and

    exists (select *

              from section as T

              where semester = ’Spring’ and year= 2018 and S.course_id= T.course_id);

Correlation subquery๋Š” ๋ณธ๋ฌธ(๋ฐ”๊นฅ ์งˆ์˜)๊ฐ€ ๋จผ์ € ์‹คํ–‰๋˜๊ณ , ํ•˜์œ„ ์งˆ์˜๊ฐ€ ์‹คํ–‰๋˜๊ณ  ๋‹ค์‹œ ํ•˜์œ„ ์งˆ์˜์˜ ๊ฒฐ๊ณผ๊ฐ’์ด ๋ฐ”๊นฅ ์งˆ์˜์—์„œ exists๋กœ ํ‰๊ฐ€๋˜๋Š” ์ˆœ์„œ๋กœ ์ง„ํ–‰๋˜๋‚˜์š”? ๋งŒ์•ฝ ๊ทธ๋ ‡๋‹ค๋ฉด, 2017๋…„ ๊ฐ€์„์— ์—ด๋ฆฐ ๊ณผ๋ชฉ์ด n๊ฐœ๊ฐ€ ์žˆ์œผ๋ฉด ํ•˜์œ„ ์งˆ์˜๊ฐ€ n๋ฒˆ๋งŒํผ ๋ฐ˜๋ณต๋˜๋ฉด์„œ exists ๊ฒฐ๊ณผ true์ด๋ฉด ๊ทธ course_id๊ฐ€ ๊ฒฐ๊ณผ์— ์ถ”๊ฐ€๋˜๋Š” ๋ฐฉ์‹์ด ๋งž๋‚˜์š”?

 

A.

Correlation subquery ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” (1) Main query ๋ฅผ ๋จผ์ € ์‹คํ–‰ํ•˜๊ณ , (2) ์ด๋•Œ ์ฝ์–ด๋“ค์ธ ๊ฐ’์„ ์ด์šฉํ•˜์—ฌ Subquery ์—์„œ ์ด์šฉํ•ด์„œ ์–ป์€ ๊ฒฐ๊ณผ๊ฐ’์„ (3) Main query ์—์„œ ์ด์šฉํ•˜๋Š” ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

 

select course_id

from section as S

where semester = ’Fall’ and year= 2017 and

    exists (select *

              from section as T

              where semester = ’Spring’ and year= 2018 and S.course_id = T.course_id);

 

 

 

์œ„ SQL ๋ฌธ์˜ ๊ฒฝ์šฐ์—๋Š” ๋ฉ”์ธ์ฟผ๋ฆฌ์—์„œ ์–ป์€ ๊ฐ’์„ ์ด์šฉํ•˜์—ฌ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰์‹œ์ผœ์„œ ๊ฐ’์„ ์–ป์–ด๋‚ด๊ณ  ์ด๋ฅผ ๋‹ค์‹œ ๋ฉ”์ธ์ฟผ๋ฆฌ์—์„œ์‚ฌ์šฉํ•˜๋Š”, ์ฆ‰ ๋ฉ”์ธ์ฟผ๋ฆฌ์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ์„œ๋กœ ์ฐธ์กฐํ•˜๋Š” Correlation subquery ์ž…๋‹ˆ๋‹ค.

1. ๋ฉ”์ธ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•ด์„œ ๊ฐ’์„ ์–ป์Œ

2. ๊ทธ ๊ฐ’์„ ์ด์šฉํ•ด์–ด ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰์‹œํ‚ด.

3. ์ด๋กœ๋ถ€ํ„ฐ ์–ป์€ ๊ฐ’์œผ๋กœ ๋ฉ”์ธ ์ฟผ๋ฆฌ์˜ where clause ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ํ‰๊ฐ€ํ•˜์—ฌ, ํ•ด๋‹น ๊ฒฐ๊ณผ๊ฐ’์„ ์ถ”์ถœํ•  ์ง€ ๋ง์ง€๋ฅผ ๊ฒฐ์ •.

4. ์œ„ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•จ. (๋ฉ”์ธ ์ฟผ๋ฆฌ์—์„œ ์กด์žฌํ•˜๋Š” ๊ฐ’ ๋งŒํผ)

 

๊ทธ๋Ÿฌ๋ฏ€๋กœ 2017 ๋…„ ๊ฐ€์„์— ์—ด๋ฆฐ ๊ณผ๋ชฉ์ด n ๊ฐœ๊ฐ€ ์žˆ๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด

๋ฉ”์ธ์ฟผ๋ฆฌ์—์„œ ์–ป์€ ๊ฐ’์ด n ๊ฐœ์ธ ๊ฒƒ์ด๊ณ ,

๊ทธ ๊ฐ’์„ ์ด์šฉํ•ด์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด๋ฏ€๋กœ,

์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ n ๋ฒˆ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‹คํ–‰๋˜๋ฉด์„œ

๋ฉ”์ธ์ฟผ๋ฆฌ์˜ where ์˜ exist ์— ๋”ฐ๋ผ์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”์ถœ๋œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
LIST

'๐Ÿ’ป Study ! > Database System' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Natural Join ์˜ ์œ„ํ—˜์„ฑ? (03/22)  (0) 2023.03.22
[#07] Normalization  (0) 2022.07.18
[#06] Database Design Using the E-R Model  (0) 2022.07.18
[#01] Introduction  (0) 2022.07.06
Comments