๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT์ž๊ฒฉ์ฆ ๊ณต๋ถ€/SQLD ์˜ˆ์ƒ๋ณต์›

[SQLD] 45ํšŒ ๊ธฐ์ถœ ๋‹ต์•ˆ ์˜ˆ์ƒ๋ณต์›/ํ•ด์„ค

by yunamom 2022. 5. 29.
๋ฐ˜์‘ํ˜•

 

์•ˆ๋…•ํ•˜์„ธ์š” yunamom ์ž…๋‹ˆ๋‹ค :D

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” 2022๋…„ 5์›” 28์ผ ํ† ์š”์ผ์— ์‹œํ—˜๋ณธ SQLD 45ํšŒ ์˜ˆ์ƒ๋ณต์›/ํ•ด์„ค ์„ ์ž‘์„ฑํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป

 

1๊ณผ๋ชฉ ๊ฐ๊ด€์‹/์ฃผ๊ด€์‹ ์˜ˆ์ƒ ๋ณต์› [ CLOSE ]

*๋ฌธ์ œ์˜ ์ˆœ์„œ๋Š” ๋‹ค๋ฅผ์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค.

 

1. ๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง ์‹œ ์œ ์˜์ ์œผ๋กœ ์ ์ ˆํ•˜์ง€ ์•Š์€ ๊ฒƒ์€? (์ž๊ฒฉ ๊ฒ€์ • 3๋ฒˆ๊ณผ ์œ ์‚ฌ)

์ •๋‹ต→ ์„ฑ๋Šฅ์„ ์œ„ํ•œ ๋ฐ˜์ •๊ทœํ™”๋ฅผ ๊ณ ๋ คํ•œ๋‹ค. (์œ ์˜์ ์œผ๋กœ ์ ์ ˆํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.)

๊ตฌ๋ถ„ ์„ค๋ช…
์ค‘๋ณต ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋žŒ, ์‹œ๊ฐ„ ๊ทธ๋ฆฌ๊ณ  ์žฅ์†Œ๋ฅผ ํŒŒ์•…ํ•˜๋Š”๋ฐ ๋„์›€์„ ์ค€๋‹ค ์ด๋Ÿฌํ•œ ์ง€์‹์‘์šฉ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์—ฌ๋Ÿฌ ์žฅ์†Œ์— ๊ฐ™์€ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ์ž˜๋ชป์„ ํ•˜์ง€ ์•Š๋„๋ก ํ•œ๋‹ค.
๋น„์œ ์—ฐ์„ฑ ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์„ ์–ด๋–ป๊ฒŒ ์„ค๊ณ„ํ–ˆ๋Š๋ƒ์— ๋”ฐ๋ผ ์‚ฌ์†Œํ•œ ์—…๋ฌด๋ณ€ํ™”์—๋„ ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์ด ์ˆ˜์‹œ๋กœ ๋ณ€๊ฒฝ๋จ์œผ๋กœ์จ ์œ ์ง€๋ณด์ˆ˜์˜ ์–ด๋ ค์›€์„ ๊ฐ€์ค‘์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
๋น„์ผ๊ด€์„ฑ ๋ฐ์ดํ„ฐ์˜ ์ค‘๋ณต์ด ์—†๋”๋ผ๋„ ๋น„์ผ๊ด€์„ฑ์€ ๋ฐœ์ƒํ•œ๋‹ค. ๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง์„ ํ•  ๋•Œ ๋ฐ์ดํ„ฐ์™€ ๋ฐ์ดํ„ฐ๊ฐ„ ์ƒํ˜ธ ์—ฐ๊ด€ ๊ด€๊ณ„์— ๋Œ€ํ•œ ๋ช…ํ™•ํ•œ ์ •์˜๋Š” ์ด๋Ÿฌํ•œ ์œ„ํ—˜์„ ์‚ฌ์ „์— ์˜ˆ๋ฐฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.

 

 

2. ์•„๋ž˜์˜ ์„ค๋ช… ์ค‘ ์†์„ฑ์— ๋Œ€ํ•œ ์„ค๋ช…์ด ๊ฐ€์žฅ ์ ์ ˆํ•˜์ง€ ์•Š์€ ๊ฒƒ์€? (์ž๊ฒฉ ๊ฒ€์ • ๋ฌธ์ œ 16๋ฒˆ)

[์„ค๋ช…]
์šฐ๋ฆฌ์€ํ–‰์€ ์˜ˆ๊ธˆ๋ถ„๋ฅ˜(์ผ๋ฐ˜์˜ˆ๊ธˆ, ํŠน๋ณ„์˜ˆ๊ธˆ ๋“ฑ)์˜ ์›๊ธˆ, ์˜ˆ์น˜๊ธฐ๊ฐ„, ์ด์ž์œจ์„ ๊ด€๋ฆฌํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.
๋˜ํ•œ ์›๊ธˆ์— ๋Œ€ํ•œ ์ด์ž์œจ์„ ์ ์šฉํ•˜์—ฌ ๊ณ„์‚ฐ๋œ ์ด์ž์— ๋Œ€ํ•ด์„œ๋„ ์†์„ฑ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ณ ์ž ํ•œ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด ์›๊ธˆ์ด 1000์›์ด๊ณ  ์˜ˆ์น˜๊ธฐ๊ฐ„์ด 5๊ฐœ์›”์ด๋ฉฐ ์ด์ž์œจ์ด 5.0%๋ผ๋Š” ์†์„ฑ์„ ๊ด€๋ฆฌํ•˜๊ณ  ๊ณ„์‚ฐ๋œ ์ด์ž๋„ ๊ด€๋ฆฌํ•œ๋‹ค.
์ผ๋ฐ˜์˜ˆ๊ธˆ์ด๋‚˜ ํŠน๋ณ„์˜ˆ๊ธˆ ๋“ฑ์— ๋Œ€ํ•ด์„œ๋Š” ์ฝ”๋“œ๋ฅผ ๋ถ€์—ฌ(์˜ˆ: 01-์ผ๋ฐ˜์˜ˆ๊ธˆ, 02-ํŠน๋ณ„์˜ˆ๊ธˆ ๋“ฑ)ํ•˜์—ฌ ๊ด€๋ฆฌํ•œ๋‹ค.
1) ์ผ๋ฐ˜์˜ˆ๊ธˆ์€ ์ฝ”๋“œ ์—”ํ„ฐํ‹ฐ๋ฅผ ๋ณ„๋„๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ  ๊ฐ’์—๋Š” ์ฝ”๋“œ๊ฐ’๋งŒ ํฌํ•จํ•œ๋‹ค.

2) ์›๊ธˆ, ์˜ˆ์น˜๊ธฐ๊ฐ„์€ ๊ธฐ๋ณธ(BASIC) ์†์„ฑ์ด๋‹ค.

3) ์ด์ž์™€ ์ด์ž์œจ์€ ํŒŒ์ƒ(DERIVED) ์†์„ฑ์ด๋‹ค.

4) ์˜ˆ๊ธˆ๋ถ„๋ฅ˜๋Š” ์„ค๊ณ„(DESIGNED) ์†์„ฑ์ด๋‹ค.

์ •๋‹ต→ 3๏ธโƒฃ์ด์ž, ์ด์ž์œจ์€ ํŒŒ์ƒ ์†์„ฑ์ด๋‹ค.(์ด์ž์œจ์€ ์ •์˜๋˜๋Š” ๊ฐ’์œผ๋กœ ํŒŒ์ƒ์†์„ฑ์ด ์•„๋‹ˆ๋‹ค.)

๋ฌธ์ œ ํ•ด์„ค
ํŒŒ์ƒ์†์„ฑ์€ ๋‹ค๋ฅธ ์†์„ฑ์— ์˜ํ–ฅ์„ ๋ฐ›์•„ ๋ฐœ์ƒํ•˜๋Š” ์†์„ฑ์œผ๋กœ, ๋ณดํ†ต ๊ณ„์‚ฐ๋œ ๊ฐ’๋“ค์ด ์ด์— ํ•ด๋‹น๋œ๋‹ค.
์ด์ž๋Š” ์ด์ž์œจ๊ณผ ์›๊ธˆ๋“ฑ์— ์˜ํ•ด ๊ณ„์‚ฐ์ด ๋˜๋Š” ํŒŒ์ƒ์†์„ฑ, ์ด์ž์œจ์€ ๊ณ„์‚ฐ์ด ๋˜๋Š” ์†์„ฑ์ด ์•„๋‹Œ ์ •์˜๋˜๋Š” ๊ฐ’์œผ๋กœ ํŒŒ์ƒ์†์„ฑ์ด ์•„๋‹ˆ๋‹ค.

 

3. ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•œ ์„ค๋ช… ์ค‘ ๊ฐ€์žฅ ์ ์ ˆํ•˜์ง€ ์•Š์€ ๊ฒƒ์€?

 

์ •๋‹ต→ ์ธ์Šคํ„ด์Šค๋Š” ์†์„ฑ์ด ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค.

ํ•ด์„ค : ์ธ์Šคํ„ด์Šค๋Š” ์†์„ฑ์ด ์—†์œผ๋ฉด ์กด์žฌํ• ์ˆ˜ ์—†๋‹ค. (์•„๋ž˜ ํ‘œ ์ฐธ๊ณ )

by yunamom

 

4. ์•„๋ž˜์˜ ERD์— ๋Œ€ํ•œ ์„ค๋ช… ์ค‘ ์ ์ ˆํ•˜์ง€ ์•Š์€ ๊ฒƒ์€? (์ž๊ฒฉ ๊ฒ€์ • ๋ฌธ์ œ 7๋ฒˆ๊ณผ ์œ ์‚ฌ)

๋ฌธ์ œ ์˜ˆ์‹œ

 

์ •๋‹ต→ ์ฃผ๋ฌธ์€ ๊ณ ๊ฐ์ด ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค. 

ํ•ด์„ค : ์ฃผ๋ฌธ์€ ๋ฐ˜๋“œ์‹œ ๊ณ ๊ฐ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. (์•„๋ž˜ ๊ด€๊ณ„์ฐจ์ˆ˜ํ‘œ ์ฐธ๊ณ )

 

5. ๋ถ€๋ชจ์—”ํ„ฐํ‹ฐ๋กœ๋ถ€ํ„ฐ ์†์„ฑ์„ ๋ฐ›์•˜์ง€๋งŒ ์ž์‹์—”ํ„ฐํ‹ฐ์˜ ์ฃผ์‹๋ณ„์ž๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ผ๋ฐ˜์ ์ธ ์†์„ฑ์œผ๋กœ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋กœ ๊ฐ€์žฅ ์ ์ ˆํ•˜์ง€ ์•Š์€ ๊ฒƒ์€?

 

์ •๋‹ต→ Data Life Cycle์ด ๊ฐ™์„ ๋•Œ.

ํ•ด์„ค : 

๋ฌธ์ œ ํ•ด์„ค
์ž์‹ ์—”ํ„ฐํ‹ฐ์—์„œ ๋ฐ›์€ ์†์„ฑ์ด ๋ฐ˜๋“œ์‹œ ํ•„์ˆ˜๊ฐ€ ์•„๋‹ˆ์–ด๋„ ๋ฌด๋ฐฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ถ€๋ชจ ์—†๋Š” ์ž์‹์ด ์ƒ์„ฑ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์ด๋‹ค.
์—”ํ„ฐํ‹ฐ๋ณ„๋กœ ๋ฐ์ดํ„ฐ์˜ ์ƒ๋ช…์ฃผ๊ธฐ(Data Life Cycle)๋ฅผ ๋‹ค๋ฅด๊ฒŒ ๊ด€๋ฆฌํ•  ๊ฒฝ์šฐ์ด๋‹ค.
์—ฌ๋Ÿฌ ๊ฐœ์˜ ์—”ํ„ฐํ‹ฐ๊ฐ€ ํ•˜๋‚˜์˜ ์—”ํ„ฐํ‹ฐ๋กœ ํ†ตํ•ฉ๋˜์–ด ํ‘œํ˜„๋˜์—ˆ๋Š”๋ฐ, ๊ฐ๊ฐ์˜ ์—”ํ„ฐํ‹ฐ๊ฐ€ ๋ณ„๋„์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ€์งˆ ๋•Œ ์ด์— ํ•ด๋‹น๋œ๋‹ค.
์ž์‹์—”ํ„ฐํ‹ฐ์— ์ฃผ์‹๋ณ„์ž๋กœ ์‚ฌ์šฉํ•˜์—ฌ๋„ ๋˜์ง€๋งŒ ์ž์‹์—”ํ„ฐํ‹ฐ์—์„œ ๋ณ„๋„์˜ ์ฃผ์‹๋ณ„์ž๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋” ์œ ๋ฆฌํ•˜๋‹ค๊ณ  ํŒ๋‹จ๋  ๋•Œ

 

6. ๋ชจ๋ธ๋ง์˜ ๋‹จ๊ณ„ ์ค‘ ๊ฐ€์žฅ ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋†’์€ ๋ชจ๋ธ๋ง์€?

 

์ •๋‹ต→ ๋…ผ๋ฆฌ์  ๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง

 

ํ•ด์„ค : ๋…ผ๋ฆฌ์  ์‹œ์Šคํ…œ์œผ๋กœ ๊ตฌ์ถ•ํ•˜๊ณ ์ž ํ•˜๋Š” ์—…๋ฌด์— ๋Œ€ํ•ด Key, ์†์„ฑ, ๊ด€๊ณ„ ๋“ฑ์„ ์ •ํ™•ํ•˜๊ฒŒ ํ‘œํ˜„, ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋†’์Œ

 

7. ์•„๋ž˜์˜ ๋‚ด์šฉ ์ค‘ ํŒŒ์ƒ ์†์„ฑ์œผ๋กœ๋งŒ ์„ ํƒ๋œ ๊ฒƒ์œผ๋กœ ์ ์ ˆํ•œ ๊ฒƒ์€?

์ •๋‹ต→ 4๏ธโƒฃ ์ตœ์ดˆ์ฃผ๋ฌธ์ผ์ž, ์ฃผ๋ฌธ๊ธˆ์•ก, ์ด์ฃผ๋ฌธ๊ธˆ์•ก 

 

ํ•ด์„ค : ํŒŒ์ƒ์†์„ฑ์€ ํƒ€ ์†์„ฑ์— ์˜ํ•ด ์˜ํ–ฅ์„๋ฐ›์•„ ์ž์‹ ์˜ ๊ฐ’์ด ๋ณ€ํ•œ๋‹ค.(์ฃผ๋ฌธ์ˆ˜๋Ÿ‰, ์ด์ฃผ๋ฌธ๊ธˆ์•ก ๋“ฑ)

 

8. ๋‹ค์Œ ์ค‘ ์—”ํ„ฐํ‹ฐ๊ฐ„์˜ ๊ด€๊ณ„์—์„œ 1:1, 1:M๊ณผ ๊ฐ™์ด ๊ด€๊ณ„์˜ ๊ธฐ์ˆ˜์„ฑ์„ ๋‚˜ํƒ€๋‚ด๋Š”๊ฒƒ์œผ๋กœ ๊ฐ€์žฅ ์ ์ ˆํ•œ ๊ฒƒ์€?

 

1) ๊ด€๊ณ„๋ช…(Relationship Membership)

2) ๊ด€๊ณ„์ฐจ์ˆ˜(Relationship Degree/Cardinality)

3) ๊ด€๊ณ„์„ ํƒ์‚ฌ์–‘(Relationship Optionality)

4) ๊ด€๊ณ„์ •์˜(Relationship Definition)

์ •๋‹ต→ ๊ด€๊ณ„์ฐจ์ˆ˜ (Cardinality)

 

๋‹จ๋‹ตํ˜• 1. ์—…๋ฌด์—์„œ ํ•„์š”๋กœ ํ•˜๋Š” ์ธ์Šคํ„ด์Šค๋กœ ๊ด€๋ฆฌํ•˜๊ณ ์ž ํ•˜๋Š” ์˜๋ฏธ์ƒ ๋” ์ด์ƒ ๋ถ„๋ฆฌ ๋˜์ง€ ์•Š๋Š” ์ตœ์†Œ์˜ ๋ฐ์ดํ„ฐ ๋‹จ์œ„๋Š”?(์ž๊ฒฉ ๊ฒ€์ • 14๋ฒˆ)

 

์ •๋‹ต→ ์†์„ฑ

 

๋‹จ๋‹ตํ˜• 2. ๊ธฐ๋ณธํ‚ค๊ฐ€ ์•„๋‹Œ ๋ชจ๋“  ์†์„ฑ์ด ๊ธฐ๋ณธํ‚ค์— ์™„์ „ ํ•จ์ˆ˜ ์ข…์†๋œ ์ƒํƒœ๋ฅผ ๋ฌด์—‡์ด๋ผ ํ•˜๋Š”๊ฐ€?

 

์ •๋‹ต→ ์ œ2์ •๊ทœํ˜•

 

 

2๊ณผ๋ชฉ  ๊ฐ๊ด€์‹  ์˜ˆ์ƒ ๋ณต์›

 

*๋ฌธ์ œ์˜ ์ˆœ์„œ๋Š” ๋‹ค๋ฅผ์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค.(SQL ๋ฌธ 100% ์˜ค๋ผํด ๊ธฐ์ค€)

 

9. ํ…Œ์ด๋ธ”์— ๊ฐ’์„ ์ž…๋ ฅํ•˜๋Š” ๋ช…๋ น์–ด

์ •๋‹ต→Insert

ํ•ด์„ค :

 

10. ๋‹ค์Œ์ค‘ ๊ฒฐ๊ณผ๊ฐ’์ด ๋‹ค๋ฅธ ํ•˜๋‚˜๋ฅผ ๊ณ ๋ฅด์‹œ์˜ค.

1) CEIL(22.14)  

2) FLOOR(22.14)

3) TRUNC(22.14)

4) ROUND(22.14)

์ •๋‹ต→1๋ฒˆ Ceil

ํ•ด์„ค : Ceil (์ •์ˆ˜์˜ฌ๋ฆผ), Floor(์ •์ˆ˜๋‚ด๋ฆผ), Trunc(์†Œ์ˆ˜์ ๋ฒ„๋ฆฌ๊ธฐ), Round(๋ฐ˜์˜ฌ๋ฆผ) ←5์ด์ƒ์„ ์˜ฌ๋ฆผ 

SQL> SELECT CEIL(22.14), FLOOR(22.14), TRUNC(22.14), ROUND(22.14) FROM DUAL;

CEIL(22.14) FLOOR(22.14) TRUNC(22.14) ROUND(22.14)
----------- ------------ ------------ ------------
	 23	      22	   22		22

 

11. ์†ํฅ๋ฏผ์ด ํฌํ•จ๋œ ํŒ€์˜ ํฌ์ง€์…˜์„ 'FW' ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” SQL๋ฌธ์œผ๋กœ ์˜ฌ๋ฐ”๋ฅธ๊ฒƒ์€?

์ •๋‹ต→4๏ธโƒฃ

ํ•ด์„ค :

 

 

12. SNS ๋ณ„ ์ถ”์ฒœ์ ์ˆ˜ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜

์ •๋‹ต→3๏ธโƒฃ

ํ•ด์„ค :

 

13. ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ์„ ์ง€ํ‚ค๊ธฐ ์œ„ํ•œ ๊ฒƒ๊ณผ ๊ด€๊ณ„ ์—†๋Š” ๊ฒƒ์„ ๊ณ ๋ฅด์‹œ์˜ค.

1) ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋กœ์ง

2) Trigger

3) Lock

4) Constraints

์ •๋‹ต→3๏ธโƒฃ

ํ•ด์„ค : Lock/Unlock์€ ๋ณ‘ํ–‰์„ฑ ์ œ์–ด(๋™์‹œ์„ฑ) ๊ธฐ๋ฒ•์ด๋‹ค.

๋ฌด๊ฒฐ์„ฑ : ๋ฐ์ดํ„ฐ ์ž„์˜ ๊ฐฑ์‹ ์œผ๋กœ๋ถ€ํ„ฐ ๋ณดํ˜ธํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ.

์ œ์•ฝ์กฐ๊ฑด์„ ๋„ฃ์–ด์„œ ๋ฌด๊ฒฐ์„ฑ์„ ๋ณด์žฅํ•˜๊ฑฐ๋‚˜, Triger ๋กœ์ง ์•ˆ์— ๊ฒ€์‚ฌ ๊ธฐ๋Šฅ์„ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๊ณ , ๊ฐœ๋ฐœ์ž์˜ ์ฝ”๋”ฉ์—์„œ ๋กœ์ง์„ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๋‹ค.

 

 

14. ๋‹ค์Œ ์ค‘ ์•„๋ž˜์™€ ๊ฐ™์€ ์ง‘ํ•ฉ์ด ์กด์žฌ ํ•  ๋•Œ, ์ง‘ํ•ฉ A์™€ B์— ๋Œ€ํ•˜์—ฌ ์ง‘ํ•ฉ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ C๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ ์ด์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ง‘ํ•ฉ์—ฐ์‚ฐ์€?

A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
C = {3, 4}
1) Union

2) Difference

3) Intersection

4) Product

์ •๋‹ต→3๏ธโƒฃ Intersection(๊ต์ง‘ํ•ฉ)

ํ•ด์„ค :

 

15. CTAS ํŠน์ง•์— ๋งž์ง€ ์•Š๋Š”๊ฒƒ์„ ๊ณ ๋ฅด์‹œ์˜ค. ( ํ…Œ์ด๋ธ” ๋ณต์‚ฌ )

์ •๋‹ต→2๏ธโƒฃ ๋ชจ๋“  ์ œ์•ฝ์กฐ๊ฑด์„ ๋‹ค ๊ฐ€์ ธ์˜ฌ์ˆ˜์žˆ๋‹ค.

ํ•ด์„ค : NOT NULL์กฐ๊ฑด๋งŒ ๊ฐ€์ ธ์˜ฌ์ˆ˜ ์žˆ๋‹ค.

 

16. ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

์ •๋‹ต→1๏ธโƒฃ First value

ํ•ด์„ค :

 

17. 9๋ช… ์ด์ƒ์ด ์žˆ๋Š” ์ง๊ธ‰ ์ค‘์— ๋‚˜์ด๊ฐ€ ์ œ์ผ ๋งŽ์€์‚ฌ๋žŒ ๊ณ ๋ฅด๊ธฐ

์ •๋‹ต→ count 9 >= group by ์ง๊ธ‰

ํ•ด์„ค :

 

18. ๋‹ค์Œ์ค‘ ์ˆ˜ํ–‰์ด ๋˜๋Š” ๋ณด๊ธฐ๋งŒ์„ ๊ณ ๋ฅธ๊ฒƒ์€ ?

INSERT 1 ...
SAVEPOINT A
INSERT 2 ...
DELETE 3 ...
ROLLBACK SAVEPOINT A
INSERT 4 ...
INSERT 5 ...
COMMIT;

์ •๋‹ต→4๏ธโƒฃ (1, 4, 5)

ํ•ด์„ค : ๋กค๋ฐฑ ์ „๊ฐ’์€ ๋‹ค ํšŒ์ˆ˜๋œ๋‹ค.

 

19. ROLLUP, CUBE, GROUPING SETS ํ•จ์ˆ˜๋ฅผ ์นญํ•˜๋Š”๊ฒƒ์€?

์ •๋‹ต→3๏ธโƒฃ Grouping

ํ•ด์„ค : 

 

20. SUBSTR ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ๊ฒƒ์„ ๊ณ ๋ฅด์‹œ์˜ค

1) SELECT SUBSTR('DATABASE',7)FROM DUAL;

2) SELECT SUBSTR('DATABASE',-2)FROM DUAL;

3) SELECT SUBSTR('DATABASE',8,-2)FROM DUAL;

4) SELECT SUBSTR('DATABASE',INSTR('DATABASE','S'),2)FROM DUAL;

์ •๋‹ต→3๏ธโƒฃ

ํ•ด์„ค : 3๋ฒˆ์€ ์Œ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ X

SQL> SELECT SUBSTR('DATABASE',7)FROM DUAL;

SU
--
SE

SQL> SELECT SUBSTR('DATABASE',-2)FROM DUAL;

SU
--
SE

SQL> SELECT SUBSTR('DATABASE',8,-2)FROM DUAL;

S
-

SQL> SELECT SUBSTR('DATABASE',INSTR('DATABASE','S'),2)FROM DUAL;

SU
--
SE

 

21. ์•„๋ž˜ ํ…Œ์ด๋ธ” ๋ฐ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ์„ ๋•Œ SQL์ˆ˜ํ–‰์œผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? (WHERE ์ ˆ์˜ NOT EXISTS ์•ˆ์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ ์กฐ๊ฑด์˜ NULL ์ฒ˜๋ฆฌ ๋ฌธ์ œ)

[table1]
COL1   COL2
-----------
1      10
2      20
3      30

[table2]
COL1   COL2
------------
1       10
2       20

[RESULT]
COL1
----
3

์ •๋‹ต→ WHERE NOT EXISTS (SELECT 1 FROM T2 B WHERE B.COL1 = A.COL1)

ํ•ด์„ค :

SELECT COL1 FROM T1 A WHERE NOT EXISTS(SELECT 1 FROM T2 B WHERE B.COL1 = A.COL1);

COL1
----
3

 

22. ํ…Œ์ด๋ธ”์˜ ๊ฐœ์ˆ˜๊ฐ€ 5๊ฐœ์ผ ๋•Œ ์ตœ์†Œ ์กฐ์ธ ๊ฐœ์ˆ˜๋Š” ๋ช‡๊ฐœ์ธ๊ฐ€?

์ •๋‹ต→ 4๊ฐœ

ํ•ด์„ค : ์ตœ์†Œ ์กฐ์ธ ๊ฐœ์ˆ˜ (N-1)

 

23. ์•„๋ž˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” SQL์„ ์™„์„ฑํ•˜๋Š” GROUP BY(    )๋กœ ์ ์ ˆํ•˜์ง€ ์•Š์€ ๊ฒƒ์€?

A ์ง‘๊ณ„, A+B ์ง‘๊ณ„ ์˜ ๊ฒฐ๊ณผ

์ •๋‹ต→ ROLLUP(A,B) = GROUP BY(A,B) UNION ALL GROUP BY (A) UNION ALL GROUP BY ()

ํ•ด์„ค : ๋‚˜๋จธ์ง€๋Š” ์ „์ฒด ์ง‘๊ณ„๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์Œ

 

 

24. ์•„๋ž˜์˜ SQL๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์„ ๊ณ ๋ฅด์‹œ์˜ค.(FULL OUTER JOIN์˜ ๋ณ€ํ™˜ ํ˜•ํƒœ์— ๋Œ€ํ•œ ๋ฌธ์ œ)

1) SELECT * FROM A FULL OUTER JOIN B
2) FULL JOIN
3) LEFT OUTER JOIN UNION RIGHT OUTER JOIN
4) LEFT OUTER JOIN UNION ALL RIGHT OUTER JOIN

์ •๋‹ต→4๏ธโƒฃ

ํ•ด์„ค : FULL OUTER JOIN ์€ LEFT UNION RIGHT , UNION ALL ์€ ์ค‘๋ณต์ œ๊ฑฐ ์—†์ด ์ „์ฒด์ถœ๋ ฅ๋˜๋ฏ€๋กœ ๋‹ค๋ฅด๋‹ค.

 

25. ์ฃผ๋ฌธ ํ…Œ์ด๋ธ”์˜ ํŠน์ • ๊ธฐ๊ฐ„ ๊ณ ๊ฐ์˜ ์ฃผ๋ฌธ๊ธˆ์•ก์„ ํ˜„์žฌ ๊ณ ๊ฐํ…Œ์ด๋ธ”์— ์žˆ๋Š” ๊ณ ๊ฐ ๋ฐ์ดํ„ฐ๋งŒ์œผ๋กœ ์ถ”๋ ค๋‚ด์–ด ์ „์ฒด๊ธฐ๊ฐ„์˜ ์ฃผ๋ฌธ๊ธˆ์•ก ํ•ฉ์ธ์ง€, ๋™์ผ ๊ธฐ๊ฐ„์˜ ์ฃผ๋ฌธ ๊ธˆ์•ก ํ•ฉ์ธ์ง€๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ (์–ด๋ ค์› ๋˜ ๋ฌธ์ œ...SQL๋ฌธ์ด ์—„์ฒญ๊ธธ์—ˆ์Œ)

์ •๋‹ต→4๏ธโƒฃ

ํ•ด์„ค : ์ •๋‹ต์€ 4๋ฒˆ์ด๋ผ๊ณ  ํ•จ

 

26. ์•„๋ž˜์˜ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ INSERT ๊ตฌ๋ฌธ ์ˆ˜ํ–‰์‹œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ๊ณ ๋ฅด์‹œ์˜ค.

CREATE TABLE T1(
C1 PRIMARY KEY,
C2 NOT NULL,
C3 UNIQUE,
C4 CHECK (NOT NULL));

1)INSERT INTO T1 VALUES(NULL, 1, 2, 3);
2)INSERT INTO T1 VALUES(1, NULL, 2, 3);
3)INSERT INTO T1 VALUES(1, 2, NULL, 3);
4)INSERT INTO T1 VALUES(1, 2, 3, NULL);

์ •๋‹ต→3๏ธโƒฃ

ํ•ด์„ค : UNIQUE ์—๋Š” NULL์ด ํ—ˆ์šฉ๋œ๋‹ค.

 

27. ์•„๋ž˜์˜ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ SQL ๊ฒฐ๊ณผ๋Š”? (AVG ํ•จ์ˆ˜์—์„œ NULL ๊ฐœ์ˆ˜ ํฌํ•จ ์—ฌ๋ถ€ ๋ฌธ์ œ)

[table]
C1
----
10
20
NULL

SELECT AVG(NVL(C1,0)) FROM table;

์ •๋‹ต→ 10

ํ•ด์„ค : NULL์ด 0์œผ๋กœ ๋ณ€๊ฒฝ๋˜์–ด AVG ์˜ ๋ชจ์ˆ˜๋กœ ํฌํ•จ๋œ๋‹ค.

 

28. TABLE1, TABLE2, TABLE3 ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ ์•„๋ž˜์˜ INSERT ๊ฒฐ๊ณผ ๊ฐœ์ˆ˜๋กœ ์•Œ๋งž์€ ๊ฒƒ์€?

(๋‹ค์ค‘ TABLE INSERT FIRST)

[TABLE0]
N1
---
1
2
5

INSERT FIRST
       WHEN N1 >= 2 THEN INTO TABLE1(N1) VALUES(N1)
       WHEN N1 >= 3 THEN INTO TABLE2(N1) VALUES(N1)
       ELSE INTO TABLE3 VALUES(N1)
SELECT N1 FROM TABLE0;

์ •๋‹ต→(2,0,1) 

ํ•ด์„ค : ๊ฐ€์žฅ ์œ„์˜ WHEN ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด ๋ฐ‘์˜ ์กฐ๊ฑด์„ ์ฒดํฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ 2,0,1

SQL> INSERT FIRST WHEN N1 >= 2 THEN INTO TABLE1(N1) VALUES(N1) WHEN N1 >= 3 THEN INTO TABLE2(N1) VALUES(N1) ELSE INTO TABLE3 VALUES(N1) SELECT N1 FROM TABLE0;

3 rows created.

SQL> SELECT COUNT(*) FROM TABLE1;

  COUNT(*)
----------
	 2

SQL> SELECT COUNT(*) FROM TABLE2;

  COUNT(*)
----------
	 0

SQL> SELECT COUNt(*) FROM TABLE3;

  COUNT(*)
----------
	 1

 

29. ์•„๋ž˜์™€ ๊ฐ™์€ SQL ๊ฒฐ๊ณผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€?

(OUTER JOIN๊ณผ INNER JOIN์ด ์‚ฌ์šฉ๋  ๊ฒฝ์šฐ ์—ฐ๊ฒฐ๋œ ์กฐ์ธ์—์„œ INNER JOIN ์ด ๋งˆ์ง€๋ง‰์— ์‚ฌ์šฉ ๋  ๋•Œ)

SELECT * FROM
       T1, T2, T3, T4
       WHERE T2.COL1(+) = T1.COL1
       AND T3.COL1(+) = T2.COL1
       AND T4.COL1 = T3.COL1;

์ •๋‹ต→ 1 

ํ•ด์„ค : LEFT - LEFT - INNER JOIN์œผ๋กœ ์—ฐ๊ฒฐ๋˜๋Š” ๊ฒฝ์šฐ ๊ฒฐ๊ตญ INNER JOIN์˜ ๊ฒฐ๊ณผ๋งŒ์ด ๋‚จ๋Š”๋‹ค. ๊ณตํ†ต์œผ๋กœ ๊ฐ€์ง„ ๊ฐ’์ด 1์ด๋ฏ€๋กœ 1๊ฑด๋งŒ ๋‚จ์Œ

 

30. ์•„๋ž˜์˜ SQL์—์„œ 1๊ฑด๋งŒ ์ถœ๋ ฅ๋˜๋Š” SQL์ด ์•„๋‹Œ๊ฒƒ์„ ๊ณ ๋ฅด์‹œ์˜ค.

1) ROWNUM = 1

2) ROWNUM < 2

3) ROWNUM <= 2

4) ROWNUM <= 2-1

์ •๋‹ต→ ROWNUM <= 2

ํ•ด์„ค : ROWNUM <= 2 (2๊ฑด ์ถœ๋ ฅ)

 

31. ์•„๋ž˜ SQL ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? (WHERE ์ ˆ NOT IN ์•ˆ์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ ๊ฐ’์ด NULL์„ ํฌํ•จํ•  ๋•Œ์˜ ๊ฒฐ๊ณผ)

SELECT ... FROM table1 A 
      WHERE A.N1 
      NOT IN(SELECT B.N1 FROM table3 B);
      
-- select ๋’ค ๋ถˆํ™•์‹ค

์ •๋‹ต→๊ฒฐ๊ณผ์—†์Œ (0 ROWS) or 0

ํ•ด์„ค : NOT IN (SUBQUERY) ์—์„œ NULL์ด ํฌํ•จ๋œ ๊ฒฝ์šฐ NULL์˜ ์ฐธ ๊ฑฐ์ง“ ํŒ์ •์ด ๋˜์ง€ ์•Š์•„ 0๊ฑด ์ฒ˜๋ฆฌ

table3 ์— null ๊ฐ’์„ ๋„ฃ๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์‹คํ–‰ํ•ด๋ณด์•˜๋Š”๋ฐ ๋ฌธ์ œ์— count(*) ์˜€๋Š”์ง€ ์ •ํ™•ํžˆ ๊ธฐ์–ต์•ˆ๋‚จ ใ… ใ… 

SQL> select count(*) from table1 A where A.N1 NOT IN(select B.N1 from table3 b);

  COUNT(*)
----------
	 0

SQL> select count(N1) from table1 A where A.N1 NOT IN(select B.N1 from table3 b);

 COUNT(N1)
----------
	 0

SQL> select N1 from table1 A where A.N1 NOT IN(select B.N1 from table3 b);

no rows selected

 

32. ์•„๋ž˜์˜ ๋ณด๊ธฐ์ค‘ ์ˆ˜์ • ์ „ํ›„๊ฐ€ ๋‹ค๋ฅธ ํ•˜๋‚˜๋ฅผ ๊ณ ๋ฅด์‹œ์˜ค. (SUM๋‚ด๋ถ€์˜ NVL๊ณผ ์™ธ๋ถ€์˜ NVL์˜ ์ฐจ์ด ๋ฌธ์ œ)

์ •๋‹ต→SUM (NVL(COL1) + NVL(COL2)) > NVL(SUM(COL1 + COL2))

ํ•ด์„ค : COL1 + COL2 ์—์„œ ๋‘˜์ค‘ ํ•˜๋‚˜์˜ ๊ฐ’์ด NULL ์ด๋ฉด NULL์ด ๋˜์–ด ๊ฐ’์ด ๋‹ฌ๋ผ์ง„๋‹ค.

 

33. ์•„๋ž˜์˜ SQL๊ฒฐ๊ณผ ์ค‘ ๋‹ค๋ฅธ ๊ฒƒ์„ ๊ณ ๋ฅด์‹œ์˜ค.

(OR ์กฐ๊ฑด๊ณผ UNION ALL ๋ณ€ํ™˜์—์„œ OR ์กฐ๊ฑด์—์„œ ์‚ฌ์šฉ๋œ ์ปฌ๋Ÿผ์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ์˜ ๊ฒฐ๊ณผ ๋ฌธ์ œ)

[TABLE1]
COL1   COL2
-----------
A      a
B      b
C      c


1) SELECT COL1 FROM TABLE1 WHERE COL1 IN ('A','B') OR COL2 <> 'c';
2) SELECT COL1 FROM TABLE1 WHERE COL1 IN ('A','B')
   UNION ALL
   SELECT COL1 FROM TABLE1 WHERE COL2 <> 'c';
3) SELECT COL1 FROM TABLE1 WHERE COL1 IN ('A','B')
   UNION
   SELECT COL1 FROM TABLE1 WHERE COL2 <> 'c';
4) SELECT COL1 FROM TABLE1 WHERE COL1 = 'A' OR COL1 = 'B' OR COL2 <> 'c';

์ •๋‹ต→2๏ธโƒฃ 

ํ•ด์„ค : UNION ALL ๋กœ ์ธํ•ด ABAB๋กœ ๊ฐ’์ด ์ถœ๋ ฅ๋จ

 

 

34. ์•„๋ž˜์˜ SQL์ค‘ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์€? ( OR, IN, AND ์˜ ๋ณ€ํ˜•์— ๋Œ€ํ•œ ๋ฌธ์ œ)

1)SELECT*FROM SQLD49 WHERE V1 = 'A' AND V2 IN ('T1','T2','T3');

2)SELECT*FROM SQLD49 WHERE V1 = 'A' AND V2='T1' OR V2='T2' OR V2='T3';

3)SELECT*FROM SQLD49 WHERE (V1,V2) IN (('A','T1'),('A','T2'),('A','T3'));

4)SELECT*FROM SQLD49 WHERE V1 = 'A' AND (V2 = 'T1' OR V2 = 'T2' OR V2 = 'T3');

์ •๋‹ต→2๏ธโƒฃ

ํ•ด์„ค : OR ์„ ๊ธฐ์ค€์œผ๋กœ ๋ชจ๋‘ ์ถœ๋ ฅ๋œ๋‹ค. ( 4๋ฒˆ๊ฐ™์€๊ฒฝ์šฐ๋Š” ๊ด„ํ˜ธ๋กœ ๋ฌถ์–ด์ ธ์žˆ์–ด์„œ ๋‹ค๋ฆ„)

SELECT*FROM SQLD49 WHERE V1 = 'A' AND V2 IN ('T1','T2','T3');

N1  V1   V2
-----------
1   A    T1


SQL> SELECT*FROM SQLD49 WHERE V1 = 'A' AND V2='T1' OR V2='T2' OR V2='T3';

N1  V1   V2
-----------
1   A    T1
2   B    T2
3        T3


SQL> SELECT*FROM SQLD49 WHERE (V1,V2) IN (('A','T1'),('A','T2'),('A','T3'));

N1  V1   V2
-----------
1   A    T1

SQL> SELECT*FROM SQLD49 WHERE V1 = 'A' AND (V2 = 'T1' OR V2 = 'T2' OR V2 = 'T3');

N1  V1   V2
-----------
1   A    T1

 

35. ํŠน์ • ๊ณผ๋ชฉ์˜ ํ•™์ ์ด 4.0 ์ด์ƒ์ธ ํ•™์ƒ์˜ ์ด๋ฆ„์„ ๊ตฌํ•˜๋Š” SQL๋กœ ์˜ฌ๋ฐ”๋ฅธ๊ฒƒ์„๊ณ ๋ฅด์‹œ์˜ค

(์ง‘๊ณ„ ์ฟผ๋ฆฌ์—์„œ SELECT ์ ˆ์— ์‚ฌ์šฉ๋˜๋Š” ์ปฌ๋Ÿผ์€ GROUP BY ์— ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•จ์„ ๋ฌป๋Š” ๋ฌธ์ œ)

์ˆ˜๊ฐ• ์ •๋ณด
-------
ํ•™๋ฒˆ
๊ณผ๋ชฉ
ํ•™์ 

ํ•™์ƒ
----
์ด๋ฆ„
ํ•™๋ฒˆ

๊ณผ๋ชฉ
----
๊ณผ๋ชฉ๋ช…
...

์ •๋‹ต→ SELECT ์ด๋ฆ„ FROM .... GROUP BY ํ•™๋ฒˆ, ์ด๋ฆ„ HAVING MAX(ํ•™์ ) > 4 

ํ•ด์„ค : SELECT ์— ์ด๋ฆ„์ด ๋‚˜์˜ค๋ฏ€๋กœ GROUP BY ์— ์ด๋ฆ„์ด ๋‚˜์™€์•ผ ํ•จ

(๊ทธ๋ฃน์˜ ๊ธฐ์ค€์„ ์ด๋ฆ„์œผ๋กœ ํ•  ๊ฒฝ์šฐ ์œ ์ผ๊ฐ’์ด ์•„๋‹ˆ๋ฏ€๋กœ ํ•™๋ฒˆ-์ด๋ฆ„์œผ๋กœ ๋ฌถ์–ด์•ผ๋งŒ ์œ ์ผ๊ฐ’์˜ ํ•™์ ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.)

 

36. ์•„๋ž˜์˜ SQL์˜ ๊ฒฐ๊ณผ๋กœ ์•Œ๋งž์€ ๊ฒƒ์€? (WHERE 1=2 ๋ฌธ์ œ)

SELECT .... FROM SQLD WHERE 1 = 2;

-- SELECT ๋’ค์˜ ๊ฐ’์ด ์ •ํ™•ํ•˜์ง€ ์•Š์Œ

์ •๋‹ต→๊ฒฐ๊ณผ๊ฐ’์ด ์—†์Œ (์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ์ด๊ฒŒ ๋‹ต์ด๋ผ๊ณ  ํ•จ)

ํ•ด์„ค : ๋ฌธ์ œ ๋ณด๊ธฐ๊ฐ€ ์ •ํ™•ํžˆ ๊ธฐ์–ต๋‚˜์ง€ ์•Š์Œ 

SQL> SELECT N1 FROM SQLD WHERE 1=2;

no rows selected

SQL> SELECT COUNT(N1) FROM SQLD WHERE 1=2;

 COUNT(N1)
----------
	 0

 

37. WHERE ์ ˆ์— ์‚ฌ์šฉ๋œ ์„œ๋ธŒ์ฟผ๋ฆฌ์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ์ ์ ˆํ•˜์ง€ ์•Š์€ ๊ฒƒ์€?

( ๋ฉ”์ธ์ฟผ๋ฆฌ์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ์ค‘ WHERE ์ ˆ์— ์‚ฌ์šฉ๋œ ์„œ๋ธŒ์ฟผ๋ฆฌ์—๋Œ€ํ•œ ๋‚ด์šฉ์„ ๋ฌป๋Š” ๋ฌธ์ œ)

์ •๋‹ต→ ๋ฉ”์ธ์ฟผ๋ฆฌ ... ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ์žˆ์„๋•Œ ๊ฒฐ๊ณผ๋Š” ํ•ญ์ƒ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค.

ํ•ด์„ค : FROM ์ ˆ์— ์‚ฌ์šฉ๋œ ์„œ๋ธŒ์ฟผ๋ฆฌ(INLINE VIEW) ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ๋ฉ”์ธ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ๋ชปํ•œ๋‹ค.

 

38. ์•„๋ž˜์˜ SQL ๊ฒฐ๊ณผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€?

(LEFT OUTER JOIN์—์„œ ON์ ˆ์— ์‚ฌ์šฉ๋œ ์กฐ๊ฑด์ ˆ๊ณผ WHERE ์ ˆ์— ์‚ฌ์šฉ๋œ ์กฐ๊ฑด์ ˆ์˜ FILTERING์— ๊ด€ํ•œ ๋ฌธ์ œ)

์ •๋‹ต→ 1๊ฑด

ํ•ด์„ค : ON ์ ˆ์˜ ์กฐ๊ฑด์ ˆ์„ ์šฐ์„ ์ ์œผ๋กœ FILTERING ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์กฐ์ธํ•จ

 

39.

์ •๋‹ต→

ํ•ด์„ค :

 

40. 

์ •๋‹ต→

ํ•ด์„ค :

 

 

 

2๊ณผ๋ชฉ  ์ฃผ๊ด€์‹  ์˜ˆ์ƒ ๋ณต์›

*๋ฌธ์ œ์˜ ์ˆœ์„œ๋Š” ๋‹ค๋ฅผ์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค.

 

๋‹จ๋‹ตํ˜• 3. ์•„๋ž˜ SQL1 ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„๋ก SQL2 ๋นˆ์นธ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

[SQL1]
SELECT*FROM A, B;

[SQL2]
SELECT*FROM A (     ) B;

์ •๋‹ต→CROSS JOIN

ํ•ด์„ค :

 

๋‹จ๋‹ตํ˜• 4. ์•„๋ž˜์˜ ๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์—์„œ C3์˜ 2๋ฒˆ์งธ ๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

[SQLD44]

C1  C2   C3
------------
 1 NULL  A
 2  1    B
 3  1    C
 4  2    D

[SQL]

SELECT C1, C2, C3
FROM SQLD44
     CONNECT BY PRIOR C1 = C2
           START WITH C1 = 1
     ORDER SIBLINGS BY C1 DESC;

์ •๋‹ต→C

ํ•ด์„ค : C1 ์ด ๊ฐ™์€ ๋ ˆ๋ฒจ์—์„œ DESC ๋กœ ์ •๋ ฌ๋จ 

[RESULT]
 C1  C2   C3
-------------
 1        A
 3   1    C
 2   1    B
 4   2    D

 

๋‹จ๋‹ตํ˜• 5. ์•„๋ž˜์˜ ๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค.

[SQLD45]

 C1  C2  C3
-------------- 
 1       KING
 2   1   JOHN
 3   2   SCOTT

[SQL]
SELECT C3 FROM SQLD45
   WHERE C1 <> 2
   CONNECT BY C1 = PRIOR C2
   START WITH C1 = 2;

์ •๋‹ต→KING

ํ•ด์„ค : C1 = PRIOR C2  ์—ญ๋ฐฉํ–ฅ

SQL> SELECT C3 FROM SQLD45 WHERE C1 <> 2 CONNECT BY C1 = PRIOR C2 START WITH C1 = 2;

C3
-----
KING

 

๋‹จ๋‹ตํ˜• 6. GRANT, REVOKE ๋“ฑ์˜ SQL ์„ ๋ฌด์—‡์ด๋ผ ํ•˜๋Š”๊ฐ€?

์ •๋‹ต→DCL

ํ•ด์„ค :

 

๋‹จ๋‹ตํ˜• 7. ์•„๋ž˜์˜ SQL๊ฒฐ๊ณผ์˜ ๋นˆ์นธ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. (NTILE ๋ฌธ์ œ)

[SQL]

[RESULT]

COL1    COL2
------------
A      (   )
B        3
C      (   )

์ •๋‹ต→3, 2

ํ•ด์„ค :

์ด 8๊ฐœ์˜ ๋กœ์šฐ์—์„œ NTILE(3) ์œผ๋กœ ๊ฐœ์ˆ˜๋Š” 3, 3, 2

 

๋‹จ๋‹ตํ˜• 8. ์•„๋ž˜ SQL์˜ ๋นˆ์นธ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

[SQLD48]

V1	    N1
----------------
A	    100
B	    150
C	   1400
D	    450
E	     50

[SQL]

SELECT V1, N1,
  COUNT(N1) OVER 
            (ORDER BY N1 RANGE 
            BETWEEN 0 PRECEDING 
            AND
            50 FOLLOWING) AS CNT 
FROM SQLD48;

์ •๋‹ต→RANGE

ํ•ด์„ค : ๊ฐ’์˜ +0 ~ +50 ์‚ฌ์ด์˜ COUNT ๊ฒฐ๊ณผ๋กœ ๋‚˜ํƒ€๋‚จ

SQL> SELECT V1, N1, COUNT(N1) OVER (ORDER BY N1 RANGE BETWEEN 0 PRECEDING AND 50 FOLLOWING) AS CNT FROM SQLD48;

V1    N1    CNT
-----------------
E     50     2
A     100    2
B     150    1
D     450    1
C    1400    1

ROW : ๋ฐ์ดํ„ฐ์˜ ๊ฐ’์„ ๋”ํ•จ

RANGE : ๋ฐ์ดํ„ฐ์˜ ๋ฒ”์œ„๋ฅผ ๋‚˜ํƒ€๋ƒ„

 

๋‹จ๋‹ตํ˜• 9. ์•„๋ž˜์˜ SQL ๊ฒฐ๊ณผ๋ฅผ ์ ์œผ์‹œ์˜ค. (AND, OR, AND ์กฐ๊ฑด์ด ์žˆ๋Š” SQL) ๊ฐฏ์ˆ˜์„ธ๊ธฐ

[SQLD49]

N1  V1  V2
--------------
 1 A	T1
 2 B	T2
 3 NULL	T3

์ •๋‹ต→3

ํ•ด์„ค :

 

๋‹จ๋‹ตํ˜• 10. ์•„๋ž˜์˜ SQL๊ฒฐ๊ณผ๋ฅผ ์ ์œผ์‹œ์˜ค.(LIKE '_L%' ๋ฌธ์ œ)

[SQLD50]

N1   V1
--------
 1  SMITH
 2  JOHN
 3  ALX
 4  CLARE
 5  BLX
 
 [SQL]
 SELECT COUNT(*) 
 FROM SQLD50 
 WHERE V1 LIKE '_L%';

์ •๋‹ต→3

ํ•ด์„ค : _L% (๋‘๋ฒˆ์งธ ์ž๋ฆฌ์˜ ๋ฌธ์ž๊ฐ€ L์ธ ๋ชจ๋“  ํ–‰ ์ถœ๋ ฅ)

SQL> SELECT COUNT(*) FROM SQLD50 WHERE V1 LIKE '_L%';

  COUNT(*)
----------
	 3

 

 

 

์ถœ์ฒ˜ : ๋ฐ์ดํ„ฐ ์ „๋ฌธ๊ฐ€ ํฌ๋Ÿผ ํšŒ์›๋‹˜๋“ค & ์›”์•ผ๋ฃจ๋‹˜

 

300x250

์ฝ”๋“œ