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

[SQLD] 46ํšŒ SQLD ( 1๊ณผ๋ชฉ/2๊ณผ๋ชฉ ์ •๋‹ต )

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

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

์ด๋ฒˆ์‹œ๊ฐ„์—๋Š” SQLD 46ํšŒ ์‹œํ—˜ 1๊ณผ๋ชฉ, 2๊ณผ๋ชฉ ์ •๋‹ต์„ ํฌ์ŠคํŒ… ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป

 

*๋ฌธ์ œ ์ˆœ์„œ๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์œผ๋ฉด ๋‹ต์˜ ๋ณด๊ธฐ๊ฐ€ ๊ธฐ์–ต์ด ์•ˆ๋‚˜๋Š” ๊ฒฝ์šฐ๋Š” ๋‹ต ์ž์ฒด๋ฅผ ์ ์–ด๋‘๊ฑฐ๋‚˜ ๋ฌธ์ œ๋งŒ ์ ์€ ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

(46ํšŒ SQLD์‹œํ—˜์€ ์ง€๊ธˆ๊นŒ์ง€ ํšŒ์ฐจ๋ž‘ ๋‹ค๋ฅด๊ฒŒ ๋‹จ๋‹ตํ˜•์ด 5๋ฌธ์ œ๋กœ ๊ธฐ์กด 10๋ฌธ์ œ์—์„œ ์ค„์–ด๋“ค์—ˆ์Œ)


SQLD 46ํšŒ

<1๊ณผ๋ชฉ : 8๋ฌธ์ œ (๊ฐ๊ด€์‹) + 2๋ฌธ์ œ (๋‹จ๋‹ตํ˜•) >

โ€‹

<1๊ณผ๋ชฉ : 8๋ฌธ์ œ (๊ฐ๊ด€์‹) + 2๋ฌธ์ œ (๋‹จ๋‹ตํ˜•) >

โ€‹

1. ๋…ผ๋ฆฌ ๋ชจ๋ธ๋ง์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ์ ์ ˆํ•˜์ง€ ์•Š์€ ๊ฒƒ์€? (์ด์˜ ์ œ๊ธฐ ํ•„์š”)

3๋ฒˆ> ์ฃผ์ œ์˜์—ญ์„ ์„ค๊ณ„ ํ•œ๋‹ค.

4๋ฒˆ> ์Šˆํผ ์„œ๋ธŒ ํƒ€์ž…์˜ ๋ถ„๋ฆฌ ๋ฐ ํ†ตํ•ฉ

>> 3๋ฒˆ์ชฝ ์˜๊ฒฌ : https://cafe.naver.com/sqlpd/39683 

>> 4๋ฒˆ์ชฝ ์˜๊ฒฌ : https://cafe.naver.com/sqlpd/39785 

 

โ€‹

2. ๊ณ ๊ฐ ->-----<- ์„œ๋น„์Šค ๋กœ ์ด๋ฃจ์–ด์ง„ ERD ์—์„œ ์ •๊ทœํ™”๊ฐ€ ์ ์ ˆํ•˜๊ฒŒ ๋œ ๊ฒƒ์€? (๊ณ ๊ฐ์€ ์„œ๋น„์Šค๋ฅผ ํ•˜๋‚˜๋งŒ ๊ฐ€์ง„๋‹ค.)

4๋ฒˆ: ๊ณ ๊ฐ ---<-์„œ๋น„์Šค๊ณ„์•ฝ ->----- ์„œ๋น„์Šค

>> ์„œ๋น„์Šค ๊ณ„์•ฝ์—๋Š” ๊ณ ๊ฐ๋ฒˆํ˜ธ์™€ ์„œ๋น„์Šค๋ฒˆํ˜ธ๋งŒ ์žˆ์–ด์•ผ ํ•จ.

>> ์„œ๋น„์Šค๊ณ„์•ฝ๋ฒˆํ˜ธ๊ฐ€ ์žˆ์œผ๋ฉด ๊ณ ๊ฐ์ด ๋™์ผ ์„œ๋น„์Šค๋ฅผ ๊ฐ€์งˆ์ˆ˜ ์žˆ์Œ

โ€‹

โ€‹

3. ์•„๋ž˜์˜ ERD ( A, B, C ,D ํ…Œ์ด๋ธ”์ด ์žˆ๊ณ  ๋ชจ๋“  ๊ด€๊ณ„๋Š” ์‹๋ณ„์ž ๊ด€๊ณ„๋กœ ์ด๋ฃจ์–ด์ง) ์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ์ ์ ˆํ•˜์ง€ ์•Š์€ ๊ฒƒ์€?

-> A ์™€ B ์˜ ๊ด€๊ณ„๊ฐ€ ๋น„์‹๋ณ„์ž ๊ด€๊ณ„๊ฐ€ ๋˜๋ฉด A,C ์˜ ์กฐ์ธ์ด ์‰ฌ์›Œ ์ง„๋‹ค.

>> A,B,C ๊ฐ€ ์‹๋ณ„์ž ๊ด€๊ณ„์ด๋ฉด A์™€ C ์˜ ์กฐ์ธ์€ B ๊ฐ€ ์—†์ด ๋ฐ”๋กœ ์กฐ์ธ์ด ๊ฐ€๋Šฅํ•˜๋‚˜ A ์™€ B ์˜ ๊ด€๊ณ„๊ฐ€ ๋น„์‹๋ณ„์ž๊ฐ€ ๋˜๋ฉด A์˜ ํ‚ค๊ฐ€ C ๋กœ ์ „๋‹ฌ์ด ๋˜์ง€ ์•Š์•„ A์™€ C ๋ฅผ ์กฐ์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ˜๋“œ์‹œ B ๋ฅผ ๊ฑฐ์ณ์•ผ ํ•˜๋Š” ๋ถˆํŽธํ•จ์ด ์ƒ๊น€.

โ€‹

โ€‹

4. ์†์„ฑ์— ๋Œ€ํ•œ ์„ค๋ช…์— ๋Œ€ํ•ด ์•Œ๋งž๊ฒŒ ์†์„ฑ์ด ์ง์ง€์–ด์ง„ ๊ฒƒ์€?

<๋ณด๊ธฐ>

์†์„ฑ์€ ์—…๋ฌด๋ถ„์„์„ ํ†ตํ•ด ๋ฐ”๋กœ ์ •์˜ํ•œ ์†์„ฑ์„ ( ) ,

์›๋ž˜ ์—…๋ฌด์ƒ ์กด์žฌํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ์„ค๊ณ„๋ฅผ ํ•˜๋ฉด์„œ ๋„์ถœํ•ด๋‚ด๋Š” ์†์„ฑ์„ ( ),

๋‹ค๋ฅธ ์†์„ฑ์œผ๋กœ๋ถ€ํ„ฐ ๊ณ„์‚ฐ์ด๋‚˜ ๋ณ€ํ˜•์ด ๋˜์–ด ์ƒ์„ฑ๋˜๋Š” ์†์„ฑ์„ ( ) ์ด๋ผ๊ณ  ํ•œ๋‹ค

-> ๊ธฐ๋ณธ์†์„ฑ, ์„ค๊ณ„ ์†์„ฑ , ํŒŒ์ƒ ์†์„ฑ

โ€‹

โ€‹

5. ๋‹ค๋ฅธ ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ๋งŒ๋“ค์–ด์ง€๋Š” ์†์„ฑ์€?

โ€‹ -> ํŒŒ์ƒ ์†์„ฑ

โ€‹

โ€‹

6. ์•„๋ž˜์˜ ์ •๊ทœํ˜•์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ๋ฐ”๋ฅธ ๊ฒƒ์€?

<๋ณด๊ธฐ> ๋ชจ๋“  ์†์„ฑ์ด ์ฃผ์‹๋ณ„์ž์— ์™„์ „ ์ข…์†๋œ๋‹ค

-> ์ œ2์ •๊ทœํ˜•

* ๋ฆด๋ ˆ์ด์…˜์— ์†ํ•œ ๋ชจ๋“  ์†์„ฑ์˜ ๋„๋ฉ”์ธ์ด ์›์ž ๊ฐ’(atomic value)์œผ๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉด ์ œ1์ •๊ทœํ˜•์— ์†ํ•œ๋‹ค.

* ๋ฆด๋ ˆ์ด์…˜์ด ์ œ1์ •๊ทœํ˜•์— ์†ํ•˜๊ณ , ๊ธฐ๋ณธํ‚ค๊ฐ€ ์•„๋‹Œ ๋ชจ๋“  ์†์„ฑ์ด ๊ธฐ๋ณธํ‚ค์— ์™„์ „ ํ•จ์ˆ˜ ์ข…์†๋˜๋ฉด ์ œ2์ •๊ทœํ˜•์— ์†ํ•œ๋‹ค.

โ€‹

โ€‹

7. ์•„๋ž˜์˜ ERD ์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ์ ์ ˆํ•œ ๊ฒƒ์€?

โ€‹ <๋ณด๊ธฐ> ์ปดํ“จํ„ฐ -o|--------|- ๋งˆ๋”๋ณด๋“œ

-> ๋งˆ๋”๋ณด๋“œ๋Š” ๋•Œ๋•Œ๋กœ ์ปดํ“จํ„ฐ์— ํฌํ•จ๋œ๋‹ค.

โ€‹

โ€‹

8. ์ฃผ์‹๋ณ„์ž์— ๋Œ€ํ•œ ์•„๋ž˜์˜ ์„ค๋ช…์— ๋Œ€ํ•ด ๋ฐ”๋ฅด๊ฒŒ ์ง์ง€์–ด์ง„ ๊ฒƒ์€?

โ€‹ <๋ณด๊ธฐ> --> ๋‹ต์— ๋งž์ถฐ์„œ ์„ค๋ช…์„ ์ฑ…์—์„œ ์ฐพ์•„ ์ ์€ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. (์ฐธ๊ณ )

( ) - ์—”ํ„ฐํ‹ฐ ๋‚ด์—์„œ ๊ฐ ์–ด์ปค๋Ÿฐ์Šค๋ฅผ ๊ตฌ๋ถ„ํ• ์ˆ˜ ์žˆ๋Š” ๊ตฌ๋ถ„์ž์ด๋ฉฐ, ์—”ํ„ฐํ‹ฐ์™€ ์ฐธ์กฐ๊ด€๊ณ„๋ฅผ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ์‹๋ณ„์ž

( ) - ์—”ํ„ฐํ‹ฐ ๋‚ด์—์„œ ๊ฐ ์–ด์ปค๋Ÿฐ์Šค๋ฅผ ๊ตฌ๋ถ„ํ• ์ˆ˜ ์žˆ๋Š” ๊ตฌ๋ถ„์ž์ด๋‚˜ ๋Œ€ํ‘œ์„ฑ์„ ๊ฐ€์ง€์ง€ ๋ชปํ•จ

( ) - ์—…๋ฌด์— ์˜ํ•ด ๋งŒ๋“ค์–ด์ง€๋Š” ์‹๋ณ„์ž

( ) - ํƒ€ ์—”ํ„ฐํ‹ฐ์™€์˜ ๊ด€๊ณ„๋ฅผ ํ†ตํ•ด ํƒ€ ์—”ํ„ฐํ‹ฐ๋กœ๋ถ€ํ„ฐ ๋ฐ›์•„์˜ค๋Š” ์‹๋ณ„์ž

-> ์ฃผ์‹๋ณ„์ž , ๋ณด์กฐ์‹๋ณ„์ž , ๋ณธ์งˆ ์‹๋ณ„์ž, ์™ธ๋ถ€ ์‹๋ณ„์ž

โ€‹

<๋‹จ๋‹ตํ˜•>

9.๊ฐ ์†์„ฑ์€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ ๋ฌด์—‡์ด๋ผ ํ•˜๋Š”๊ฐ€?

-> ๋„๋ฉ”์ธ

โ€‹

โ€‹

10. ์•„๋ž˜์˜ ๋นˆ์นธ์— ์•Œ๋งž์€ ๊ฒƒ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค

> ์œ ์ผ์„ฑ : ์ฃผ์‹๋ณ„์ž์— ์˜ํ•ด ์—”ํ„ฐํ‹ฐ๋‚ด์— ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๋“ค์„ ์œ ์ผํ•˜๊ฒŒ ๊ตฌ๋ถ„ํ•จ

์กด์žฌ์„ฑ : ์ฃผ์‹๋ณ„์ž๊ฐ€ ์ง€์ •๋˜๋ฉด ๋ฐ˜๋“œ์‹œ ๋ฐ์ดํ„ฐ ๊ฐ’์ด ์กด์žฌ

(์ตœ์†Œ์„ฑ) : ์ฃผ์‹๋ณ„์ž๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์†์„ฑ์˜ ์ˆ˜๋Š” ์œ ์ผ์„ฑ์„ ๋งŒ์กฑํ•˜๋Š” ์ตœ์†Œ์˜ ์ˆ˜๊ฐ€ ๋˜์–ด์•ผ ํ•จ

(๋ถˆ๋ณ€์„ฑ) : ์ฃผ์‹๋ณ„์ž๊ฐ€ ํ•œ ๋ฒˆ ํŠน์ • ์—”ํ„ฐํ‹ฐ์— ์ง€์ •๋˜๋ฉด ๊ทธ ์‹๋ณ„์ž์˜ ๊ฐ’์€ ๋ณ€ํ•˜์ง€ ์•Š์•„์•ผ ํ•จ


<2๊ณผ๋ชฉ : 37๋ฌธ์ œ (๊ฐ๊ด€์‹) + 3๋ฌธ์ œ (๋‹จ๋‹ตํ˜•) >

โ€‹

1. ์•„๋ž˜์˜ ๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ์•Œ๋งž์€ ๊ฒƒ์€? ( SQL ์ˆ˜ํ–‰ ํ›„ ๊ฒฐ๊ณผ ํ™•์ธ ์™„๋ฃŒ)

โ€‹

col1 col2 col3
--------------------
A null 1
B A 2
C A 3
D B 4
โ€‹
SELECT *
FROM SQLD46_01
WHERE COL3 <> 2
START WITH COL3 = 4
CONNECT BY COL1 = PRIOR COL2;

-> 2๊ฑด

โ€‹

2. ์•„๋ž˜์˜ SQL ์ค‘ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์€?

-> IFNULL ( COL, 'value') = nvl( COL, 'value') ์™€ ๋™์ผ

-> NULL ์ด๋ฉด ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋˜์–ด ์žˆ๋Š” ๊ฒƒ. IFNULL ( COL2, 0) ์œผ๋กœ ๋˜์–ด ์žˆ์œผ๋ฉด ๋จ

โ€‹

3. ์•„๋ž˜์˜ ๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ์˜ ๋นˆ์นธ์„ ์ฑ„์›Œ ๋„ฃ์œผ์‹œ์˜ค.

SELECT ..
FROM ...
WHERE ...
START WITH ( ) --> ๊ด€๋ฆฌ์ž IS NULL
CONNECT BY PRIOR ( ์‚ฌ์› ) = ( ๊ด€๋ฆฌ์ž )
โ€‹

4. ์•„๋ž˜์˜ SQL ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ( UNION ALL ๊ณผ UNION ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ)

SELECT A,B,C FROM TAB1
UNION ALL
SELECT A,B,C FROM TAB2
โ€‹
SELECT A,B,C FROM TAB1
UNION
SELECT A,B,C FROM TAB2

โ€‹

5. ์•„๋ž˜์˜ SQL ์ค‘ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์€? ( ์ „์ฒด ์ง‘๊ณ„๊ฐ€ ํฌํ•จ๋œ ์ง‘๊ณ„ํ•จ์ˆ˜ ๊ฒฐ๊ณผ ์ฐพ๊ธฐ)

1) SELECT A, SUM(..) FROM ...
UNION ALL
SELECT NULL, SUM(...) FROM ..
2) SELECT A, SUM(..) FROM ... GROUP BY GROUPING SETS(A) --> GROUPIUNG SETS(A) ๋Š” ์ „์ฒด ์ง‘๊ณ„๊ฐ€ ์—†์Œ

โ€‹

6. SQL ์˜ ๋ถ„๋ฅ˜์— ๋Œ€ํ•œ ์„ค๋ช…์ค‘ ํ‹€๋ฆฐ ๊ฒƒ์€?

-> ๋ฐ์ดํ„ฐ ์ œ์–ด์–ด๋Š” CREATE, ALTER, DROP ์ด๋‹ค. --> DDL ์ž„.

โ€‹

7. ๋งค์žฅID, ๋งค์žฅ๋ช…?? , ๋…„๋„๊ฐ€ ์žˆ๊ณ , ๋งค์žฅID๋Š” ์ง‘๊ณ„๊ฐ€ ์žˆ๊ณ , ๋งค์žฅID,๋งค์žฅ๋ช…์œผ๋กœ ๋ฌถ์ธ ์ง‘๊ณ„๊ฐ€ ๋…„๋„์™€ ๋ฌถ์—ฌ์„œ ๋‹ค์‹œ ์ง‘๊ณ„๊ฐ€ ๋จ

-> GROUP BY ROLLUP(๋งค์žฅID, ๋งค์žฅ๋ช…), ๋…„๋„

โ€‹

8. ์•„๋ž˜์˜ SQL ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์€? ( ๊ณต์ง‘ํ•ฉ์ผ ๊ฒฝ์šฐ ์ง‘๊ณ„ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋Š” NULL ์ž„)

1) SELECT NVL( SUM(COL1), 0 ) ... WHERE 1=2 --> 0
2) SELECT MIN(COL1) ... WHERE 1=2 --> NULL
3) SELECT COUNT(*) ... WHERE 1=2 --> 0
-> MIN(..) ์€ ๊ณต์ง‘ํ•ฉ์ผ ๊ฒฝ์šฐ NULL ์„ ๋ฐ˜ํ™˜ํ•จ

โ€‹

9. ์•„๋ž˜์˜ SQL ๊ฒฐ๊ณผ๋กœ ์˜ณ์€ ๊ฒƒ์€? ( UNION MINUS ์— ๋Œ€ํ•œ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ)

โ€‹SELECT *
UNION
SELECT *
MINUS
SELECT *
-> ์ˆœ์ฐจ์ ์œผ๋กœ ์ˆ˜ํ–‰. UNION ๊ฒฐ๊ณผ 1,2,3,4,5 MINUS 2 ์ด๋ฏ€๋กœ 1,3,4,5 -> 4๊ฐœ

โ€‹

10. ์•„๋ž˜์˜ SQL ์—์„œ ๊ฒฐ๊ณผ์™€ ์ƒ๊ด€์—†์ด ๋ชจ๋“  ๋กœ์šฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๋Š” ํ…Œ์ด๋ธ”์€?

<SQL>
SELECT ...
FROM TUTOR LEFT OUTER JOIN SESSIONS .....
-> TUTOR

โ€‹

11. ์•„๋ž˜์˜ SQL ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ๊ฒƒ์€? ( RANK, DENSE_RANK, ROW_NUMBER() ๊ตฌ๋ถ„)

1) SELECT ... COUNT... RANK... RN < 4 --> RANK 1,2,2,4,5
2) SELECT ... COUNT... DENSE_RANK ... RN < 4 --> 1,2,2,3,5 
3) SELECT ... COUNT... ROW_NUMBER() ... RN < 4 --> 1,2,3,4,5
4) SELECT ... COUNT... ROWNUMBER ... RN < 4 --> 1,2,3,4,5

โ€‹

12. Window Function ์ค‘ ์ˆœ์œ„ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ๊ฒƒ์€?

-> RATIO_TO_REPORT

โ€‹

13. ์•„๋ž˜์˜ SQL ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•˜๋Š” SQL ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ( WINDOW FUNCTION ์—์„œ ROWS ์™€ RANGE ๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฌธ์ œ)

-> A, 300

A, 300

โ€‹

ROWS BETWEEN ...

RANGE BETWEEN ....

โ€‹

-> ORDER BY ์˜ ์ค‘๋ณต์ด ์žˆ์„ ๋•Œ ํ•˜๋‚˜๋กœ ์ทจ๊ธ‰ํ•˜๋ฉฐ SUM ๊ฐ’์„ ๊ฐ™์€ ๊ฒƒ์œผ๋กœ ๊ฐ€์ ธ๊ฐ€๋ฉด RANGE, ์ค‘๋ณต์ด ์žˆ๋”๋ผ๋„ ROWS ๋‹จ์œ„๋กœ

SUM ์„ ํ•ด๋‚˜๊ฐ€๋ฉด ROWS ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

-> ์ค‘๋ณต ์ˆœ์„œ์— ๋Œ€ํ•ด ๋™์ผ ํ•ฉ์„ ๊ฐ€์ง€๋ฏ€๋กœ RANGE BETWEEN ์œผ๋กœ ํ™•์ธ๋จ

โ€‹

14. ์•„๋ž˜์˜ SQL ์ˆ˜ํ–‰ ์‹œ ๊ฒฐ๊ณผ๋กœ ์•Œ๋งž์€ ๊ฒƒ์€?

-> MAX(์—ฐ๋ด‰) OVER (ORDER BY ์—ฐ๋ด‰ ROWS CURRENT ROWS) --> ํ˜„์žฌ ์—ฐ๋ด‰์ด ์ถœ๋ ฅ๋จ

โ€‹

15. ์•„๋ž˜์˜ SQL ์ค‘ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์€? ( ์ค‘๋ณต ๋ฐ์ดํ„ฐ ์ œ๊ฑฐ์— ๋Œ€ํ•œ ๋ฌธ์ œ)

1) SELECT JOB
FROM .. WHERE ... EXISTS (SELECT ... FROM .... ) --> ์ค‘๋ณต ์ œ๊ฑฐ๊ฐ€ ์•„๋‹˜
2) SELECT DISTINCT JOB
3) SELECT JOB ... GROUP BY JOB
4) SELECT JOB UNION SELECT JOB ...

โ€‹

16. ํ‰๊ท ํ‚ค๊ฐ€ 180 ์ด์ƒ์ธ SQL ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€?

-> SELECT ...
FROM ...
WHERE ..
HAVING AVG(HEGIHT)>=180
GROUP BY ...
: HAVING ๊ณผ GROUP BY ๋Š” ์ˆœ์„œ๊ณผ ๋ฐ”๋€Œ์–ด๋„ ์ƒ๊ด€ ์—†์Œ

โ€‹

17. ์•„๋ž˜์˜ SQL ๊ฒฐ๊ณผ๋กœ ์˜ณ์€ ๊ฒƒ์€?

<SQL>
SELECT ... FROM ... WHERE SALARY >= (SELECT MAX(SALARY) FROM ... GROUP BY DEPT )
1) single-row subquery returns more than one row ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•จ

 

18. ์•„๋ž˜์˜ ERD ์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ์˜ฎ์€ ๊ฒƒ์€? ( ํ™•์‹คํ•˜์ง€ ์•Š์Œ)

์ง€์  ---<- ๊ณ ๊ฐ

-> ์ง€์ ์— ์†ํ•˜์ง€ ์•Š๋Š” ๊ณ ๊ฐ์€ ๋ณ„๋„๋กœ ์ถ”์ถœํ•ด์•ผ ํ•œ๋‹ค.

โ€‹

19. ์•„๋ž˜์˜ SQL ์ค‘ ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€ ์•Š๋Š” ๊ฒƒ์€?

1) SELECT A, SUM(..) FROM ... GROUP BY A ORDER BY C --> ์—๋Ÿฌ ๋ฐœ์ƒ.
2) SELECT A, SUM(..) FROM ... GROUP BY A HAVING COUNT(C)
* ์ผ๋ถ€ DBMS ์—์„  1) ๋„ ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€ ์•Š์Œ.

โ€‹

20. ์˜ค๋ผํด, SQL SERVER ์—์„œ ์•„๋ž˜์˜ SQL ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ์ ์ ˆํ•˜์ง€ ์•Š์€ ๊ฒƒ์€?

UPDATE .. SET COL2 = 200
CREATE TABLE TAB1 ...
ROLLBACK
-> ORACLE ์—์„œ ROLLBACK ์ด ๋˜๋ฉด ํ…Œ์ด๋ธ”๋„ ๋งŒ๋“ค์–ด์ง€์ง€ ์•Š๋Š”๋‹ค.

โ€‹

21. ROLLBACK ์— ๋Œ€ํ•œ ์„ค๋ช…์ค‘ ์ ์ ˆํ•˜์ง€ ์•Š์€ ๊ฒƒ์€?

-> ROLLBACK ๋˜๋ฉด ํ…Œ์ด๋ธ” ์ƒ์„ฑ๋„ ์ทจ์†Œ๋œ๋‹ค.

โ€‹

22. ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์› (5 ROWS) , ๋ถ€์„œ (3 ROWS) ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์„ ๋•Œ SQL ๊ฒฐ๊ณผ๋Š” ?

>> CORSS JOIN ํ˜•ํƒœ ๋ฌธ์ œ. 15๊ฐœ

โ€‹

23. ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๋Š” ๋ช…๋ น์–ด๋Š”?

-> GRANT

โ€‹

24. ์•„๋ž˜์˜ SQL ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ์˜ณ์€ ๊ฒƒ์€?

SQL> SELECT ์šด์ „์ž, COUNT(์šดํ–‰๋ฒˆํ˜ธ) AS ์šดํ–‰ ํšŸ์ˆ˜ FROM ... GROUP BY ์šด์ „์ž

1) ์šด์ „์ž์™€ ์šดํ–‰ํšŸ์ˆ˜๋ฅผ ๋ชจ๋‘ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

โ€‹

25. ์•„๋ž˜์˜ SQL ์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ์˜ณ์€ ๊ฒƒ์€? ( IN ์ ˆ์— ๋Œ€ํ•œ ์„ค๋ช… )

SQL) SELECT ... FROM ... WHERE COL1 IN ('์„œ์šธ','๋Œ€์ „','๋ถ€์‚ฐ')

2) ์„œ์šธ์ด๊ฑฐ๋‚˜ ๋Œ€์ „ ๋˜๋Š” ๋ถ€์‚ฐ์— ์†ํ•œ ........ --> IN ์€ OR ์กฐ๊ฑด์œผ๋กœ ๋˜๋Š” ์œผ๋กœ ๊ตฌ๋ถ„๋จ

โ€‹

26. ์•„๋ž˜์˜ SQL ๊ฒฐ๊ณผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ( ORACLE ์ผ ๋•Œ NULL ์˜ ์ˆœ์œ„ ๋ฐ‘ DESC ์— ๋Œ€ํ•œ ์ดํ•ด)

SQL> SELECT ...
FROM ..
ORDER BY COL1, COL2, COL3 DESC
-> ORACLE ์€ NULL ์ด ๊ฐ€์žฅ ํฐ ๊ฐ’์œผ๋กœ ์ธ์‹ํ•จ. ์ฆ‰, ASC ๋กœ ๋  ๊ฒฝ์šฐ ๊ฐ€์žฅ ๋’ค๋กœ ๊ฐ.

โ€‹

27. ์•„๋ž˜์˜ SQL ํ•จ์ˆ˜๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์„ ๊ณ ๋ฅด์‹œ์˜ค.

๋ณด๊ธฐ> ๋ฌธ์ž์—ด์—์„œ M ์˜ ์œ„์น˜์—์„œ N๊ฐœ ๋งŒํผ์„ ๋ฆฌํ„ด

1) SUBSTR / SUBSTRING

โ€‹

28. ์•„๋ž˜์˜ ํŠธ๋žœ์žญ์…˜์— ๋Œ€ํ•œ ํŠน์„ฑ์— ๋Œ€ํ•œ ์„ค๋ช… ์ค‘ ์ ์ ˆํ•œ ๊ฒƒ์€?

๋ณด๊ธฐ> ํŠธ๋žœ์žญ์…˜์—์„œ ์ •์˜๋œ ์—ฐ์‚ฐ๋“ค์€ ๋ชจ๋‘ ์„ฑ๊ณต์ ์œผ๋กœ ์‹คํ–‰๋˜๋˜์ง€ ์•„๋‹ˆ๋ฉด ์ „ํ˜€ ์‹คํ–‰๋˜์ง€ ์•Š์€ ์ƒํƒœ๋กœ ๋‚จ์•„ ์žˆ์–ด์•ผ ํ•จ (ALL OR NOTHING)

-> ์›์ž์„ฑ

โ€‹

29. ์•„๋ž˜์˜ SQL ๊ฒฐ๊ณผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ( NOT IN ์ ˆ์—์„œ NULL ์ด ์žˆ์—ˆ์„ ๋•Œ ์˜ ๊ฒฐ๊ณผ )

SQL> SELECT ... FROM ... WHERE COL1 NOT IN (SELECT COL2 FROM ... ) --> COL2 ๊ฐ€ NULL ์ด ์žˆ์Œ
-> ๊ฒฐ๊ณผ 0 ๊ฑด ๋‚˜์˜ด

โ€‹

30. ์•„๋ž˜์˜ SQL ๊ฒฐ๊ณผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ( CASE ๋ฌธ์ด ์‚ฌ์šฉ๋  ๋•Œ์˜ ๊ฒฐ๊ณผ ๋ฌป๋Š” ๋ฌธ์ œ)

SQL> SELECT ... CASE ... WHEN COL1 <= 100 THEN 'B'
WHEN COL2 <= 200 THEN 'A'
ELSE 'S'
-> B A A S ( CASE ๊ฐ’์ด ์œ„์™€ ๊ฐ™์„ ๊ฒฝ์šฐ)

โ€‹

31. ๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์กฐ์ธ์€?

-> Self Join

โ€‹

32. ์•„๋ž˜์˜ SQL ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ( ROUND ์—์„œ ์Œ์ˆ˜๊ฐ’ ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์„ ๋•Œ์˜ ๋ฌธ์ œ)

SQL> ROUND( 10333.3333. -2) ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฌป๋Š” ๋ฌธ์ œ
-> 10300

โ€‹

33. ์•„๋ž˜์˜ SQL ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ( COALESCE ์— ๋Œ€ํ•œ ๋ฌธ์ œ )

SQL> SELECT ... COALESCE(COM, SAL) --> COM ์ด NULL ์ด๋ฉด SAL ์•„๋‹ˆ๋ฉด COM

โ€‹

34. ์•„๋ž˜์˜ SQL ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€?

SQL>
SELECT ..
FROM (SELECT .. ROW_NUMBER() OVER (...) RN )
WHERE RN <=2
-> 150, 140 ???

โ€‹

35. ์•„๋ž˜์˜ SQL ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€?

SQL> SELECT COUNT(COL), AVG(COL2)
FROM.
WHERE COL2 > 21
-> 3, 27

โ€‹

36.

โ€‹

37.

โ€‹

<๋‹จ๋‹ตํ˜•>

โ€‹

38. 6๊ฐœ ๊ฐ’์ค‘ 2๊ฐœ์˜ NULL ์ด ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ AVG ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

SQL> SELECT AVG(COL1) - AVG(NVL(COL1,0) ) ===> 6 - 4 = 2

โ€‹

39. ๋ถ€์„œ ์—†๋Š” ์‚ฌ์›์„ ์กฐํšŒํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์กฐ์ธ : ์‚ฌ์›์ด ์˜ค๋ฅธ์ชฝ์ด๋ฏ€๋กœ RIGHT OUTER JOIN

โ€‹

 

40. GRADE ์— ๋Œ€ํ•œ ์ง‘๊ณ„์™€ GRADE,JOB ์— ๋Œ€ํ•œ ์ง‘๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚˜๋Š” SQL ์ž‘์„ฑ ๋ฌธ์ œ

-> GROUPING SETS ( ใ„ฑ, (ใ„ด, ใ„ท) )

-> GRADE, GRADE, JOB

* ๋ฌธ์ œ๊ฐ€ ๋‘๊ฐœ ํ…Œ์ด๋ธ”์˜ ์กฐ์ธ ๊ฒฐ๊ณผ ์˜€๋Š”์ง€ ๊ธฐ์–ต์ด ์•ˆ๋‚˜๋Š”๋ฐ, ๋‘๊ฐœ ํ…Œ์ด๋ธ”์ด๊ณ  ์ปฌ๋Ÿผ์ด ๋‘ ํ…Œ์ด๋ธ”์— ๊ฐ™์ด ์กด์žฌํ•œ๋‹ค๋ฉด,

alias (A.GRADE ๋“ฑ) ๋ฅผ ์จ์ค˜์•ผ ํ•˜๋ฉฐ, ํ…Œ์ด๋ธ” ํ•˜๋‚˜์—๋งŒ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ๋Š” alias ๋ฅผ ์จ์ฃผ์ง€ ์•Š์•„๋„ ๋จ

* ํ…Œ์ด๋ธ”์ด ํ•œ๊ฐœ์˜€๋‹ค๋ฉด alias ๋Š” ์จ๋„ ๋˜๊ณ , ์•ˆ์จ๋„ ๋จ.

* GROUPING SETS (A, B) = GROUP BY (A) UNION ALL GROUP BY (B) ์ด๋ฏ€๋กœ GRADE , JOB, GRADE ๋กœ ์จ๋„ ์ƒ๊ด€์—†์Œ

 

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

โ€‹

โ€‹

โ€‹

โ€‹

โ€‹

โ€‹

โ€‹

โ€‹

โ€‹

 

300x250

์ฝ”๋“œ