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

[SQLD] 38ํšŒ ๊ธฐ์ถœ ๋ฌธ์ œ ( 50๋ฌธ์ œ / ์ •๋‹ต ) + ํ•ด์„ค์ถ”๊ฐ€

by yunamom 2022. 5. 25.
๋ฐ˜์‘ํ˜•
โณ๋‚จ์€์‹œ๊ฐ„ : 87๋ถ„27์ดˆ

๐Ÿ“–SQLD. ์ œ 38 ํšŒ ๊ธฐ์ถœ ๋ฌธ์ œ


๋ฌธ์ œ 1. ๋‹ค์Œ์˜ ERD์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

1) ์ƒ๊ธ‰์ข…ํ•ฉ๋ณ‘์›์—๋Š” ์˜์‚ฌ๊ฐ€ ๊ทผ๋ฌดํ•˜์ง€ ์•Š์„์ˆ˜๊ฐ€ ์žˆ๋‹ค.

2) ํ•œ ๊ฐœ์˜ ์ƒ๊ธ‰์ข…ํ•ฉ๋ณ‘์›์—๋Š” ์—ฌ๋Ÿฌ ๋ช…์˜ ์˜์‚ฌ๊ฐ€ ๊ทผ๋ฌดํ•œ๋‹ค.

3) ์ง„๋ฃŒ๋Š” ๋ฐ˜๋“œ์‹œ ์˜์‚ฌ๊ฐ€ ํ•ด์•ผ ํ•œ๋‹ค.

4) ์˜์‚ฌ๊ฐ€ ์—†์ด ์ง„๋ฃŒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋ฌธ์ œ 2. ์„ฑ๋Šฅ์„ ๊ณ ๋ คํ•œ ๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง์—์„œ ๊ณ ๋ คํ•ด์•ผ ํ•  ์‚ฌํ•ญ์œผ๋กœ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

 
1) ์„ฑ๋Šฅ ํŠœ๋‹์„ ์œ„ํ•ด์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜๋Š” ํŠธ๋žœ์žญ์…˜ ์œ ํ˜•์€ ๋ฌด์‹œํ•ด๋„ ๋œ๋‹ค.

2) ๋ฐฐ์น˜๋ฅผ ํ†ตํ•ด์„œ ์ž…๋ ฅ๋˜๋Š” ๋ฐ์ดํ„ฐ ์šฉ๋Ÿ‰์ด ํฌ๋ฉด ํด์ˆ˜๋ก ์„ฑ๋Šฅ ํŠœ๋‹์„ ์œ„ํ•œ ๋น„์šฉ์€ ์ฆ๊ฐ€๋œ๋‹ค.

3) ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ์œ„ํ•ด์„œ ํŠœ๋‹์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ชจ๋ธ๋ง์ด ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋‹ค.

4) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ชจ๋ธ๋ง ์‹œ์— ์„ฑ๋Šฅ์„ ๊ณ ๋ คํ•œ ๋ชจ๋ธ๋ง์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ์„ฑ๋Šฅ ๋น„์šฉ์„ ๊ฐ์†Œ ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

 

๋ฌธ์ œ 3. ๋‹ค์Œ ์ค‘ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋…ผ๋ฆฌ ๋ชจ๋ธ์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

1) ๊ฐœ๋… ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์€ ์‚ฌ์šฉ์ž ๊ด€์ ์—์„œ ๋ฐ์ดํ„ฐ ์š”๊ตฌ์‚ฌํ•ญ์„ ์‹๋ณ„ํ•œ๋‹ค.

2) ๋…ผ๋ฆฌ ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์€ M:N ๊ด€๊ณ„ํ•ด์†Œ, ์‹๋ณ„์ž ํ™•์ •, ์ •๊ทœํ™”, ๋ฌด๊ฒฐ์„ฑ ์ •์˜ ๋“ฑ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

3) ๋…ผ๋ฆฌ ๋ชจ๋ธ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊ตฌ์ถ•์„ ์œ„ํ•ด์„œ๋งŒ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

4) ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฌผ๋ฆฌ์ ์œผ๋กœ ์ €์žฅ๋˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด ๋ฌผ๋ฆฌ์  ๋ชจ๋ธ์ด๋‹ค.


๋ฌธ์ œ 4. ์•„๋ž˜ ERD์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ๊ฐ€์žฅ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

1) ์‚ฌ์›์€ ๋™์ผํ•œ ์ฝ˜๋„๋ฅผ ์˜ˆ์•ฝํ•ด์„œ ๋ฐ˜๋ณต์ ์œผ๋กœ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋‹ค.

2) ํšŒ์‚ฌ ์ฝ˜๋„๋Š” ๋ˆ„๊ตฌ๋„ ์ด์šฉํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.

3) ์‚ฌ์›์€ ๋™์ผ ์ผ์ž์— ์—ฌ๋Ÿฌ ์ฝ˜๋„๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

4) ์—ฌ๋Ÿฌ ์‚ฌ์›์ด ๋™์ผํ•œ ์ฝ˜๋„๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋ฌธ์ œ 5. ๋ฆด๋ ˆ์ด์…˜์„ ์ •๊ทœํ™”(Normalization)ํ•˜๋Š” ๋ชฉ์ ์— ๊ด€ํ•œ ์„ค๋ช… ์ค‘ ๊ฐ€์žฅ ๊ฑฐ๋ฆฌ๊ฐ€ ๋จผ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

1) ์ •๋ณด์˜ ๊ฐฑ์‹  ์ด์ƒ์ด ์ƒ๊ธฐ์ง€ ์•Š๋„๋ก ํ•œ๋‹ค.

2) ์ •๋ณด์˜ ๋ณด์•ˆ์„ ๋ชฉ์ ์œผ๋กœ ํ•œ๋‹ค.

3) ์ •๋ณด์˜ ์†์‹ค์„ ๋ง‰๋Š”๋‹ค.

4) ์ •๋ณด์˜ ์ค‘๋ณต์„ ๋ง‰๋Š”๋‹ค.

 

๋ฌธ์ œ 6. ์†์„ฑ์— ๋Œ€ํ•œ ์•„๋ž˜์˜ ์„ค๋ช…์—์„œ ๋นˆ์นธ์— ๋“ค์–ด๊ฐˆ ๊ฒƒ์œผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

(ใ„ฑ)์€ ์—”ํ„ฐํ‹ฐ๋ฅผ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ์†์„ฑ์ด๊ณ  (ใ„ด)
์€ ๋‹ค๋ฅธ ์—”ํ„ฐํ‹ฐ์˜ ๊ด€๊ณ„์— ํฌํ•จ๋˜๋Š” ์†์„ฑ์ด๋‹ค.
๋‹ค๋ฅธ ์—”ํ„ฐํ‹ฐ์˜ ๊ด€๊ณ„์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ์†์„ฑ์„
(ใ„ท)์ด๋ผ๊ณ  ํ•œ๋‹ค.
1) (ใ„ฑ)๊ธฐ๋ณธํ‚ค์†์„ฑ - (ใ„ด)์™ธ๋ž˜ํ‚ค์†์„ฑ - (ใ„ท)์ผ๋ฐ˜์†์„ฑ

2) (ใ„ฑ)์™ธ๋ž˜ํ‚ค์†์„ฑ - (ใ„ด)๊ธฐ๋ณธํ‚ค์†์„ฑ - (ใ„ท)ํŒŒ์ƒ์†์„ฑ

3) (ใ„ฑ)ํŒŒ์ƒ์†์„ฑ - (ใ„ด)์™ธ๋ž˜ํ‚ค์†์„ฑ - (ใ„ท)๊ธฐ๋ณธํ‚ค์†์„ฑ

4) (ใ„ฑ)์ผ๋ฐ˜์†์„ฑ - (ใ„ด)๊ธฐ๋ณธํ‚ค์†์„ฑ - (ใ„ท)์™ธ๋ž˜ํ‚ค์†์„ฑ

 

๋ฌธ์ œ 7. ๋‹ค์Œ ๋ณด๊ธฐ ์ค‘ ์—”ํ„ฐํ‹ฐ, ๊ด€๊ณ„, ์†์„ฑ์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ



1) ํ•œ ๊ฐœ์˜ ์—”ํ„ฐํ‹ฐ๋Š” ๋‘ ๊ฐœ ์ด์ƒ์˜ ์ธ์Šคํ„ด์Šค์˜ ์ง‘ํ•ฉ์ด์–ด์•ผ ํ•œ๋‹ค.

2) ์—”ํ„ฐํ‹ฐ๋Š” ๊ด€๊ณ„๋ฅผ ๋‘ ๊ฐœ๊นŒ์ง€๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

3) ํ•œ ๊ฐœ์˜ ์—”ํ„ฐํ‹ฐ๋Š” ๋‘ ๊ฐœ ์ด์ƒ์˜ ์†์„ฑ์„ ๊ฐ–๋Š”๋‹ค.

4) ํ•œ ๊ฐœ์˜ ์†์„ฑ์€ ํ•œ ๊ฐœ์˜ ์†์„ฑ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค.

 

๋ฌธ์ œ 8. ๋‹ค์Œ ๋ณด๊ธฐ ์ค‘ ์•„๋ž˜ ERD์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

1) ์ƒํ’ˆ์€ ์ฃผ๋ฌธ์„ ํ•˜๋‚˜ ์ด์ƒ ๋ฐ˜๋“œ์‹œ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค.

2) ์ฃผ๋ฌธ์€ ์ƒํ’ˆ 1๊ฐœ ์ด์ƒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

3) ์ฃผ๋ฌธ์€ ์ƒํ’ˆ์ด ์—†์„ ์ˆ˜ ์žˆ๋‹ค.

4) ์ฃผ๋ฌธ, ์ƒํ’ˆ์€ ๋น„์‹๋ณ„๊ด€๊ณ„๋กœ ๋ถ€๋ชจ๊ฐ€ ์—†์–ด๋„ ์ž์‹์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค.

 

๋ฌธ์ œ 9. ๋‹ค์Œ ์ฃผ์–ด์ง„ ๊ทธ๋ฆผ์— ํ•ด๋‹นํ•˜๋Š” ERD ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ์•Œ๋งž์€ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

1) Barker

2) IE

3) IE Notation

4) IDEF1X

 

๋ฌธ์ œ 10. ์•„๋ž˜์˜ ๋‚ด์šฉ์€ ์ฃผ์‹๋ณ„์ž์˜ ์–ด๋–ค ํŠน์ง•์„ ์„ค๋ช…ํ•œ ๊ฒƒ์ธ๊ฐ€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

ํ•™์ƒ์˜ ํ•™๋ฒˆ์œผ๋กœ ๊ณ ์œ ํ•œ ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค.
ํ•˜์ง€๋งŒ ABC๋Œ€ํ•™๊ต์˜ ํ•™์ƒ ์—”ํ„ฐํ‹ฐ์˜ ์ฃผ์‹๋ณ„์ž๋ฅผ 
ํ•™๋ฒˆ๊ณผ ์ž…ํ•™์ผ์ž๋กœ ํ•ด์„œ ์ž˜๋ชป๋œ ๋ชจ๋ธ๋ง์„ ํ–ˆ๋‹ค.
1) ์œ ์ผ์„ฑ

2) ์ตœ์†Œ์„ฑ

3) ๋ถˆ๋ณ€์„ฑ

4) ์กด์žฌ์„ฑ

 

๋ฌธ์ œ 11. ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์•„๋ž˜์˜ SQL๋ฌธ์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋กœ ์ ์ ˆํ•œ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

[TEST11]

COL1    COL2    COL3
--------------------
A               1
B       A       2
C       A       3
D       B       4

[SQL]

SELECT COUNT(*) FROM TEST11
WHERE COL3 <> 3
START WITH COL3 = 4
CONNECT BY COL1 = PRIOR COL2;
1) 0

2) 1

3) 2

4) 3

 

๋ฌธ์ œ 12. ๋‹ค์Œ ๋ณด๊ธฐ ์ค‘ JOIN์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

1) Nested Loop Join์€ ๋žœ๋ค ์—‘์„ธ์Šค (Random Access)๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

2) Sort Merge Join์€ ์ •๋ ฌ์„ ์œ ๋ฐœํ•˜์—ฌ ์กฐ์ธํ•˜๋Š” ํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

3) ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ธํ•  ํ›„ํ–‰ ํ…Œ์ด๋ธ”์— ์ธ๋ฑ์Šค๊ฐ€ ์—†์„ ๊ฒฝ์šฐ Nested Loop Join์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

4) Hash Join์€ ์ •๋ ฌ์ž‘์—…์ด ์—†์–ด ์ •๋ ฌ์ด ๋ถ€๋‹ด๋˜๋Š” ๋Œ€๋Ÿ‰๋ฐฐ์น˜์ž‘์—…์— ์œ ๋ฆฌํ•˜๋‹ค.

 

๋ฌธ์ œ 13. ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๋Š” DCL ๋ช…๋ น์–ด๋Š”? ์ •๋‹ตํ™•์ธ๐ŸŒผ

 
1) COMMIT

2) GRANT

3) REVOKE

4) ROLLBACK

 

๋ฌธ์ œ 14. ๋‹ค์Œ ๋ณด๊ธฐ ์ค‘ SQL ๋ช…๋ น์–ด๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

 

1) DDL : TRUNCATE

2) DDL : ALTER

3) DCL : REVOKE

4) DML : RENAME

 

๋ฌธ์ œ 15. ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” SQL๋ฌธ์„ ๊ณ ๋ฅด์‹œ์˜ค.  ์ •๋‹ตํ™•์ธ๐ŸŒผ

[TEST15]
BAN      NAME
-------------
1       ์กฐ์กฐ
1       ์กฐ์กฐ
1       ์กฐ์กฐ
2       ์—ฌํฌ
2       ์œ ๋น„
3       ๊ด€์šฐ
3       ๊ด€์šฐ

[RESULT]
BAN   RESULT
---------------
1       1
3       1
2       2
 
1)                              2)
SELECT BAN, COUNT(*) AS RESULT   SELECT BAN, COUNT(1) AS RESULT
FROM TEST15                      FROM TEST15
GROUP BY BAN;                    GROUP BY BAN;

3)                              4)
SELECT BAN,                      SELECT
COUNT(DISTINCT NAME) AS RESULT   COUNT(CASE WHEN BAN=1 THEN 1 END)
FROM TEST15                      AS RESULT,
GROUP BY BAN;                    COUNT(CASE WHEN BAN=2 THEN 1 END)
                                 AS B,
                                 COUNT(CASE WHEN BAN=3 THEN 1 END)
                                 AS C
                                 FROM TEST15;

 

๋ฌธ์ œ 16. ๋‹ค์Œ ๋ณด๊ธฐ ์ค‘ ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์ˆ˜ํ–‰ํ•˜์˜€์„ ๋•Œ ๊ฒฐ๊ณผ๊ฐ’์ด ๋‹ค๋ฅธ ๊ฒƒ์€?  ์ •๋‹ตํ™•์ธ๐ŸŒผ

[TEST 16]
MemberID    Name
-----------------
NULL        ์กฐ์กฐ
2           ์—ฌํฌ
3           ๊ด€์šฐ
4           ์žฅ๋น„
5           ์กฐํ›ˆ
NULL        ์œ ๋น„
 
1) SELECT COUNT(3) FROM TEST16;

2) SELECT COUNT(MemberID) FROM TEST16;

3) SELECT COUNT(NULLIF (MemberID, NULL)) FROM TEST16;

4) SELECT COUNT(*) FROM TEST16 WHERE MemberID IS NOT NULL;

 

๋ฌธ์ œ 17. ๋‹ค์Œ ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”์—์„œ ํ•ด๋‹น SQL๋ฌธ์„ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋กœ ์•Œ๋งž์€ ๊ฒƒ์€?์ •๋‹ตํ™•์ธ๐ŸŒผ

[TEST17]
COL1     COL2
-------------
NULL      A
1         B
2         C
3         D
4         E

[SQL]
SELECT*FROM TEST17 WHERE COL1 IN(1, 2, NULL);
 
1)                        2)
COL1     COL2              COL1     COL2
-------------              -------------
1         B                 2         B
2         C                 2         C

3)                        4)
COL1     COL2              COL1     COL2
-------------              -------------
1         B                NULL       A
2         C                 1         B
3         D                 2         C
4         E                 3         D
                            4         E

 

๋ฌธ์ œ 18. ๋‹ค์Œ ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ๊ฒฐ๊ณผ๊ฐ’ ๊ฐ™์ด ๋ฐ˜ํ™˜๋˜๊ฒŒ ํ•˜๋Š” ์•„๋ž˜ SQL๋ฌธ์˜ ๋นˆ์นธ์— ๋“ค์–ด๊ฐˆ ๊ฒƒ์œผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

[TEST18]
DNAME        JOB        SAL
----------------------------
ACCOUNTING   CLERK      1000
ACCOUNTING   MANAGER    2000
ACCOUNTING   PRESIDENT  3000
RESEARCH     CLERK      4000
RESEARCH     MANAGER    5000
RESEARCH     PRESIDENT  6000
SALES        CLERK      7000
SALES        MANAGER    8000
SALES        PRESIDENT  9000

[SQL]
SELECT DNAME, JOB, SUM(SAL)
FROM TEST18
GROUP BY(           )

[๊ฒฐ๊ณผ]
DNAME       JOB        SUM(SAL)
-------------------------------
                       45000
            CLERK      12000  
            MANAGER    15000
            PRESIDENT  18000
SALES                  24000
SALES       CLERK      7000
SALES       MANAGER    8000
SALES       PRESIDENT  9000
RESEARCH               15000
RESEARCH    CLERK      4000
RESEARCH    MANAGER    5000
RESEARCH    PRESIDENT  6000
ACCOUNTING             6000
ACCOUNTING  CLERK      1000
ACCOUNTING  MANAGER    2000
ACCOUNTING  PRESIDENT  3000
1) CUBE(DNAME, JOB)

2) ROLLUP(DNAME, JOB)

3) GROUPING SETS(DNAME, JOB)

4) CUBE(DNAME)

 

๋ฌธ์ œ 19. ๋‹ค์Œ ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์•„๋ž˜์˜ SQL๋ฌธ์„ ์‹คํ–‰ํ•˜์˜€์„ ๋•Œ์˜ ๊ฒฐ๊ณผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

[TEST19]
C1        C2
-------------
1         80
2         70
3         80
4         90
5         100
6         110

[SQL]
SELECT C1, C2,
CASE
WHEN C2 <= 100 THEN 'B'
WHEN C2 <= 300 THEN 'A'
ELSE 'S'
END GRADE
FROM TEST19
ORDER BY C2;

1)                 2)
C1   C2   GRADE     C1   C2   GRADE
---------------     ---------------
2    70     B       6    70     B
1    80     B       2    80     B
3    80     B       1    80     B
4    90     B       3    90     A
5    100    B       4    100    A
6    110    A       5    110    A

3)                 4)
C1   C2   GRADE     C1   C2   GRADE
---------------     ---------------
6    30     A       6    70     A
2    70     A       2    80     A
1    80     A       1    80     A
3    100    A       3    90     B
4    150    B       4    100    B
5    300    B       5    110    B

 

๋ฌธ์ œ 20. SELECT NVL(COUNT(*), 9999) FROM TABLE WHERE 1 = 2 ์˜ ๊ฒฐ๊ณผ๊ฐ’์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

1) 9999

2) 0

3) NULL

4) 1

 

๋ฌธ์ œ 21. ์•„๋ž˜์˜ WINDOW FUNCTION์„ ์‚ฌ์šฉํ•œ SQL ์ค‘ ๊ฐ€์žฅ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒƒ์€?์ •๋‹ตํ™•์ธ๐ŸŒผ

 

1)
SUM(SAL) OVER()

2)
SUM(SAL) 
 OVER(PARTITION BY JOB 
          ORDER BY EMPNO RANGE
      BETWEEN UNBOUNDED PRECEDING
      AND UNBOUNDED FOLLOWING
      ) SAL1

3)
SUM(SAL)
 OVER(PARTITION BY JOB
          ORDER BY JOB RANGE
      BETWEEN UNBOUNDED PRECEDING
      AND CURRENT ROW
      ) SAL2

4)
SUM(SAL)
 OVER(PARTITION BY JOB
          ORDER BY EMPNO RANGE
      BETWEEN UNBOUNDED PRECEDING
      AND UNBOUNDED PRECEDING
      ) SAL3

 

๋ฌธ์ œ 22. ๋‹ค์Œ์˜ PL/SQL์— ๋Œ€ํ•œ ์„ค๋ช…์ด๋‹ค. ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

1) PL/SQL์€ ์ ˆ์ฐจํ˜• ์–ธ์–ด์ด๋‹ค.

2) PL/SQL์—์„œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•  ์ˆ˜๋Š” ์—†๋‹ค.

3) PL/SQL์—์„œ ์กฐ๊ฑด๋ฌธ์€ IF ~ THEN ~ ELSE IF ~ END IF์™€ CASE ~ WHEN์„ ์‚ฌ์šฉํ•œ๋‹ค.

4) PL/SQL์—์„œ NAME์ด๋ผ๋Š” ๋ณ€์ˆ˜์— 'aaa'๋ฅผ ๋Œ€์ž…ํ•  ๊ฒฝ์šฐ ":="์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

๋ฌธ์ œ 23. ๋‹ค์Œ ๋ณด๊ธฐ ์ค‘ ์ธ๋ฑ์Šค ์ƒ์„ฑ ๊ตฌ๋ฌธ์œผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

1) ALTER TABLE [ํ…Œ์ด๋ธ”๋ช…] ADD INDEX [์ธ๋ฑ์Šค ๋ช…] [์นผ๋Ÿผ๋ช…]

2) INDEX [์ธ๋ฑ์Šค ๋ช…] [์นผ๋Ÿผ๋ช…]

3) CREATE INDEX [์ธ๋ฑ์Šค ๋ช…] ON [ํ…Œ์ด๋ธ” ๋ช…] [์นผ๋Ÿผ๋ช…]

4) DROP INDEX FROM [ํ…Œ์ด๋ธ”๋ช…]

 

๋ฌธ์ œ 24. ์ฃผ์–ด์ง„ SQL๋ฌธ์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

[SQL]
INSERT INTO TEST24 VALUES(1);
INSERT INTO TEST24 VALUES(2);
COMMIT;
INSERT INTO TEST24 VALUES(3);
SAVEPOINT SP;
INSERT INTO TEST24 VALUES(4);
ROLLBACK TO SP;
SELECT COUNT(*) FROM TEST24;
1) 2

2) 3

3) 5

4) 6

 

๋ฌธ์ œ 25. TEST25 ํ…Œ์ด๋ธ”์— 1,2,3์˜ 3๊ฐœ์˜ ํ–‰์ด ์žˆ์„๋•Œ ๋‹ค์Œ์˜ SQL์‹คํ–‰๊ฒฐ๊ณผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€?  ์ •๋‹ตํ™•์ธ๐ŸŒผ

SELECT * FROM TEST25
MINUS
SELECT 1 FROM DUAL;
1) 1, 2, 3

2) 2, 3

3) 1, 2

4) 1

 

๋ฌธ์ œ 26. ORDERSํ…Œ์ด๋ธ”์—๋Š” CUSTOMERS ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ณ ๊ฐID๊ฐ€ ์žˆ๋‹ค. ํ•ด๋‹น SQL๋ฌธ์—์„œ (      )๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€?  ์ •๋‹ตํ™•์ธ๐ŸŒผ

SELECT * FROM ORDERS
WHERE (  ใ„ฑ  ) (SELECT * FROM
               CUSTOMERS
               WHERE(  ใ„ด  )
               );
1) ใ„ฑ : EXISTS, ใ„ด : CUSTOMERS.ID = ORDERS.ID

2) ใ„ฑ : EXISTS, ใ„ด : CUSTOMERS.ID <> ORDERS.ID

3) ใ„ฑ : NOT EXISTS, ใ„ด : CUSTOMERS.ID = ORDERS.ID

4) ใ„ฑ : NOT EXISTS, ใ„ด : CUSTOMERS.ID <> ORDERS.ID

 

๋ฌธ์ œ 27. ๋ฆด๋ ˆ์ด์…˜ EMP, DEPT๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋˜์–ด ์žˆ๋‹ค. ์‚ฌ์›์ด ํ•œ๋ช…๋„ ์—†๋Š” ๋ถ€์„œ(DEPTNO)๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ์งˆ์˜๋ฅผ ์ž‘์„ฑํ–ˆ์„๋•Œ, ๊ฐ€์žฅ ๊ฑฐ๋ฆฌ๊ฐ€ ๋จผ ๊ฒƒ์€?(๋‹จ, EMP์˜ DEPTNO์€ DEPT์˜ DEPTNO์„ ์ฐธ์กฐํ•˜๋Š” ์™ธ๋ž˜ํ‚ค์ด๋‹ค.)  ์ •๋‹ตํ™•์ธ๐ŸŒผ

EMP(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
    
DEPT(DEPTNO, DNAME, LOC)
1)
 SELECT DEPTNO FROM DEPT
 WHERE DEPTNO NOT IN
 (SELECT DEPTNO FROM EMP);
 
2)
 SELECT DEPTNO FROM DEPT A
 WHERE NOT EXISTS
 (SELECT * FROM EMP B WHERE
 A.DEPTNO = B.DEPTNO);
 
3)
 SELECT B.DEPTNO FROM EMP A
 RIGHT OUTER JOIN
 DEPT B ON A.DEPTNO = B.DEPTNO
 WHERE EMPNO IS NULL;
 
4)
 SELECT DEPTNO FROM DEPT
 WHERE DEPTNO <> ANY (SELECT
 DEPTNO FROM EMP);

 

๋ฌธ์ œ 28. ๋‹ค์Œ ๋ฆด๋ ˆ์ด์…˜์— ๋Œ€ํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑํ•˜์˜€๋‹ค. ๋‹ค์Œ ์ค‘ ์ƒ์„ฑ๋œ ์ธ๋ฑ์Šค์— ์˜ํ•˜์—ฌ ๊ฒ€์ƒ‰์†๋„๋ฅผ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ์งˆ์˜๋กœ ๊ฐ€์žฅ ์ ์ ˆํ•˜์ง€ ์•Š์€ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

[๋ฆด๋ ˆ์ด์…˜]
ARTICLES(ID, TITLE, JOURNAL, ISSUE, YEAR, STARTPAGE, ENDPAGE, TR_ID)

[์ธ๋ฑ์Šค]
CREATE INDEX IDX1 ON ARTICLES(YEAR, STARTPAGE);
CREATE INDEX IDX2 ON ARTICLES(STARTPAGE, ENDPAGE);
CREATE INDEX IDX3 ON ARTICLES(JOURNAL, ISSUE, YEAR);
1)                             2)
SELECT TITLE FROM ARTICLES      SELECT TITLE FROM ARTICLES
WHERE JOURNAL = 'JACM' AND      WHERE ENDPAGE - STARTPAGE > 50;
ISSUE = 55;

3)                             4)
SELECT TITLE FROM ARTICLES      SELECT TITLE FROM ARTICLES
WHERE YEAR > 1995 AND           WHERE JOURNAL = 'JACM';
YEAR < 2000;

 

๋ฌธ์ œ 29. ๋‹ค์Œ์€ ๋ถ„์‚ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€ํ•œ ์„ค๋ช…์ด๋‹ค. ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒƒ์€?  ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

1) ๋ถ„์‚ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ๋„คํŠธ์›Œํฌ๋ฅผ ๊ฒฝ์œ ํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ๋ถ„๋ฆฌ๋˜์–ด ์žˆ๋‹ค.

2) ๋ถ„์‚ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ์‹œ์Šคํ…œ ๊ฐ€์šฉ์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.

3) ๋ถ„์‚ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋ฅผ ๋ณ‘๋ ฌ์ ์œผ๋กœ ์‹คํ–‰ํ•˜์—ฌ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚จ๋‹ค.

4) ์‚ฌ์šฉ์ž๋Š” ๋ถ„์‚ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

๋ฌธ์ œ 30. ์•„๋ž˜์˜ ๋ณด๊ธฐ๊ฐ€ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๋งž์€ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

[๋ณด๊ธฐ]
- SQL์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์‹คํ–‰๋  ๋•Œ ์‹คํ–‰ ์ ˆ์ฐจ ๋ฐ
  ๋ฐฉ๋ฒ•์„ ํ‘œํ˜„ํ•˜์—ฌ DBA์—๊ฒŒ ์•Œ๋ ค์ค€๋‹ค.
- ์˜ตํ‹ฐ๋งˆ์ด์ €์˜ ์ข…๋ฅ˜๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” RULE, COST๊ฐ€ ํ‘œํ˜„๋˜๊ณ 
  SQL์ด ๋‚ด๋ถ€์ ์œผ๋กœ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์‹คํ–‰๋˜์—ˆ๋Š”์ง€ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.
1) ์‹คํ–‰๊ณ„ํš

2) ๋‚ด๋ถ€๊ณ„ํš

3) ์ ˆ์ฐจ๊ณ„ํš

4) ํ‘œํ˜„๊ณ„ํš

 

๋ฌธ์ œ 31. ๋‹ค์Œ ๋ณด๊ธฐ ์ค‘ ์ธ๋ฑ์Šค์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

1) ์ธ๋ฑ์Šค๋Š” ์ˆœ์ฐจ์ธ๋ฑ์Šค, ๊ฒฐํ•ฉ์ธ๋ฑ์Šค, ๋น„ํŠธ๋งต, ํด๋Ÿฌ์Šคํ„ฐ, ํ•ด์‹œ ์ธ๋ฑ์Šค๊ฐ€ ์žˆ๋‹ค.

2) VARCHAR, CHAR, DATE, NUMBER ๋ชจ๋‘ ์ธ๋ฑ์Šค ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

3) ํŒŒํ‹ฐ์…˜ ํ…Œ์ด๋ธ”์€ ํŒŒํ‹ฐ์…˜ ํ‚ค์— ๋Œ€ํ•ด์„œ ์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋‹ค.

4) ์ธ๋ฑ์Šค์˜ ์ˆ˜๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉด ์ž…๋ ฅ๊ณผ ์‚ญ์ œ, ์ˆ˜์ • ์†๋„๊ฐ€ ์ €ํ•˜๋  ์ˆ˜ ์žˆ๋‹ค.

 

๋ฌธ์ œ 32. ๋‹ค์Œ ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์•„๋ž˜์˜ SQL๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜์˜€์„ ๋•Œ์˜ ๊ฒฐ๊ณผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

[TEST 32]
COL1    COL2
-------------
NULL     10
 12     NULL
NULL    NULL
 10      12
 
 [SQL]
 SELECT CASE WHEN SUM(COL1 + COL2)
 IS NULL THEN 0
 ELSE SUM(COL1 + COL2)
 END AS ํ•ฉ๊ณ„
 FROM TEST32;
1) NULL

2) 12

3) 22

4) 25

 

๋ฌธ์ œ 33. ๋‹ค์Œ ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์ˆ˜ํ–‰ํ•˜์˜€์„๋•Œ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ์ž˜๋ชป๋œ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

[TEST33]
EMPNO  ENAME    SAL
--------------------
1      ์œ ๋น„      1000
2      ์กฐ์กฐ      2000
3      ๊ด€์šฐ      3000
4      ์—ฌํฌ      4000
5      ์ดˆ์„       5000
6     ์กฐ์ž๋ฃก     6000
1)                             2)
SELECT ENAME, SAL               SELECT ENAME, SAL
FROM (SELECT ENAME, SAL FROM    FROM (SELECT * FROM TEST33 ORDER
TEST33 ORDER BY SAL DESC)       BY SAL DESC)
WHERE ROWNUM = 1;               WHERE ROWNUM = 2;
โ†’ SAL์€ 6000์ด ์กฐํšŒ๋œ๋‹ค.           โ†’ ๋์—์„œ 2๊ฑด์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”์ถœ๋œ๋‹ค.

3)                             4)
SELECT ENAME, SAL               SELECT ENAME, SAL
FROM (SELECT * FROM TEST33      FROM (SELECT * FROM TEST33 ORDER
ORDER BY SAL DESC)              BY SAL DESC)
WHERE ROWNUM > 0;               WHERE ROWNUM <= 3;
โ†’ ์ด 6๊ฐœ์˜ ํ–‰์ด ์ถœ๋ ฅ๋œ๋‹ค.            โ†’ 3๊ฐœ์˜ ํ–‰์ด ์ถœ๋ ฅ๋œ๋‹ค.

 

๋ฌธ์ œ 34. ๋ณด๊ธฐ์˜ ์—ฐ์‚ฐ์ž ์ค‘ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋‚˜์ค‘์ธ ๊ฒƒ์€?  ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

1) ์—ฐ๊ฒฐ ์—ฐ์‚ฐ์ž

2) ๋น„๊ต ์—ฐ์‚ฐ์ž

3) NOT ์—ฐ์‚ฐ์ž

4) OR ์—ฐ์‚ฐ์ž

 

๋ฌธ์ œ 35. ๋‹ค์Œ ๋ณด๊ธฐ ์ค‘ SELF JOIN์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋กœ ๊ฐ€์žฅ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

1) ๋™์ผํ•œ ํ…Œ์ด๋ธ” ๋‚ด์—์„œ ๋‘ ๊ฐœ์˜ ์นผ๋Ÿผ ๊ฐ„์— ์กฐ์ธ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

2) ๋„คํŠธ์›Œํฌ๋กœ ๋ถ„์‚ฐ๋œ ์‹œ์Šคํ…œ์—์„œ ๊ฐ™์€ 2๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋œ๋‹ค.

3) ์˜จ๋ผ์ธ ์‡ผํ•‘๋ชฐ์—์„œ ์ฃผ๋ฌธ์ •๋ณด๋Š” ์ฃผ๋ฌธ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•ด์„œ SELF JOIN์„ ํ•œ๋‹ค.

4) ํ•œ ํ…Œ์ด๋ธ” ๋‚ด์—์„œ ์„œ๋กœ ์—ฐ๊ด€๋œ ์นผ๋Ÿผ์ด ์—†์„ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

 

๋ฌธ์ œ 36. ๋‹ค์Œ ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”๋“ค์— ๋Œ€ํ•ด์„œ ์•„๋ž˜์˜ SQL๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜์˜€์„๋•Œ ๊ฒฐ๊ณผ์˜ ํ–‰์ˆ˜๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€?  

์ •๋‹ตํ™•์ธ๐ŸŒผ

[TEST36_1]
EMPNO     ENAME
---------------
1000       ์กฐ์กฐ
2000       ๊ด€์šฐ
3000       ์กฐํ›ˆ

[TEST36_2]
NO      CONDITION
-----------------
1         ์กฐ%
2        %์šฐ%

[SQL]
SELECT COUNT(*) ROWCNT
FROM TEST36_1 A, TEST36_2 B
WHERE A.ENAME LIKE B.CONDITION;
1) 0

2) 3

3) 4

4) 6

 

๋ฌธ์ œ 37. ABC๊ธฐ์—…์˜ TEST37ํ…Œ์ด๋ธ”์—๋Š” COL1๋ฒˆ๊ณผ COL2๋ฒˆ์˜ ์นผ๋Ÿผ์ด ์žˆ๋‹ค. ์ด ๋•Œ ์•„๋ž˜์˜ SQL๋ฌธ์„ ์‹คํ–‰ํ•  ๊ฒฝ์šฐ ๊ฑด ์ˆ˜๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

INSERT INTO TEST37 VALUES(NULL, 10);
INSERT INTO TEST37 VALUES(12, NULL);
INSERT INTO TEST37 VALUES(NULL, NULL);
INSERT INTO TEST37 VALUES(10, 12);

SQL1)
SELECT COUNT(COL1) FROM TEST37; -- ์ถœ๋ ฅ๊ฐ’

SQL2)
SELECT*FROM TEST37 WHERE COL1 IN(12, 10, NULL); -- ํ–‰์˜์ˆ˜

SQL3)
SELECT COL1, COUNT(*) FROM TEST37 GROUP BY COL1; -- ํ–‰์˜์ˆ˜
1) 2, 3, 4

2) 2, 1, 3

3) 2, 2, 3

4) 4, 2, 3

 

๋ฌธ์ œ 38. ๋‹ค์Œ์˜ A, Bํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋ณด๊ณ  ์•Œ๋งž์€ ๊ฒƒ์„ ๊ณ ๋ฅด์‹œ์˜ค.  ์ •๋‹ตํ™•์ธ๐ŸŒผ

CREATE TABLE A(
A NUMBER(10) PRIMARY KEY,
B NUMBER(10)
);

CREATE TABLE B(
A NUMBER(10),
B NUMBER(10) 
REFERENCES A(A) ON DELETE CASCADE);

INSERT INTO A VALUES(1,1);
INSERT INTO A VALUES(2,2);

INSERT INTO B VALUES(1,1);
INSERT INTO B VALUES(2,2);

DELETE FROM A WHERE A=1;
SELECT*FROM B;
1)        2)
A   B     A   B
-----     -----
2   2     1   1
          1   1
          
3)        4)
A   B     A   B
-----     -----
2   2     1   1
1   1

 

๋ฌธ์ œ 39. ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์•„๋ž˜์˜ SQL๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜์˜€์„๋•Œ ๊ฒฐ๊ณผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

[TEST39]
COL1    COL2
------------
์กฐ์กฐ      1
์œ ๋น„      2
์œ ๋น„      3
๊ด€์šฐ      4
๊ด€์šฐ      5
๊ด€์šฐ      6
์—ฌํฌ      7
์ดˆ์„       8

[SQL]
SELECT COUNT(*) FROM TEST39
GROUP BY COL1
HAVING COUNT(*) > 2;
1) NULL

2) 3

3) 5

4) 6

 

๋ฌธ์ œ 40. ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”์—์„œ ํ•ด๋‹น SQL๋ฌธ์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€?  ์ •๋‹ตํ™•์ธ๐ŸŒผ

[TEST40]
COL1     COL2
--------------
์กฐ์กฐ       1
์กฐ์กฐ       1
์กฐ์กฐ       1
์กฐ์กฐ       2
์กฐ์กฐ       3

[SQL]
SELECT COUNT(COL1), COUNT(COL2)
FROM(SELECT DISTINCT COL1, COL2
     FROM TEST40);
1) 1, 2

2) 2, 1

3) 2, 2

4) 3, 3

 

๋ฌธ์ œ 41. ๋‹ค์Œ ๋ณด๊ธฐ ์ค‘ NUMERIC(์ˆซ์ž)ํ˜•์ด ์•„๋‹Œ ๊ฒƒ์€? ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

1) INT

2) CHAR

3) FLOAT

4) DECIMAL

 

๋ฌธ์ œ 42. ์•„๋ž˜์˜ SQL๋ฌธ์— ๋Œ€ํ•ด์„œ ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋‚˜์—ดํ•œ ๊ฒƒ์€?  ์ •๋‹ตํ™•์ธ๐ŸŒผ

SELECT DEPTNO, COUNT(EMPNO)
FROM SCOTT.EMP
WHERE SAL >= 400
GORUP BY DEPTNO
HAVING COUNT(EMPNO) >= 3
ORDER BY DEPTNO;
1) FROM โ†’ WHERE โ†’ GROUP BY โ†’ HAVING โ†’ ORDER BY โ†’ SELECT

2) FROM โ†’ WHERE โ†’ HAVING โ†’ GROUP BY โ†’ ORDER BY โ†’ SELECT

3) FROM โ†’ WHERE โ†’ GROUP BY โ†’ SELECT โ†’ HAVING โ†’ ORDER BY

4) FROM โ†’ WHERE โ†’ GROUP BY โ†’ HAVING โ†’ SELECT โ†’ ORDER BY

 

๋ฌธ์ œ 43. ๋‹ค์Œ ๋ณด๊ธฐ ์ค‘ ์ˆœ์ˆ˜ ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž์— ํ•ด๋‹นํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์€?  ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

1) SELECT

2) DELETE

3) JOIN

4) DIVISION

 

- - - - - - - - ์ฃผ๊ด€์‹ - - - - - - - -

๋ฌธ์ œ 44. ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜ํ™˜๋˜๋„๋ก ์ฃผ์–ด์ง„ SQL๋ฌธ์˜ ๋นˆ์นธ์„ ์™„์„ฑํ•˜์‹œ์˜ค.์ •๋‹ตํ™•์ธ๐ŸŒผ

[TEST44]
NAME  DEPTNAME  POSITION  SAL
------------------------------
์กฐ์กฐ     ITํŒ€       ๋ถ€์žฅ    5000
์—ฌํฌ     ITํŒ€       ๋Œ€๋ฆฌ    3000
์œ ๋น„     ๋ณด์•ˆํŒ€      ์ฐจ์žฅ    4000
๊ด€์šฐ     ๋ณด์•ˆํŒ€      ์‚ฌ์›    2000
์žฅ๋น„     ์ด๋ฌดํŒ€      ๋ถ€์žฅ    5000
๋™ํƒ     ์ธ์‚ฌํŒ€      ์ฐจ์žฅ    4000

[SQL]
SELECT
(     ) OVER(ORDER BY SAL DESC)
AS RANK, NAME, DEPTNAME, POSITION, SAL
FROM TEST44;

[RESULT]
RANK  NAME  DEPTNAME  POSITION  SAL
-----------------------------------
1     ์กฐ์กฐ    ITํŒ€       ๋ถ€์žฅ    5000
2     ์žฅ๋น„    ์ด๋ฌดํŒ€      ๋ถ€์žฅ    5000
3     ๋™ํƒ    ์ธ์‚ฌํŒ€      ์ฐจ์žฅ    4000
4     ์œ ๋น„    ๋ณด์•ˆํŒ€      ์ฐจ์žฅ    4000
5     ์—ฌํฌ    ITํŒ€       ๋Œ€๋ฆฌ    3000
6     ๊ด€์šฐ    ๋ณด์•ˆํŒ€      ์‚ฌ์›    2000

โœ๏ธ

 

๋ฌธ์ œ 45. ๋‹ค์Œ์€ ORACLE ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์‚ฌ์šฉํ•˜๋Š” SQL๋ฌธ๊ณผ SQL server ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ SQL์˜ ์ฐจ์ด์ ์— ๋Œ€ํ•œ ๋ฌธ์ œ์ด๋‹ค. (    )์— ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์€?

[ORACLE]
CREATE TABLE TEAM_EMP
AS SELECT * FROM EMP;

[SQL server]
SELECT * (     ) FROM EMP;

โœ๏ธ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

๋ฌธ์ œ 46. ์ฃผ์–ด์ง„ ๋ณด๊ธฐ์˜ SQL1์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ์™€ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก SQL2์˜ (       )์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

[SQL1]
SELECT COL1, COL2, COUNT(*)
FROM TEST46
GROUP BY ROLLUP(COL1, COL2);

[SQL2]
SELECT COL1, COL2, COUNT(*)
FROM TEST46
GROUP BY GROUPING SETS(     );

โœ๏ธ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

๋ฌธ์ œ 47. ๋‹ค์Œ์˜ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ SQL๋ฌธ์˜ ์ตœ์ข…๊ฒฐ๊ณผ๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์ •๋‹ตํ™•์ธ๐ŸŒผ

[PRODUCT]
CREATE TABLE PRODUCT(
COL1 VARCHAR(20),
COL2 NUMBER(10));

INSERT INTO PRODUCT VALUES('1',1000);
INSERT INTO PRODUCT VALUES('2',2000);
INSERT INTO PRODUCT VALUES('3',3000);
INSERT INTO PRODUCT VALUES('4',4000);
INSERT INTO PRODUCT VALUES('5',5000);

[SQL]
INSERT INTO PRODUCT VALUES('6',6000);
COMMIT;
DELETE PRODUCT WHERE COL1 = '2';
UPDATE PRODUCT SET COL2 = 9000 WHERE COL2 = 1000;
ROLLBACK;
SELECT COUNT(COL1) FROM PRODUCT WHERE COL2 = 2000;

โœ๏ธ

 

๋ฌธ์ œ 48. ๋‹ค์Œ์˜ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ SQL๋ฌธ์˜ ์ตœ์ข…๊ฒฐ๊ณผ๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค.

[TEST48]
COL1     COL2
-----------------
1         10
2         20
3        NULL
4         40
5         50

[SQL]
SELECT AVG(NVL(COL2,0)) FROM TEST48;

โœ๏ธ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

๋ฌธ์ œ 49. ๋‹ค์Œ์˜ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ SQL๋ฌธ์˜ ์ตœ์ข…๊ฒฐ๊ณผ๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค.

[TEST49_1]
COL1  COL2  
-----------
1      1    
1      2    
2      1   
3      1    
3      2    

[TEST49_2]
COL1  COL2 
-----------
1      1    
1      2    
2      1   
3      1   
3      2   

[SQL]
SELECT COUNT(*)
FROM TEST49_1 A, TEST49_2 B
WHERE A.COL1 <> B.COL1;

โœ๏ธ์ •๋‹ตํ™•์ธ๐ŸŒผ

 

๋ฌธ์ œ 50. TEST50 ํ…Œ์ด๋ธ”์—๋Š” ์ด 5๊ฑด์˜ ํ–‰์ด ์žˆ๋‹ค. ๋‹ค์Œ ๋นˆ์นธ์— ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

[SQL]
SELECT COUNT(*) FROM TEST50
(      ) TEST50;

[RESULT]
  COUNT(*)
----------
	25

1row selected

โœ๏ธ์ •๋‹ตํ™•์ธ๐ŸŒผ


๐Ÿ“SQLD. ์ œ 38 ํšŒ ๊ธฐ์ถœ ๋ฌธ์ œ(์ด 50) - ์ •๋‹ต / ํ•ด์„ค

 

1. ์ •๋‹ต :  4

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ์ƒ๊ธ‰์ข…ํ•ฉ๋ณ‘์›์—๋Š” ํ•œ ๋ช…์˜ ํ˜น์€ ์—ฌ๋Ÿฌ ๋ช…์˜ ์˜์‚ฌ๊ฐ€ ๊ทผ๋ฌดํ•˜๊ณ  ๋ชจ๋ธ๋ง์œผ๋กœ๋Š” ์˜์‚ฌ๊ฐ€ ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค.

์ง„๋ฃŒ๋Š” ์˜์‚ฌ๋งŒ ํ•  ์ˆ˜ ์žˆ๊ณ  ์˜์‚ฌ๋Š” ์ง„๋ฃŒ๋ฅผ ํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค.

๋‘ ์—”ํ„ฐํ‹ฐ๊ฐ„ ๊ด€๊ณ„์—์„œ ์ˆ˜ํ–‰๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ (๊ด€๊ณ„์ฐจ์ˆ˜)

2. ์ •๋‹ต :  1

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : 

์„ฑ๋Šฅ ๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง ๊ณ ๋ ค์‚ฌํ•ญ

1) ์ •๊ทœํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ชจ๋ธ์˜ ์œ ์—ฐ์„ฑ์„ ํ™•๋ณดํ•œ๋‹ค.

2) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ „์ฒด ์šฉ๋Ÿ‰, ์›”๊ฐ„, ์—ฐ๊ฐ„ ์ฆ๊ฐ์œจ์„ ์˜ˆ์ธกํ•œ๋‹ค.

3) ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํŠธ๋žœ์žญ์…˜์˜ ์œ ํ˜•(CRUD: Create Read Update Delete)์„ ํŒŒ์•…ํ•œ๋‹ค.

4) ํ•ฉ๊ณ„ ๋ฐ ์ •์‚ฐ ๋“ฑ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ˜์ •๊ทœํ™”๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.(์„ฑ๋Šฅํ–ฅ์ƒ์„ ์œ„ํ•œ ํŠœ๋‹)

5) ๊ธฐ๋ณธํ‚ค์™€ ์™ธ๋ž˜ํ‚ค, ์ˆ˜ํผํƒ€์ž…๊ณผ ์„œ๋ธŒํƒ€์ž… ๋“ฑ์„ ์กฐ์ •ํ•œ๋‹ค.

6) ์„ฑ๋Šฅ๊ด€์ ์—์„œ ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์„ ๊ฒ€์ฆํ•˜๊ณ  ํ™•์ธํ•œ๋‹ค.

 

[ 3๊ฐ€์ง€ ๋ชจ๋ธ๋ง ์•Œ์•„๋‘๊ธฐ ]

  • ๊ฐœ๋…์  ๋ชจ๋ธ๋ง: ๊ฐœ์ฒด์™€ ๊ฐœ์ฒด๋“ค ๊ฐ„์˜ ๊ด€๊ณ„์—์„œ ER๋‹ค์ด์–ด๊ทธ๋žจ์„ ๋งŒ๋“œ๋Š” ๊ณผ์ •
  • ๋…ผ๋ฆฌ์  ๋ชจ๋ธ๋ง: ER๋‹ค์ด์–ด๊ทธ๋žจ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ด€๊ณ„ ์Šคํ‚ค๋งˆ ๋ชจ๋ธ์„ ๋งŒ๋“œ๋Š” ๊ณผ์ •
  • ๋ฌผ๋ฆฌ์  ๋ชจ๋ธ๋ง: ๊ด€๊ณ„ ์Šคํ‚ค๋งˆ ๋ชจ๋ธ์˜ ๋ฌผ๋ฆฌ์  ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๊ณ  ๊ตฌํ˜„ํ•˜๋Š” ๊ณผ์ •

 

3. ์ •๋‹ต :  3

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ๋…ผ๋ฆฌ์  ๋ชจ๋ธ์€ ๋น„์ฆˆ๋‹ˆ์Šค ์ •๋ณด์˜ ๋…ผ๋ฆฌ์  ๊ตฌ์กฐ ๋ฐ ๊ตฌ์ถ•์„ ํŒŒ์•…ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

์ฆ‰, ํ•ต์‹ฌ ์—”ํ„ฐํ‹ฐ์™€ ํ‚ค ์—”ํ„ฐํ‹ฐ๋“ฑ์„ ์‹๋ณ„ํ•˜๊ณ  ๋ชจ๋ธ๋งํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ตฌ์กฐ๋ฅผ ๋ชจ๋ธ๋งํ•œ๋‹ค.

๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง ๊ณผ์ • by yunamom

 

 

4. ์ •๋‹ต :  3

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ์ด์šฉ๋‚ด์—ญ ์—”ํ„ฐํ‹ฐ์—์„œ ์ด์šฉ์ผ์ž + ์‚ฌ์›๋ฒˆํ˜ธ๊ฐ€ ๊ธฐ๋ณธํ‚ค(PK) ์ด๋ฏ€๋กœ ์ผ์ž๊ฐ€ ๊ฐ™์€ ๋‚ ์— ์—ฌ๋Ÿฌ ์ฝ˜๋„๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์—†๋‹ค.

 

์‚ฌ์›, ์ด์šฉ ๋‚ด์—ญ( (1 : M) โ†’ 1๋ช…์˜ ์‚ฌ์›์€ ์ด์šฉ๋‚ด์—ญ์ด ์žˆ์„์ˆ˜๋„ ์žˆ๊ณ  ์—†์„์ˆ˜๋„ ์žˆ๋‹ค.)

์ฝ˜๋„์ด์šฉ์ •๋ณด, ์ด์šฉ ๋‚ด์—ญ( (1 : 0 or 1 : 1 ) : M โ†’ ์ฝ˜๋„ ์ด์šฉ์ •๋ณด๊ฐ€ ์žˆ์„์ˆ˜๋„ ์žˆ๊ณ  ์—†์„์ˆ˜๋„ ์žˆ์œผ๋ฉฐ ์ฝ˜๋„ ์ด์šฉ์ •๋ณด๊ฐ€ ์ด์šฉ๋‚ด์—ญ์— ์žˆ์„์ˆ˜๋„ ์žˆ๊ณ  ์—†์„์ˆ˜๋„ ์žˆ๋‹ค.)

 

5. ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ์ •๊ทœํ™”(Normalization)๋Š” ํ•จ์ˆ˜์  ์ข…์†์„ฑ์— ๋”ฐ๋ผ์„œ ํ…Œ์ด๋ธ”์„ ๋ถ„ํ•ดํ•˜๋Š” ๊ณผ์ •์œผ๋กœ ๋ฐ์ดํ„ฐ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•ด์„œ ๋ชจ๋ธ์˜ ๋…๋ฆฝ์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚จ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ •๊ทœํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฐœ์ƒ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๊ฐฑ์‹ ์ด์ƒ (์ด์ƒํ˜„์ƒ(Anoma-ly) ์‚ฝ์ž…, ์‚ญ์ œ, ์ˆ˜์ • ์ด์ƒํ˜„์ƒ์ด ์žˆ๋‹ค.) ์ด๊ณ ,

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ณด์•ˆ๊ณผ ๊ด€๋ จ์ด ์žˆ๋Š” ๊ฒƒ์€ ๋ทฐ(View) ์ด๋‹ค.

 

6. ์ •๋‹ต :  1

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ๊ธฐ๋ณธํ‚ค(PK)๋Š” ์—”ํ„ฐํ‹ฐ๋ฅผ ๋Œ€ํ‘œํ•˜๋Š” ํ‚ค๋กœ ์ตœ์†Œ์„ฑ(Not Null)๊ณผ ์œ ์ผ์„ฑ(์ค‘๋ณต ์—†์Œ)์„ ๋งŒ์กฑํ•ด์•ผ ํ•œ๋‹ค.

์™ธ๋ž˜ํ‚ค(FK)๋Š” ๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ” ๊ฐ„์— ์—ฐ๊ฒฐ์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ Key์ด๋‹ค. ํ•œ ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธํ‚ค๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์˜ ์นผ๋Ÿผ์ด๋‹ค.

 

ํŒŒ์ƒ์†์„ฑ โ†’ ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ๋ฐ์ดํ„ฐ, ์˜ˆ) ์ฃผ์‹์˜ ๊ฐ€๊ฒฉ์˜ ํ‰๊ท ๋ฐ์ดํ„ฐ ๋“ฑ 

 

7. ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ์—”ํ„ฐํ‹ฐ๋Š” ๊ด€๊ณ„๋ฅผ 2๊ฐœ ์ด์ƒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

์—”ํ„ฐํ‹ฐ ํŠน์ง• ์„ค๋ช…
์‹๋ณ„์ž - ์—”ํ„ฐํ‹ฐ๋Š” ์œ ์ผํ•œ ์‹๋ณ„์ž๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
์˜ˆ) ๋ฅผ ๋“ค์–ด ํšŒ์›ID, ๊ณ„์ขŒ๋ฒˆํ˜ธ
์ธ์Šคํ„ด์Šค ์ง‘ํ•ฉ - 2๊ฐœ ์ด์ƒ์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
์˜ˆ) ๊ณ ๊ฐ์ •๋ณด๋Š” 2๋ช…์ด์ƒ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
์†์„ฑ - ์—”ํ„ฐํ‹ฐ๋Š” ๋ฐ˜๋“œ์‹œ ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
๊ด€๊ณ„ - ์—”ํ„ฐํ‹ฐ๋Š” ๋‹ค๋ฅธ ์—”ํ„ฐํ‹ฐ์™€ ์ตœ์†Œ ํ•œ ๊ฐœ ์ด์ƒ ๊ด€๊ณ„๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
- ์—”ํ„ฐํ‹ฐ์˜ ๊ด€๊ณ„ โ†’ ์ง‘ํ•ฉ๊ณผ ์ง‘ํ•ฉ๊ฐ„์˜ ๊ด€๊ณ„
์˜ˆ) ๊ณ ๊ฐ ์—”ํ„ฐํ‹ฐ์—์„œ ๊ณ ๊ฐ ๊ณ„์ขŒ๊ฐœ์„ค, ๊ณ ๊ฐ ํšŒ์›๋“ฑ๊ธ‰ ๋ถ€์—ฌ, ๊ณ ๊ฐ ๋ฐฐ์†ก์ง€ ์ฃผ์†Œ ๋“ฑ๋“ฑ
์—…๋ฌด - ์—”ํ„ฐํ‹ฐ๋Š” ์—…๋ฌด์—์„œ ๊ด€๋ฆฌ๋˜์–ด์•ผ ํ•˜๋Š” ์ง‘ํ•ฉ์ด๋‹ค.
์˜ˆ) ๊ณ ๊ฐ, ๊ณ„์ขŒ

 ์—”ํ„ฐํ‹ฐ์˜ ์ข…๋ฅ˜ 

์ข…๋ฅ˜ ์„ค๋ช…
๋…๋ฆฝ ์—”ํ‹ฐํ‹ฐ
[Kernel Entity, Master Entity]
์‚ฌ๋žŒ, ๋ฌผ๊ฑด, ์žฅ์†Œ ๋“ฑ๊ณผ ๊ฐ™์ด ํ˜„์‹ค์„ธ๊ณ„์— ์กด์žฌํ•˜๋Š” ์—”ํ„ฐํ‹ฐ
์—…๋ฌด์ค‘์‹ฌ ์—”ํ„ฐํ‹ฐ
[Transaction Entity]
Transaction์ด ์‹คํ–‰๋˜๋ฉด์„œ ๋ฐœ์ƒํ•˜๋Š” ์—”ํ„ฐํ‹ฐ
์ข…์† ์—”ํ„ฐํ‹ฐ
[Dependent Entity]
์ฃผ๋กœ 1์ฐจ ์ •๊ทœํ™”๋กœ ์ธํ•ด ๊ด€๋ จ ์ค‘์‹ฌ์—”ํ‹ฐํ‹ฐ๋กœ๋ถ€ํ„ฐ ๋ถ„๋ฆฌ๋œ ์—”ํ„ฐํ‹ฐ
๊ต์ฐจ ์—”ํ‹ฐํ‹ฐ
[Intersaction Entity]
M:M์˜ ๊ด€๊ณ„๋ฅผ ํ•ด์†Œํ•˜๋ ค๋Š” ๋ชฉ์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ์—”ํ„ฐํ‹ฐ [ex> M:M -> 1:M]

1. ์œ ํ˜•๊ณผ ๋ฌดํ˜•์— ๋”ฐ๋ฅธ ์—”ํ„ฐํ‹ฐ ์ข…๋ฅ˜

     * ์œ ํ˜•๊ณผ ๋ฌดํ˜•์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ธฐ์ค€์€ ๋ฌผ๋ฆฌ์  ํ˜•ํƒœ์˜ ์กด์žฌ ์—ฌ๋ถ€

์ข…    ๋ฅ˜ ์„ค    ๋ช…
์œ ํ˜• ์—”ํ„ฐํ‹ฐ ์—…๋ฌด์—์„œ ๋„์ถœ๋˜๋ฉฐ ์ง€์†์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” Entity
๊ฐœ๋… ์—”ํ„ฐํ‹ฐ ๊ฐœ๋…์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” Entity
์œ ํ˜• ์—”ํ„ฐํ‹ฐ๋Š” ๋ฌผ๋ฆฌ์  ํ˜•ํƒœ๊ฐ€ ์žˆ์ง€๋งŒ, ๊ฐœ๋… ์—”ํ„ฐ๋‹ˆ๋Š” ๋ฌผ๋ฆฌ์  ํ˜•ํƒœ๊ฐ€ ์—†๋‹ค
์‚ฌ๊ฑด ์—”ํ„ฐํ‹ฐ ๋น„์ฆˆ๋‹ˆ์Šค ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ ์ƒ์„ฑ๋˜๋Š” Entity

 2. ๋ฐœ์ƒ์‹œ์ ์— ๋”ฐ๋ฅธ ์—”ํ„ฐํ‹ฐ ์ข…๋ฅ˜

์ข…    ๋ฅ˜ ์„ค    ๋ช…
๊ธฐ๋ณธ ์—”ํ„ฐํ‹ฐ
[Basic Entity]
๋‹ค๋ฅธ ์—”ํ„ฐํ‹ฐ๋กœ๋ถ€ํ„ฐ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๊ณ  ๋…๋ฆฝ์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ์—”ํ„ฐํ‹ฐ
ํ‚ค ์—”ํ„ฐํ‹ฐ[Key Entity]๋ผ๊ณ ๋„ ํ•จ
์ค‘์‹ฌ ์—”ํ„ฐํ‹ฐ
[Main Entity]
๊ธฐ๋ณธ ์—”ํ„ฐํ‹ฐ์™€ ํ–‰์œ„ ์—”ํ„ฐํ‹ฐ ๊ฐ„์˜ ์ค‘๊ฐ„์— ์žˆ๋Š” ์—”ํ„ฐํ‹ฐ
๊ธฐ๋ณธ ์—”ํ„ฐํ‹ฐ๋กœ๋ถ€ํ„ฐ ๋ฐœ์ƒ๋˜๊ณ  ํ–‰์œ„ ์—”ํ„ฐํ‹ฐ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์—”ํ„ฐํ‹ฐ
ํ–‰์œ„ ์—”ํ„ฐํ‹ฐ
[Active Entity]
2๊ฐœ ์ด์ƒ์˜ ์—”ํ„ฐํ‹ฐ๋กœ๋ถ€ํ„ฐ ๋ฐœ์ƒํ•˜๋Š” ์—”ํ„ฐํ‹ฐ
์ง€์†์ ์œผ๋กœ ์ •๋ณด๊ฐ€ ์ถ”๊ฐ€๋˜๊ณ  ๋ณ€๊ฒฝ๋˜๋Š” ์—”ํ„ฐํ‹ฐ

 

8. ์ •๋‹ต :  1

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ์ƒํ’ˆ์€ ์ฃผ๋ฌธ์„ ํ•œ ๊ฐœ ์ด์ƒ ๋ฐ˜๋“œ์‹œ ๊ฐ€์ ธ์•ผ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์•ˆ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ๋‹ค.

ํ‘œ๊ธฐ๋ฒ• ๋ฌธ์ œ๋ฅผ ๋ณผ๋•Œ O ๋™๊ทธ๋ผ๋ฏธ๋ฅผ ์œ ์˜ํ•ด์„œ ๋ณด๋ฉด ๋ฌธ์ œ๋ฅผ ์‰ฝ๊ฒŒ ํ’€์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค.

O = or (์žˆ์„์ˆ˜๋„ ์žˆ๊ณ  or ์—†์„์ˆ˜๋„ ์žˆ๋‹ค.)

 

9. ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ERD ํ‘œ๊ธฐ๋ฒ• ์ค‘ IE ํ‘œ๊ธฐ๋ฒ•์€ ๊ด€๊ณ„์˜ 1:N ๊ด€๊ณ„์—์„œ N์ชฝ์— ์ƒˆ๋ฐœ์„ ํ‘œ์‹œํ•˜๊ณ  ์„ ํƒ,

ํ•„์ˆ˜ ์ฐธ์—ฌ๊ด€๊ณ„์—์„œ ์„ ํƒ ์ฐธ์—ฌ(or)์— O, ํ•„์ˆ˜ ์ฐธ์—ฌ์— | ๋กœ ํ‘œ์‹œํ•œ๋‹ค.

 

10. ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ๊ฐ™์€ํ•™๋…„์€ ์ž…ํ•™ ๋‚ ์งœ๊ฐ€ ๋ชจ๋‘ ๊ฐ™๊ฒŒ ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋’ค์— ๊ฐ™์€ ๊ฐ’์ด ๋ถ™์Œ์œผ๋กœ์จ ์ตœ์†Œ์˜ ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ์ž˜๋ชป๋œ ๋ชจ๋ธ๋ง์ด๋‹ค.

์‹๋ณ„์ž์˜ ํŠน์ง•

  1. ์œ ์ผ์„ฑ : ์ฃผ์‹๋ณ„์ž์— ์˜ํ•ด ์—”ํ„ฐํ‹ฐ๋‚ด์— ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๋“ค์ด ์œ ์ผํ•˜๊ฒŒ ๊ตฌ๋ถ„๋˜์–ด์•ผ ํ•จ
  2. ์ตœ์†Œ์„ฑ : ์ฃผ์‹๋ณ„์ž๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์†์„ฑ์˜ ์ˆ˜๋Š” ์œ ์ผ์„ฑ์„ ๋งŒ์กฑํ•˜๋Š” ์ตœ์†Œ์˜ ์ˆ˜๊ฐ€ ๋˜์–ด์•ผ ํ•จ
  3. ๋ถˆ๋ณ€์„ฑ : ์ง€์ •๋œ ์ฃผ์‹๋ณ„์ž ๊ฐ’์€ ์ž์ฃผ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด์–ด์•ผ ํ•จ
  4. ์กด์žฌ์„ฑ : ์ฃผ์‹๋ณ„์ž๊ฐ€ ์ง€์ •์ด ๋˜๋ฉด ๋ฐ˜๋“œ์‹œ ๊ฐ’์ด ๋“ค์–ด์™€์•ผ ํ•จ(NOT NULL)
์‹๋ณ„์ž ๋ถ„๋ฅ˜ ์‹๋ณ„์ž ์„ค๋ช…
๋Œ€ํ‘œ์„ฑ์—ฌ๋ถ€

์ฃผ์‹๋ณ„์ž - ์—”ํ„ฐํ‹ฐ ๋‚ด์—์„œ ๊ฐ ํ–‰์„ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ๋ถ„์ž์ด๋ฉฐ, ํƒ€ ์—”ํ„ฐํ‹ฐ์™€ ์ฐธ์กฐ๊ด€๊ณ„๋ฅผ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ์‹๋ณ„์ž (ex. ์‚ฌ์›๋ฒˆํ˜ธ, ๊ณ ๊ฐ๋ฒˆํ˜ธ)
๋ณด์กฐ์‹๋ณ„์ž - ์—”ํ„ฐํ‹ฐ ๋‚ด์—์„œ ๊ฐ ํ–‰์„ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ๋ถ„์ž์ด๋‚˜ ๋Œ€ํ‘œ์„ฑ์„ ๊ฐ€์ง€์ง€ ๋ชปํ•ด ์ฐธ์กฐ๊ด€๊ณ„ ์—ฐ๊ฒฐ์„ ๋ชปํ•จ(ex. ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ)
์Šค์Šค๋กœ
์ƒ์„ฑ์—ฌ๋ถ€
๋‚ด๋ถ€์‹๋ณ„์ž - ์—”ํ„ฐํ‹ฐ ๋‚ด๋ถ€์—์„œ ์Šค์Šค๋กœ ๋งŒ๋“ค์–ด์ง€๋Š” ์‹๋ณ„์ž(ex. ๊ณ ๊ฐ๋ฒˆํ˜ธ)
์™ธ๋ถ€์‹๋ณ„์ž - ํƒ€ ์—”ํ„ฐํ‹ฐ์™€์˜ ๊ด€๊ณ„๋ฅผ ํ†ตํ•ด ํƒ€ ์—”ํ„ฐํ‹ฐ๋กœ๋ถ€ํ„ฐ ๋ฐ›์•„์˜ค๋Š” ์‹๋ณ„์ž(ex. ์ฃผ๋ฌธ์—”ํ„ฐํ‹ฐ์˜ ๊ณ ๊ฐ๋ฒˆํ˜ธ)
์†์„ฑ์˜ ์ˆ˜ ๋‹จ์ผ์‹๋ณ„์ž - ํ•˜๋‚˜์˜ ์†์„ฑ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์‹๋ณ„์ž(ex. ๊ณ ๊ฐ์—”ํ„ฐํ‹ฐ์˜ ๊ณ ๊ฐ๋ฒˆํ˜ธ )
๋ณตํ•ฉ์‹๋ณ„์ž - ๋‘˜ ์ด์ƒ์˜ ์†์„ฑ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์‹๋ณ„์ž(ex. ์ฃผ๋ฌธ์ƒ์„ธ์—”ํ„ฐํ‹ฐ์˜ ์ฃผ๋ฌธ๋ฒˆํ˜ธ+์ƒ์„ธ์ˆœ๋ฒˆ)
๋Œ€์ฒด์—ฌ๋ถ€ ๋ณธ์งˆ์‹๋ณ„์ž - ์—…๋ฌด(๋น„์ฆˆ๋‹ˆ์Šค)์— ์˜ํ•ด ๋งŒ๋“ค์–ด์ง€๋Š” ์‹๋ณ„์ž(ex. ๊ณ ๊ฐ๋ฒˆํ˜ธ)
์ธ์กฐ์‹๋ณ„์ž - ์—…๋ฌด์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ง€์ง€๋Š” ์•Š์ง€๋งŒ ์›์กฐ์‹๋ณ„์ž๊ฐ€ ๋ณต์žกํ•œ ๊ตฌ์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ธ์œ„์ ์œผ๋กœ ๋งŒ๋“  ์‹๋ณ„์ž(ex. ์ฃผ๋ฌธ์—”ํ„ฐํ‹ฐ์˜ ์ฃผ๋ฌธ๋ฒˆํ˜ธ(๊ณ ๊ฐ๋ฒˆํ˜ธ+์ฃผ๋ฌธ๋ฒˆํ˜ธ+์ˆœ๋ฒˆ))

 

11. ์ •๋‹ต :  4

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : Connect by ๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์„ ํƒํ•˜์—ฌ ๊ณ„์ธต์ ์ธ ์ˆœ์„œ ๊ทธ๋Œ€๋กœ ๋ฆฌํ„ดํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

ํ˜„์žฌ ํ–‰๊ณผ ๋‹ค๋ฅธ ํ–‰์€ Prior๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ตฌ๋ณ„๋œ๋‹ค. 

Prior๋Š” ์ƒ์œ„ ํ–‰์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋ฌธ์ œ์—์„œ๋Š” "๋ฐฉ๊ธˆ ์ „ ํ–‰์˜ COL2 ๊ฐ’์ด ํ˜„์žฌ ํ–‰์˜ COL1 ๊ฐ’์ธ ๋ชจ๋“  ํ–‰์„ ์ฐพ์•„๋ผ" ๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.

CONNECT BY COL1 = PRIOR COL2;

WHERE ์ ˆ์— COL3์ด 3์ธํ–‰์€ ์ œ์™ธ. โ†’ COL3 ์ด 4์ธ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘โ†’ CONNECT BY COL1 = PRIOR COL2 

COL1(D)  COL2(B)         COL3(4)     โ†’ 1๊ฐœ

 

COL1(B)  COL2(A)         COL3(2)     โ†’ COL2(B)์˜ ๊ฐ’๊ณผ ๊ฐ™์€ COL1(B) 1๊ฐœ

 

COL1(A)  COL2(NULL) COL3(1)      โ†’ COL2(A)์˜ ๊ฐ’๊ณผ ๊ฐ™์€ COL1(A) 1๊ฐœ

COL1 ์ค‘์— NULL ์€ ์—†์œผ๋ฏ€๋กœ ์ข…๋ฃŒ

1 + 1 + 1 = 3๊ฐœ

 

๐Ÿ’ก์•Œ์•„๋‘๊ธฐ

CONNECT BY PRIOR ์ž์‹ = ๋ถ€๋ชจ (๋ถ€๋ชจ โ†’ ์ž์‹) (์ˆœ โ†“ ๋ฐฉํ–ฅ)
CONNECT BY PRIOR ๋ถ€๋ชจ = ์ž์‹ (์ž์‹ โ†’ ๋ถ€๋ชจ) (์—ญ โ†‘ ๋ฐฉํ–ฅ)

CONNECT BY ์ž์‹ = PRIOR ๋ถ€๋ชจ (์ž์‹ โ†’ ๋ถ€๋ชจ) (์—ญ โ†‘ ๋ฐฉํ–ฅ)
CONNECT BY ๋ถ€๋ชจ = PRIOR ์ž์‹ (๋ถ€๋ชจ โ†’ ์ž์‹) (์ˆœ โ†“ ๋ฐฉํ–ฅ)
๊ตฌ๋ถ„ ์„ค๋ช…
START WITH ๊ณ„์ธต๊ตฌ์กฐ ์ „๊ฐœ์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ตฌ๋ฌธ, ๋ฃจํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์นญ
CONNECT BY ๋‹ค์Œ์— ์ „๊ฐœ๋  ์ž์‹ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •, ์ž์‹ ๋ฐ์ดํ„ฐ๋Š” CONNECT BY ์ ˆ์— ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•ด์•ผํ•จ(JOIN)
PRIOR CONNECT BY ์ ˆ์— ์‚ฌ์šฉ๋˜๋ฉฐ ํ˜„์žฌ ์ฝ์€ ์นผ๋Ÿผ์„ ์ง€์ •
PRIOR ์ž์‹ ๋ถ€๋ชจํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ณ„์ธต๊ตฌ์กฐ์—์„œ ๋ถ€๋ชจ -> ์ž์‹ ๋ฐฉํ–ฅ์œผ๋กœ ์ˆœ๋ฐฉํ–ฅ ์ „๊ฐœ
PRIOR ๋ถ€๋ชจ ์ž์‹ ํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ž์‹ -> ๋ถ€๋ชจ ๋ฐฉํ–ฅ์œผ๋กœ ์—ญ๋ฐฉํ–ฅ ์ „๊ฐœ
NOCYCLE ๋ฐ์ดํ„ฐ๋ฅผ ์ „๊ฐœํ•˜๋ฉด์„œ ์ด๋ฏธ ๋‚˜ํƒ€๋‚ฌ๋˜ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ „๊ฐœ์ค‘์— ๋‹ค์‹œ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒฝ์šฐ CYCLE์ด ์ƒ์„ฑ. CYCLE์ด ๋ฐœ์ƒํ•œ ๋ฐ์ดํ„ฐ๋Š” ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๋ฅผ ๋ฐฉ์ƒ์‹œ์ผœ NOCYCLE ๊ตฌ๋ฌธ์„ ํ†ตํ•ด CYCLE์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ ์ดํ›„ ๋ฐ์ดํ„ฐ ์ „๊ฐœ๋ฅผ ๋ฐฉ์ง€
ORDER SIBLINGS BY ํ˜•์ œ๋…ธ๋“œ(๋™์ผ LEVEL) ์‚ฌ์ด ๋ฐ์ดํ„ฐ ์ •๋ ฌ
WHERE ๋ชจ๋“  ์ „๊ฐœ๋ฅผ ์ˆ˜ํ–‰ํ•œ ๋’ค ์ง€์ •์กฐ๊ฑด์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ํ•„ํ„ฐ๋ง
๊ตฌ๋ถ„ ์„ค๋ช…
CONNECT_BY_ROOT ๊ณ„์ธตํ˜• ์ฟผ๋ฆฌ์—์„œ ์ตœ์ƒ์œ„ ๋กœ์šฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์—ฐ์‚ฐ์ž๋‹ค. ์—ฐ์‚ฐ์ž์ด๋ฏ€๋กœ CONNECT_BY_ROOT ๋‹ค์Œ์—๋Š” ํ‘œํ˜„์‹์ด ์˜จ๋‹ค.
CONNECT_BY_ISLEAF CONNECT BY ์กฐ๊ฑด์— ์ •์˜๋œ ๊ด€๊ณ„์— ๋”ฐ๋ผ ํ•ด๋‹น ๋กœ์šฐ๊ฐ€ ์ตœํ•˜์œ„ ์ž์‹ ๋กœ์šฐ์ด๋ฉด 1์„, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์˜์‚ฌ ์ปฌ๋Ÿผ์ด๋‹ค.

 

 

12. ์ •๋‹ต :  3

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ธํ•  ๋•Œ ํ›„ํ–‰ ํ…Œ์ด๋ธ”์— ์ธ๋ฑ์Šค๊ฐ€ ์—†์œผ๋ฉด Nested Loop์กฐ์ธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ๋œ๋‹ค. ๋ฌผ๋ก  ์˜ตํ‹ฐ๋งˆ์ด์ €๊ฐ€ ์ด๋Ÿฐ ๊ฒฝ์šฐ์— ์ž๋™์œผ๋กœ Nested Loop์กฐ์ธ์œผ๋กœ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  Hash์กฐ์ธ ํ˜น์€ Sort Merge, Full Scan์„ ์‚ฌ์šฉํ•œ๋‹ค.

๋ฐฉ ๋ฒ• ์„ค ๋ช…
์ค‘์ฒฉ ๋ฐ˜๋ณต ์กฐ์ธ
(Nested Loop Join)
- ์ข์€ ๋ฒ”์œ„์— ์œ ๋ฆฌ
- ์œ ๋ฆฌ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, Random Access ์œ„์ฃผ
- ํ›„ํ–‰(Driven) ํ…Œ์ด๋ธ”์—๋Š” ์กฐ์ธ์„ ์œ„ํ•œ ์ธ๋ฑ์Šค๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์žˆ์–ด์•ผ ํ•จ
- ์‹คํ–‰์†๋„ = ์„ ํ–‰ ํ…Œ์ด๋ธ” ์‚ฌ์ด์ฆˆ * ํ›„ํ–‰ ํ…Œ์ด๋ธ” ์ ‘๊ทผํšŸ์ˆ˜
์ƒ‰์ธ๋œ ์ค‘์ฒฉ ๋ฐ˜๋ณต ์กฐ์ธ, ๋‹จ์ผ ๋ฐ˜๋ณต ์กฐ์ธ
(Single Loop Join)
- ํ›„ํ–‰(Driven) ํ…Œ์ด๋ธ”์˜ ์กฐ์ธ ์†์„ฑ์— ์ธ๋ฑ์Šค๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉ
- ์„ ํ–‰ ํ…Œ์ด๋ธ”์˜ ๊ฐ ๋ ˆ์ฝ”๋“œ๋“ค์— ๋Œ€ํ•˜์—ฌ ํ›„ํ–‰ ํ…Œ์ด๋ธ”์˜ ์ธ๋ฑ์Šค ์ ‘๊ทผ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง์ ‘ ๊ฒ€์ƒ‰ ํ›„ ์กฐ์ธํ•˜๋Š” ๋ฐฉ์‹
์ •๋ ฌ ํ•ฉ๋ณ‘ ์กฐ์ธ
(Sort Merge Join)
- Sort Merge ์กฐ์ธ์€ ํ•ด๋‹น ํ…Œ์ด๋ธ”์˜ ์ธ๋ฑ์Šค๊ฐ€ ์—†์„๋•Œ ์ˆ˜ํ–‰์ด ๋œ๋‹ค.
- ํ…Œ์ด๋ธ”์„ ์ •๋ ฌ(Sort) ํ•œ ํ›„์— ์ •๋ ฌ๋œ ํ…Œ์ด๋ธ”์„ ๋ณ‘ํ•ฉ(Merge) ํ•˜๋ฉด์„œ ์กฐ์ธ์„ ์‹คํ–‰ํ•œ๋‹ค.

- ์กฐ์ธ ์—ฐ๊ฒฐ๊ณ ๋ฆฌ์˜ ๋น„๊ต ์—ฐ์‚ฐ์ž๊ฐ€ ๋ฒ”์œ„ ์—ฐ์‚ฐ( >, < )์ธ ๊ฒฝ์šฐ Nested Loop ์กฐ์ธ๋ณด๋‹ค ์œ ๋ฆฌ
- ๋‘ ๊ฒฐ๊ณผ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ ์ฐจ์ด๊ฐ€ ๋งŽ์ด ๋‚˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋น„ํšจ์œจ์ 
ํ•ด์‹œ ์กฐ์ธ
(Hash Join)
- ํ•ด์‹œ(Hash)ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ํ…Œ์ด๋ธ”์˜ ์ž๋ฃŒ๋ฅผ ๊ฒฐํ•ฉํ•˜๋Š” ์กฐ์ธ ๋ฐฉ์‹
- Nested Loop ์กฐ์ธ๊ณผ Sort Merge ์กฐ์ธ์˜ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐ
- ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋Š” ์ƒ๋‹นํžˆ ํฐ hash area๋ฅผ ํ•„์š”๋กœ ํ•จ์œผ๋กœ, ๋ฉ”๋ชจ๋ฆฌ์˜ ์ง€๋‚˜์นœ ์‚ฌ์šฉ์œผ๋กœ ์˜ค๋ฒ„ํ—ค๋“œ ๋ฐœ์ƒ ๊ฐ€๋Šฅ์„ฑ

 

13. ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : GRANT(๊ถŒํ•œ๋ถ€์—ฌ), REVOKE(๊ถŒํ•œํšŒ์ˆ˜)

DCL(Data Control Language) ๋ฐ์ดํ„ฐ ์ œ์–ด์–ด

 

14. ์ •๋‹ต :  4

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : RENAME์€ DDL(Data Definition Language) ๋ฐ์ดํ„ฐ์ •์˜์–ด ์— ์†ํ•œ๋‹ค.

๊ตฌ๋ถ„ ์ข…๋ฅ˜
DDL
(๋ฐ์ดํ„ฐ ์ •์˜์–ด)
CREATE, DROP, MODIFY(์˜ค๋ผํด), ALTER(SQL์„œ๋ฒ„), RENAME, TRUNCATE
DML
(๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด)
SELECT, INSERT, DELETE, UPDATE
DCL
(๋ฐ์ดํ„ฐ ์ œ์–ด์–ด)
GRANT, REVOKE
TCL
(ํŠธ๋žœ์žญ์…˜ ์ œ์–ด์–ด)
COMMIT, ROLLBACK, SAVE POINT

 

15. ์ •๋‹ต :  3

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค :BAN ์นผ๋Ÿผ์œผ๋กœ ๊ทธ๋ฃนํ•‘ํ•˜๊ณ  DISTINCT๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ค‘๋ณต๋œ ์ด๋ฆ„์„ ์ œ๊ฑฐํ•˜๊ณ  ์นด์šดํŒ…ํ•œ๋‹ค.

 

16. ์ •๋‹ต :  1

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ๋ณด๊ธฐ 1๋ฒˆ์€ 6๊ฐœ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋‚˜๋จธ์ง€๋Š” ๋ชจ๋‘ 4๊ฐœ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

NULLIF์˜ ํŠน์ง• : ๋‘ ๊ฐœ์˜ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด NULL, ๊ฐ™์ง€ ์•Š์œผ๋ฉด ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 

17. ์ •๋‹ต :  1

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค :NULL์€ ๋น„๊ต์—์„œ ์• ์ดˆ์— ์ œ์™ธ๋˜์–ด IN() ์—ฐ์‚ฐ์ž ์•ˆ์— NULL์ด ์žˆ์–ด๋„ ๋น„๊ต์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.

์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”์˜ COL1 ์†์„ฑ๊ฐ’ 1, 2๊ฐ’์„ ๊ฐ–๋Š” ํ–‰๋งŒ ์กฐํšŒ๋œ๋‹ค.

 


18. ์ •๋‹ต :  1

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ์กฐํšŒ๊ฒฐ๊ณผ๊ฐ’ ์ˆœ์„œ๋ฅผ ๋‚˜์—ดํ•˜๋ฉด

DNAME, JOB๋ณ„ ์†Œ๊ณ„ โ†’ DNAME ๋ณ„ ์†Œ๊ณ„ โ†’ JOB ๋ณ„ ์†Œ๊ณ„ โ†’ ์ „์ฒด ์ง‘๊ณ„ 

๋ชจ๋“  ์กฐํ•ฉ ๊ฐ€๋Šฅํ•œ ์†Œ๊ณ„์™€ ์ง‘๊ณ„๊ฐ€ ์กฐํšŒ ๋˜์—ˆ์œผ๋ฏ€๋กœ ๋นˆ์นธ์— ๋“ค์–ด๊ฐˆ ๊ทธ๋ฃนํ•จ์ˆ˜๋Š” CUBE(DEPTNO, JOB) ์ž…๋‹ˆ๋‹ค.

SQL> SELECT DNAME,JOB,SUM(SAL) FROM TEST18 GROUP BY CUBE(DNAME,JOB);   

DNAME		     JOB		    SUM(SAL)
-------------------- --------------------
					       45000
		     CLERK		       12000
		     MANAGER		       15000
		     PRESIDENT		       18000
SALES					       24000
SALES		     CLERK			7000
SALES		     MANAGER			8000
SALES		     PRESIDENT			9000
RESEARCH				       15000
RESEARCH	     CLERK			4000
RESEARCH	     MANAGER			5000
RESEARCH	     PRESIDENT			6000
ACCOUNTING					6000
ACCOUNTING	     CLERK			1000
ACCOUNTING	     MANAGER			2000
ACCOUNTING	     PRESIDENT			3000

16 rows selected.

SQL> SELECT DNAME,JOB,SUM(SAL) FROM TEST18 GROUP BY ROLLUP(DNAME,JOB); 

DNAME		     JOB		    SUM(SAL)
-------------------- --------------------
SALES		     CLERK			7000
SALES		     MANAGER			8000
SALES		     PRESIDENT			9000
SALES					       24000
RESEARCH	     CLERK			4000
RESEARCH	     MANAGER			5000
RESEARCH	     PRESIDENT			6000
RESEARCH				       15000
ACCOUNTING	     CLERK			1000
ACCOUNTING	     MANAGER			2000
ACCOUNTING	     PRESIDENT			3000
ACCOUNTING					6000
					       45000

13 rows selected.

SQL> SELECT DNAME,JOB,SUM(SAL) FROM TEST18 GROUP BY GROUPING SETS(DNAME,JOB);

DNAME		     JOB		    SUM(SAL)
-------------------- -------------------- ----------
ACCOUNTING					6000
RESEARCH				       15000
SALES					       24000
		     CLERK		       12000
		     PRESIDENT		       18000
		     MANAGER		       15000

6 rows selected.

SQL> SELECT DNAME,JOB,SUM(SAL) FROM TEST18 GROUP BY CUBE(DNAME);       
SELECT DNAME,JOB,SUM(SAL) FROM TEST18 GROUP BY CUBE(DNAME)
             *
ERROR at line 1:
ORA-00979: not a GROUP BY expression

 

4๋ฒˆ ๋ณด๊ธฐ์— CUBE(DNAME) ์ด๋ ‡๊ฒŒ ํ๋ธŒ๋ฅผ ์ž‘์„ฑํ•˜๋Š”๊ฑด ์˜๋ฏธ๊ฐ€ ์—†๋‹ค.

๊ตฌ๋ถ„ ์„ค๋ช…
ROLLUP - ์ „์ฒดํ•ฉ๊ณ„์™€ ์†Œ๊ทธ๋ฃน ๊ฐ„์˜ ์†Œ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ROLLUP ํ•จ์ˆ˜

์˜ˆ)
GROUP BY ROLLUP (DEPTNO);
โ†’ DEPTNO ํ•ฉ๊ณ„(์†Œ๊ณ„), ์ „์ฒด ํ•ฉ๊ณ„๋ฅผ ์กฐํšŒ
CUBE - CUBE๋Š” ์ œ์‹œํ•œ ์นผ๋Ÿผ์— ๋Œ€ํ•ด์„œ ๊ฒฐํ•ฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์ง‘๊ณ„๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
- ๋‹ค์ฐจ์› ์ง‘๊ณ„๋ฅผ ์ œ๊ณตํ•˜์—ฌ ๋‹ค์–‘ํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ)
GROUP BY CUBE(DEPTNO, JOB);
โ†’ DEPTNO ํ•ฉ๊ณ„, JOB ํ•ฉ๊ณ„, DEPTNO & JOB ํ•ฉ๊ณ„, ์ „์ฒด ํ•ฉ๊ณ„๋ฅผ ์กฐํšŒ
์กฐํ•ฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์กฐํ•ฉ๋œ๋‹ค.  *์‹œ์Šคํ…œ์— ๋ถ€ํ•˜๋ฅผ ๋งŽ์ด ์ฃผ๋Š” ๋‹จ์ ์ด ์žˆ์Œ
GROUPING SET - ์›ํ•˜๋Š” ๋ถ€๋ถ„์˜ ์†Œ๊ณ„๋งŒ ์†์‰ฝ๊ฒŒ ์ถ”์ถœํ•˜์—ฌ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” GROUPING SETS ํ•จ์ˆ˜

 

19. ์ •๋‹ต :  1

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : C2 ๊ฐ’์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ  CASE๋ฌธ์œผ๋กœ B, A, S ๋“ฑ๊ธ‰์„ ๋ถ€์—ฌํ•œ๋‹ค. ์ „์ฒด๋“ฑ๊ธ‰์ด 300์ ์„ ๋„˜๋Š” ๋“ฑ๊ธ‰์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— S๋“ฑ๊ธ‰์€ ์—†๊ณ 

100 ์ด์ƒ์ธ C1์˜ 6๋ฒˆ๋งŒ A๋“ฑ๊ธ‰์„ ๋ฐ›๋Š”๋‹ค.

 

20. ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ์ง‘๊ณ„ ํ•จ์ˆ˜์—์„œ COUNT(*) ํ•จ์ˆ˜๋Š” ์กฐ๊ฑด์ ˆ์ด ๊ฑฐ์ง“์ผ ๋•Œ 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

21. ์ •๋‹ต :  4

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ๋˜‘๊ฐ™์€๊ฒŒ ๋‘๋ฒˆ ๋‚˜์™€์„œ ๋ฌธ๋ฒ•์˜ค๋ฅ˜ ์ด๋‹ค. 

 

1) SUM(๊ธ‰์—ฌ) OVER() : ์ „์ฒด ๊ธ‰์—ฌ์˜ ํ•ฉ๊ณ„ / AVG(๊ธ‰์—ฌ) OVER() : ํ‰๊ท  ๊ธ‰์—ฌ

2) UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING โ†’ ์‹œ์ž‘๋ถ€ํ„ฐ ๋๊นŒ์ง€์˜ ์ „์ฒด ํ•ฉ๊ณ„

2)
SUM(SAL) 
 OVER(PARTITION BY JOB 
          ORDER BY EMPNO RANGE
      BETWEEN UNBOUNDED PRECEDING
      AND UNBOUNDED FOLLOWING
      ) SAL1
      
[๊ฒฐ๊ณผ ์˜ˆ์‹œ]
SAL   SAL2
----------
10     60
20     60
30     60

3) UNBOUNDED PRECEDING AND CURRENT ROW โ†’ ๋ˆ„์  ํ•ฉ๊ณ„

3)
SUM(SAL)
 OVER(PARTITION BY JOB
          ORDER BY JOB RANGE
      BETWEEN UNBOUNDED PRECEDING
      AND CURRENT ROW
      ) SAL2


[๊ฒฐ๊ณผ ์˜ˆ์‹œ]
SAL   SAL2
----------
10     10
20     30
30     60

โœจOVER์ ˆ ์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

[ OPEN ]

๋ˆ„์ , ์ˆœ์œ„, ํผ์„ผํŠธ, ํ‰๊ท , ์ดํ•ฉ ๋“ฑ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ต๊ณ„ or ์ง‘๊ณ„ ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” ์ ˆ์ด OVER์ ˆ์ž…๋‹ˆ๋‹ค.

๋‹จ์ผํ•จ์ˆ˜์™€ ์ง‘๊ณ„ํ•จ์ˆ˜๊ฐ€ ๊ฐ™์ด ์˜ฌ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—, ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์€๋ฐ์š”

๋‹ค์ˆ˜์˜ ์ง‘๊ณ„๊ฒฐ๊ณผ๊ฐ€ ํ•„์š”ํ•  ๋•Œ ์—ฌ๋Ÿฌ ์„œ๋ธŒ์ฟผ๋ฆฌ์™€ ๊ทธ๋ฃน๋ฐ”์ด๋กœ ์ธํ•ด ๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋ฅผ OVER ์ ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑํ• ์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค.

[์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉ ์˜ˆ์ œ]
SELECT col1,
      (SELECT SUM(col2)
       FROM TEST) AS total
FROM TEST;

[OVER ์ ˆ ์‚ฌ์šฉ ์˜ˆ์ œ]
SELECT col1,
       SUM(col2) OVER() total
FROM TEST;

โœจWINDOW FUNCTION

[ OPEN ]

WINDOW ํ•จ์ˆ˜์—๋Š” OVER ๋ฌธ๊ตฌ๊ฐ€ ํ‚ค์›Œ๋“œ๋กœ ํ•„์ˆ˜ ํฌํ•จ๋จ.

SELECT WINDOW_FUNCTION (ARGUMENTS)
OVER ( [PARTITION BY ์ปฌ๋Ÿผ] [ORDER BY ์ ˆ] [WINDOWING ์ ˆ] )
FROM ํ…Œ์ด๋ธ” ๋ช…
  • WINDOW_FUNCTION : ๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋˜ ํ•จ์ˆ˜(SUM, AVG ๋“ฑ๋“ฑ)๋„ ์žˆ๊ณ , ์ƒˆ๋กญ๊ฒŒ WINDOW ํ•จ์ˆ˜์šฉ์œผ๋กœ ์ถ”๊ฐ€๋œ ํ•จ์ˆ˜๋„ ์žˆ์Œ.
  • ARGUMENTS(์ธ์ˆ˜) : ์œˆ๋„์šฐํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์„ค์ •์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์˜ต์…˜์œผ๋กœ ์‚ฌ์šฉ (ํ•จ์ˆ˜์— ๋”ฐ๋ผ 0 ~ N๊ฐœ์˜ ์ธ์ˆ˜๊ฐ€ ์ง€์ •๋  ์ˆ˜ ์žˆ์Œ)
  • PARTITION BY ์ ˆ : ์ปฌ๋Ÿผ ์„ ํƒ์€ ์˜ต์…˜์œผ๋กœ ์ „์ฒด ์กฐํšŒ๋œ ํ–‰์— ๋Œ€ํ•ด ์†Œ๊ทธ๋ฃน์„ ์ง€์ •ํ•œ ์ปฌ๋Ÿผ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
  • ORDER BY ์ ˆ : ์„ ํƒ ์˜ต์…˜ ํ•ด๋‹น ์ปฌ๋Ÿผ์— ๋Œ€ํ•ด์„œ ์ •๋ ฌ
  • WINDOWING ์ ˆ : ํ•จ์ˆ˜์˜ ๋Œ€์ƒ์ด ๋˜๋Š” ํ–‰ ๊ธฐ์ค€์˜ ๋ฒ”์œ„๋ฅผ ๊ฐ•๋ ฅํ•˜๊ฒŒ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ. ROWS๋Š” ๋ฌผ๋ฆฌ์ ์ธ ๊ฒฐ๊ณผ ํ–‰์˜ ์ˆ˜๋ฅผ, RANGE๋Š” ๋…ผ๋ฆฌ์ ์ธ ๊ฐ’์— ์˜ํ•œ ๋ฒ”์œ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š”๋ฐ, ๋‘˜ ์ค‘์˜ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ, WINDOWING ์ ˆ์€SQL SERVER์—์„œ ์ง€์›ํ•˜์ง€ ์•Š์Œ.

โœจWINDOW ์ ˆ

[ OPEN ]

ROWS |  RANGE BETWEEN
UNBOUNDED PRECEDING | CURRENT ROW | VALUE_EXPR PRECEDING/FOLLOWING
AND
UNBOUNDED FOLLOWING | CURRENT ROW | VALUE_EXPR PRECEDING/FOLLOWING
  • ROWS : ๋ฌผ๋ฆฌ์ ์ธ ROW ๋‹จ์œ„๋กœ ํ–‰ ์ง‘ํ•ฉ์„ ์ง€์ • ( ํ˜„์žฌํ–‰์„ ๊ธฐ์ค€์œผ๋กœ ๋ช‡๊ฐœ์˜ ํ–‰์„ ํฌํ•จํ•˜๋Š”์ง€ )
  • RANGE : ๋…ผ๋ฆฌ์ ์ธ ์ƒ๋Œ€๋ฒˆ์ง€๋กœ ํ–‰ ์ง‘ํ•ฉ์„ ์ง€์ • ( ํ˜„์žฌํ–‰์„ ๊ธฐ์ค€์œผ๋กœ ์–ด๋–ค ๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ )
  • BETWEEN ~ AND ์ ˆ : ์œˆ๋„์šฐ์˜ ์‹œ์ž‘๊ณผ ๋ ์œ„์น˜๋ฅผ ์ง€์ •
  • UNBOUNDED PRECEDING : PARTITION์˜ ์ฒซ ๋ฒˆ์งธ ๋กœ์šฐ์—์„œ ์œˆ๋„์šฐ๊ฐ€ ์‹œ์ž‘
  • UNBOUNDED FOLLOWING : PARTITION์˜ ๋งˆ์ง€๋ง‰ ๋กœ์šฐ์—์„œ ์œˆ๋„์šฐ๊ฐ€ ์‹œ์ž‘
  • CURRENT ROW : ์œˆ๋„์šฐ์˜ ์‹œ์ž‘์ด๋‚˜ ๋ ์œ„์น˜๊ฐ€ ํ˜„์žฌ ๋กœ์šฐ

 

22. ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : PL/SQL์€ ์ ˆ์ฐจํ˜• ์–ธ์–ด๋กœ PL/SQL ๋‚ด๋ถ€์—์„œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

PL/SQL๋‚ด๋ถ€์—์„œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋Š” ์ด์œ ๋Š” ์ž„์‹œ ํ…Œ์ด๋ธ”๋กœ ์ž ๊น ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๊ฐ€ ๋งŽ๋‹ค.

 

 

23. ์ •๋‹ต :  3

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : CREATE INDEX [์ธ๋ฑ์Šค๋ช…] ON [ํ…Œ์ด๋ธ”๋ช…] [์นผ๋Ÿผ๋ช…]

์ธ๋ฑ์Šค ์ƒ์„ฑ / ์‚ญ์ œ ๊ตฌ๋ฌธ
-- ์ƒ์„ฑ
  CREATE INDEX ์ธ๋ฑ์Šค๋ช…
  ON ํ…Œ์ด๋ธ”๋ช… ( ์†์„ฑ๋ช… , ์†์„ฑ๋ช…,โ€ฆ)
-- ์‚ญ์ œ
  DROP INDEX ์ธ๋ฑ์Šค๋ช…
  ON ํ…Œ์ด๋ธ”๋ช…
  
-- ์ˆ˜์ •
์ธ๋ฑ์Šค ์‚ญ์ œ ํ›„ ๋‹ค์‹œ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค
 
-- ์ธ๋ฑ์Šค ์กฐํšŒ
SELECT ํ…Œ์ด๋ธ”๋ช…, ์ธ๋ฑ์Šค๋ช…, ์ปฌ๋Ÿผ๋ช…
FROM ALL_IND_COLUMNS
WHERE TABLE_NAME = 'ํ…Œ์ด๋ธ”๋ช…'

 

24. ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ROLLBACK (์ด์ „ ์ƒํƒœ๋กœ ๋˜๋Œ๋ฆฌ๊ธฐ) ์ด๋ฏ€๋กœ TEST24์˜ COUNT(*) โ†’ 3๊ฐœ

SAVEPOINT [์„ธ์ด๋ธŒํฌ์ธํŠธ ๋ช…]

ROLLBACK TO [์ €์žฅ๋œ ์„ธ์ด๋ธŒํฌ์ธํŠธ ๋ช…]

-- SAVEPOINT ๋กœ ์ €์žฅํ•˜๊ณ  ROLLBACK TO ๋กœ ์ €์žฅ๋œ ๊ณณ์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค.

์˜ˆ) ๊ฒŒ์ž„ํ• ๋•Œ ์„ธ์ด๋ธŒํฌ์ธํŠธ ์ €์žฅํ›„, ์ €์žฅ๋œ ๊ทธ ์ˆœ๊ฐ„์œผ๋กœ ๋˜๋Œ์•„๊ฐ

 

25. ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : MINUS (์ฐจ์ง‘ํ•ฉ) 1์„ ์ œ์™ธํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ 2, 3์ด ์ถœ๋ ฅ๋œ๋‹ค.

 

26. ์ •๋‹ต :  3

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : EXISTS(์กด์žฌํ•˜๋Š”), NOT EXISTS(์กด์žฌํ•˜์ง€ ์•Š์€) ์ด๋ฏ€๋กœ

CUSTOMERS ํ…Œ์ด๋ธ”์—์„œ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ณ ๊ฐID ์‹๋ณ„์„ ์œ„ํ•ด์„œ๋Š” NOT EXISTS๋ฅผ ์‚ฌ์šฉํ•˜๊ณ 

WHERE์ ˆ์— CUSTOMER.ID = ORDER.ID ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

27. ์ •๋‹ต :  4

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : <>(๊ฐ™์ง€์•Š์€), ANY (๋‹ค์ˆ˜์˜ ๋น„๊ต๊ฐ’ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๋งŒ์กฑํ•˜๋ฉด TRUE ) ์ด๋ฏ€๋กœ ๋ณด๊ธฐ 4๋ฒˆ์€ ๋ชจ๋“  ํ–‰์ด ์ถœ๋ ฅ๋œ๋‹ค.

 

์˜ˆ) EMP ํ…Œ์ด๋ธ”์— DEPTNO(10, 20, 30) , DEPT ํ…Œ์ด๋ธ”์— DEPTNO(10, 20, 30, 40) ๋ผ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„๋•Œ

๋ณด๊ธฐ 1๋ฒˆ
๋ณด๊ธฐ 2๋ฒˆ
๋ณด๊ธฐ 3๋ฒˆ
๋ณด๊ธฐ 4๋ฒˆ

 

28. ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ์ธ๋ฑ์Šค์— ๋Œ€ํ•ด์„œ ์—ฐ์‚ฐ์„ ํ•˜๋ฉด ์ธ๋ฑ์Šค๊ฐ€ ๋ณ€ํ˜•์ด ๋˜๋ฏ€๋กœ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ• ์ˆ˜๊ฐ€ ์—†๋‹ค.

 

 

29. ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ๋ถ„์‚ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด์„œ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋ฌผ๋ฆฌ์ ์œผ๋กœ ๋ถ„๋ฆฌํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ด๋‹ค.

๋ถ„์‚ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์žฅ์ ๊ณผ ๋‹จ์ 

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

 

30. ์ •๋‹ต :  1

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ์‹คํ–‰๊ณ„ํš (Execution Plan)

์‹คํ–‰๊ณ„ํš์ด๋ž€ SQL์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์ ˆ์ฐจ์™€ ๋ฐฉ๋ฒ•์„ ์˜๋ฏธํ•œ๋‹ค.

- SQL๊ฐœ๋ฐœ์ž๊ฐ€ SQL์„ ์ž‘์„ฑํ•˜์—ฌ ์‹คํ–‰ํ•  ๋•Œ, SQL์„ ์–ด๋–ป๊ฒŒ ์‹คํ–‰ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ๊ณ„ํšํ•˜๊ฒŒ ๋œ๋‹ค. ์ฆ‰, SQL์‹คํ–‰๊ณ„ํš์„ ์ˆ˜๋ฆฝํ›„ SQL์„ ์‹คํ–‰ํ•œ๋‹ค.

- ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” SQL์˜ ์‹คํ–‰๊ณ„ํš์„ ์ˆ˜๋ฆฝํ•˜๊ณ  SQL์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์˜ ์†Œํ”„ํŠธ์›จ์–ด์ด๋‹ค.

SQL> SET AUTOTRACE ON;
SQL> SELECT*FROM TEST30;

DNAME		     JOB			 SAL
-------------------- -------------------- ----------
ACCOUNTING	     CLERK			1000
ACCOUNTING	     MANAGER			2000
ACCOUNTING	     PRESIDENT			3000
RESEARCH	     CLERK			4000
RESEARCH	     MANAGER			5000
RESEARCH	     PRESIDENT			6000
SALES		     CLERK			7000
SALES		     MANAGER			8000
SALES		     PRESIDENT			9000

9 rows selected.


Execution Plan -- ์‹คํ–‰๊ณ„ํš
----------------------------------------------------------
Plan hash value: 2195686282

----------------------------------------------------------------------------
| Id  | Operation	  | Name   | Rows  | Bytes | Cost (%CPU)| Time	   |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |	   |	 9 |   333 |	 2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS FULL| TEST30 |	 9 |   333 |	 2   (0)| 00:00:01 |
----------------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement (level=2)


Statistics
----------------------------------------------------------
	  4  recursive calls
	  0  db block gets
	  8  consistent gets
	  0  physical reads
	  0  redo size
	921  bytes sent via SQL*Net to client
	519  bytes received via SQL*Net from client
	  2  SQL*Net roundtrips to/from client
	  0  sorts (memory)
	  0  sorts (disk)
	  9  rows processed

 

31. ์ •๋‹ต :  3

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ํŒŒํ‹ฐ์…˜ ์ธ๋ฑ์Šค์˜ ๊ฒฝ์šฐ ํŒŒํ‹ฐ์…˜ ํ‚ค์— ๋Œ€ํ•ด์„œ ์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๊ณ  ํŒŒํ‹ฐ์…˜ ํ‚ค์— ๋Œ€ํ•ด์„œ ์ƒ์„ฑํ•œ ์ธ๋ฑ์Šค๋ฅผ GLOBAL์ธ๋ฑ์Šค๋ผ๊ณ  ํ•œ๋‹ค.

ํŒŒํ‹ฐ์…˜์ด๋ผ๋Š”๊ฒƒ์€ ์–ด๋–ค ํŠน์ •ํ•œ ๊ธฐ์ค€์œผ๋กœ ๋ถ„ํ• ํ•˜๋Š”๊ฒƒ์ด๋‹ค. (์ปฌ๋Ÿผ์ด ํŒŒํ‹ฐ์…˜ Key ๊ฐ€ ๋œ๋‹ค.)

 

32. ์ •๋‹ต :  3

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ์ˆซ์ž์™€ NULL ์„ ๋”ํ•˜๋ฉด NULL ์ด๋ฏ€๋กœ 0,  10 + 12 = 22

WHEN SUM (COL1 + COL2) IS NULL THEN 0
ELSE SUM (COL1 + COL2) ์ด๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

NULL + 10   = NULL โ†’ 0
12   + NULL = NULL โ†’ 0
NULL + NULL = NULL โ†’ 0
10   +  12  = 22

 

33. ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : 2๋ฒˆ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์กฐํšŒ๋˜์ง€ ์•Š๋Š”๋‹ค. ์™œ๋ƒํ•˜๋ฉด ROWNUM์€ ๋…ผ๋ฆฌ์ ์ธ ์ˆซ์ž์ด๋ฏ€๋กœ "ROWNUM = 2" ์™€ ๊ฐ™์ด ์กฐํšŒํ•˜๋ฉด ์ฐพ์„ ์ˆ˜ ์—†๋‹ค.

[ 1๋ฒˆ RESULT ]
ENAME		  SAL
---------- ----------
์กฐ์ž๋ฃก           6000

[ 2๋ฒˆ RESULT ]
no rows selected

[ 3๋ฒˆ RESULT ]
ENAME		  SAL
---------- ----------
์กฐ์ž๋ฃก           6000
์ดˆ์„              5000
์—ฌํฌ             4000
๊ด€์šฐ             3000
์กฐ์กฐ             2000
์œ ๋น„             1000

[ 4๋ฒˆ RESULT ]
ENAME		  SAL
---------- ----------
์กฐ์ž๋ฃก           6000
์ดˆ์„              5000
์—ฌํฌ             4000

 

34. ์ •๋‹ต :  4

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค :

์šฐ์„ ์ˆœ์œ„ ์—ฐ์‚ฐ์ž
1 ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž(*, /, +, -)
2 ์—ฐ๊ฒฐ ์—ฐ์‚ฐ์ž (||)
3 ๋น„๊ต ์—ฐ์‚ฐ์ž(<, >, <=, =>, <>, =)
4 IS NULL, LIKE, IN
5 BETWEEN
6 NOT ์—ฐ์‚ฐ์ž
7 AND ์—ฐ์‚ฐ์ž
8 OR ์—ฐ์‚ฐ์ž

 

35. ์ •๋‹ต :  1

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : SELF JOIN์€ ๋™์ผํ•œ ํ…Œ์ด๋ธ”์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์กฐ์ธ์„ ์˜๋ฏธํ•˜๋ฉฐ, FROM์ ˆ์— ๋™์ผํ•œ ํ…Œ์ด๋ธ”๋ช…์ด ๋‘ ๋ฒˆ ์ด์ƒ ๋‚˜ํƒ€๋‚œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  SELF JOIN์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋™์ผํ•œ ํ…Œ์ด๋ธ”์„ ๋‘ ๋ฒˆ ์ด์ƒ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ FROM์ ˆ์— ๋ณ„์นญ(Alias)์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

[SELF JOIN]
SELECT A.์นผ๋Ÿผ๋ช…,
       B.์นผ๋Ÿผ๋ช…
   FROM ํ…Œ์ด๋ธ”1 A, ํ…Œ์ด๋ธ”2 B
   WHERE A.์นผ๋Ÿผ๋ช…2 = B.์นผ๋Ÿผ๋ช…1

 

36. ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : SELECT * FROM [ํ…Œ์ด๋ธ”๋ช…] WHERE LIKE [์กฐ๊ฑด]

SQL> SELECT*FROM TEST36_1 A, TEST36_2 B WHERE A.ENAME LIKE B.CONDITION;

     EMPNO ENAME	      NO CONDITION
---------- ---------- ---------- ----------
      1000 ์กฐ์กฐ                1 ์กฐ%
      3000 ์กฐํ›ˆ                1 ์กฐ%
      2000 ๊ด€์šฐ                2 %์šฐ%

-- ๋ฌธ์ œ์—์„œ COUNT(*) ์ด๋ฏ€๋กœ 

SQL> SELECT COUNT(*) ROWCNT FROM TEST36_1 A, TEST36_2 B WHERE A.ENAME LIKE B.CONDITION;

    ROWCNT
----------
	 3


--'์กฐ์กฐ'๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฆ„ ์กฐํšŒ
WHERE B.CONDITION LIKE '์กฐ์กฐ%'

--'์šฐ'๊ฐ€ ํฌํ•จ๋œ ์ด๋ฆ„ ์กฐํšŒ
WHERE B.CONDITION LIKE '%์šฐ%'

--'์กฐ์กฐ'๋กœ ๋๋‚˜๋Š” ์‚ฌ์›์˜ ์ด๋ฆ„ ์กฐํšŒ
WHERE B.CONDITION LIKE '%์กฐ์กฐ'

 

--A๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž๋ฅผ ์ฐพ๊ธฐ--
SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ” WHERE ์ปฌ๋Ÿผ๋ช… LIKE 'A%'

--A๋กœ ๋๋‚˜๋Š” ๋ฌธ์ž ์ฐพ๊ธฐ--
SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ” WHERE ์ปฌ๋Ÿผ๋ช… LIKE '%A'

--A๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž ์ฐพ๊ธฐ--
SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ” WHERE ์ปฌ๋Ÿผ๋ช… LIKE '%A%'

--A๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋‘๊ธ€์ž ๋ฌธ์ž ์ฐพ๊ธฐ--
SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ” WHERE ์ปฌ๋Ÿผ๋ช… LIKE 'A_'

--์ฒซ๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ 'A''๊ฐ€ ์•„๋‹Œ ๋ชจ๋“  ๋ฌธ์ž์—ด ์ฐพ๊ธฐ--
SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ” WHERE ์ปฌ๋Ÿผ๋ช… LIKE'[^A]'

--์ฒซ๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ 'A'๋˜๋Š”'B'๋˜๋Š”'C'์ธ ๋ฌธ์ž์—ด ์ฐพ๊ธฐ--
SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ” WHERE ์ปฌ๋Ÿผ๋ช… LIKE '[ABC]'
SELECT ์ปฌ๋Ÿผ๋ช… FROM ํ…Œ์ด๋ธ” WHERE ์ปฌ๋Ÿผ๋ช… LIKE '[A-C]'

 

37. ์ •๋‹ต :  3

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : COUNT์— ์ปฌ๋Ÿผ์„ ์ฃผ์—ˆ์„๋•Œ NULL๊ฐ’์€ ํฌํ•จ์ด ๋˜์ง€์•Š์œผ๋ฏ€๋กœ (2,2,3)

SQL> SELECT COUNT(*) FROM TEST37;

  COUNT(*)
----------
	 4
โ†’ ์ „์ฒด ํ–‰์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ• ๋•Œ๋Š” NULL ํฌํ•จ     

SQL> SELECT COUNT(COL1) FROM TEST37;

COUNT(COL1)
-----------
	  2     
โ†’ (์ปฌ๋Ÿผ๋ช…์„ ์ง€์ •ํ• ๊ฒฝ์šฐ NULL ๋ฏธํฌํ•จ)

SQL> SELECT*FROM TEST37 WHERE COL1 IN(12,10,NULL);

      COL1	 COL2
---------- ----------
	12
	10	   12
โ†’ 2๊ฑด

SQL> SELECT COL1, COUNT(*) FROM TEST37 GROUP BY COL1;

      COL1   COUNT(*)
---------- ----------
		    2
	12	    1
	10	    1
โ†’ 3๊ฑด

 

38. ์ •๋‹ต :  1

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ํ…Œ์ด๋ธ” B๋ฅผ ์ƒ์„ฑํ• ๋•Œ "ON DELETE CASCADE" ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๊ณ  Aํ…Œ์ด๋ธ”์˜ ํ–‰์„ ์‚ญ์ œํ•˜๋ฉด B ํ…Œ์ด๋ธ”์˜ ํ–‰๋„ ๊ฐ™์ด ์‚ญ์ œ๋œ๋‹ค.

SQL> SELECT*FROM B;

	 A	    B
---------- ----------
	 1	    1
	 2	    2

SQL> DELETE FROM A WHERE A=1;

1 row deleted.

SQL> SELECT*FROM B;

	 A	    B
---------- ----------
	 2	    2

 

39. ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค :  GROUP BY COL1 โ†’ ์กฐ์กฐ(1๊ฐœ),์œ ๋น„(2๊ฐœ),๊ด€์šฐ(3๊ฐœ),์—ฌํฌ(1๊ฐœ),์ดˆ์„ (1๊ฐœ)

โ†’ HAVING COUNT(*) > 2  ์ด๋ฏ€๋กœ ๊ฐฏ์ˆ˜๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ ๊ด€์šฐ์˜ ๊ฐฏ์ˆ˜ โ†’ 3

 

40. ์ •๋‹ต :  4

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : DISTINCT (์ค‘๋ณต์ œ๊ฑฐ) 

COL1   COL2
-----------
์กฐ์กฐ     1
์กฐ์กฐ     2
์กฐ์กฐ     3


COUNT(COL1)  COUNT(COL2)
------------------------
    3            3

 

41. ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : CHAR๋Š” ๊ณ ์ •๊ธธ์ด ๋ฌธ์ž์—ด์„ ์˜๋ฏธํ•œ๋‹ค.

DOUBLE ์ด DECIMAL ์— ๋น„ํ•ด ๋น„๊ตํ•  ์ˆ˜ ์—†์ด ํฌ์ง€๋งŒ, ์†Œ์ˆ˜์ ์„ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์—๋Š” DECIMAL ์ด ๋” ์ •๋ฐ€ํ•˜๋‹ค.

์ˆซ์ž์˜ ์ •๋ฐ€ํ•œ ํ‘œํ˜„๊ณผ ๊ธˆ์œต๊ด€๋ จ ๊ณ„์‚ฐ๊ณผ ๊ฐ™์€ ๋ถ€๋ถ„์— ์‚ฌ์šฉ ๋œ๋‹ค.

 

DECIMAL(m, d) : M์˜ ์ตœ๋Œ€๊ฐ’์€ 65, D๋Š” ์†Œ์ˆ˜ ์ž๋ฆฟ์ˆ˜์ด๋ฉฐ 0์ด๋ฉด ์†Œ์ˆ˜์  ๊ฐ€์ง€์ง€ ์•Š์Œ

mysql> CREATE TABLE T1(NUM DECIMAL(65,30));
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO T1 VALUES(123.123);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT*FROM T1;
+------------------------------------+
| NUM                                |
+------------------------------------+
| 123.123000000000000000000000000000 |
+------------------------------------+
1 row in set (0.00 sec)

 

 

42. ์ •๋‹ต :  4

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ORDER BY ๊ฐ€ ํ•ญ์ƒ ๋งˆ์ง€๋ง‰์— ์‹คํ–‰๋œ๋‹ค.

 

1๏ธโƒฃFROM ์ ˆ์—์„œ ํ…Œ์ด๋ธ”์˜ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜จ๋‹ค.

2๏ธโƒฃWHERE ์ ˆ์—์„œ ๊ฒ€์ƒ‰ ์กฐ๊ฑด์— ์ผ์น˜ํ•˜์ง€์•Š๋Š” ํ–‰์„ ์ œ์™ธํ•œ๋‹ค.

3๏ธโƒฃGROUP BY ์ ˆ์— ๋ช…์‹œ๋œ ํ–‰์˜ ๊ฐ’์„ ๊ทธ๋ฃนํ™” ํ•œ๋‹ค.

4๏ธโƒฃHAVING ์ ˆ์€ GROUP BY ์ ˆ๋กœ ๊ทธ๋ฃนํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์กฐ๊ฑด์„ ์ •์˜ํ•œ๋‹ค.

5๏ธโƒฃSELECT ์ ˆ์—์„œ ๋ช…์‹œํ•œ ์นผ๋Ÿผ๊ฐ’๋“ค์„ ์กฐํšŒํ•œ๋‹ค.

6๏ธโƒฃORDER BY ์ ˆ์—์„œ ๋ช…์‹œํ•œ ์นผ๋Ÿผ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œํ›„ ์ถœ๋ ฅ

 

 

43. ์ •๋‹ต : 2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ์ˆœ์ˆ˜ ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž๋ž€, ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐœ๋ฐœํ•œ ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž๋ฅผ ์˜๋ฏธํ•œ๋‹ค. DELETE๋Š” ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค.

์ˆœ์ˆ˜ ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž

์—ฐ์‚ฐ์ž ํŠน์ง•
SELECT
( ฯƒ )
ยท ๋ฆด๋ ˆ์ด์…˜์— ์กด์žฌํ•˜๋Š” ํŠœํ”Œ ์ค‘์—์„œ ์„ ํƒ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํŠœํ”Œ์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ๊ตฌํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฆด๋ ˆ์ด์…˜์„ ๋งŒ๋“ฆ
ยท ๋ฆด๋ ˆ์ด์…˜์˜ ํ–‰(๊ฐ€๋กœ)์— ํ•ด๋‹นํ•˜๋Š” ํŠœํ”Œ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์ˆ˜ํ‰ ์—ฐ์‚ฐ์ด๋ผ๊ณ ๋„ ํ•จ
ยท ์—ฐ์‚ฐ์ž์˜ ๊ธฐํ˜ธ๋Š” ๊ทธ๋ฆฌ์Šค ๋ฌธ์ž ์‹œ๊ทธ๋งˆ(ฯƒ)๋ฅผ ์‚ฌ์šฉํ•จ
PROJECT
( ฯ€ )
ยท ์ฃผ์–ด์ง„ ๋ฆด๋ ˆ์ด์…˜์—์„œ ์†์„ฑ List์— ์ œ์‹œ๋œ Attribute๋งŒ์„ ์ถ”์ถœํ•˜๋Š” ์—ฐ์‚ฐ
ยท ๋ฆด๋ ˆ์ด์…˜์˜ ์—ด(์„ธ๋กœ)์— ํ•ด๋‹นํ•˜๋Š” Attribute๋ฅผ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์ˆ˜์ง ์—ฐ์‚ฐ์ž๋ผ๊ณ ๋„ ํ•จ
ยท ์—ฐ์‚ฐ์ž์˜ ๊ธฐํ˜ธ๋Š” ๊ทธ๋ฆฌ์Šค ๋ฌธ์ž ํŒŒ์ด(ฯ€)๋ฅผ ์‚ฌ์šฉํ•จ
JOIN
( โ‹ˆ )
ยท ๊ณตํ†ต ์†์„ฑ์„ ์ค‘์‹ฌ์œผ๋กœ 2๊ฐœ์˜ ๋ฆด๋ ˆ์ด์…˜์„ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์„œ ์ƒˆ๋กœ์šด ๋ฆด๋ ˆ์ด์…˜์„ ๋งŒ๋“œ๋Š” ์—ฐ์‚ฐ
ยท ์—ฐ์‚ฐ์ž์˜ ๊ธฐํ˜ธ๋Š” โ‹ˆ๋ฅผ ์‚ฌ์šฉํ•จ
ยท ์กฐ์ธ ์กฐ๊ฑด์ด '='์ผ ๋•Œ ๋™์ผํ•œ ์†์„ฑ์ด ๋‘ ๋ฒˆ ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด ์ค‘ ์ค‘๋ณต๋œ ์†์„ฑ์„ ์ œ๊ฑฐํ•˜์—ฌ ๊ฐ™์€ ์†์„ฑ์„ ํ•œ ๋ฒˆ๋งŒ ํ‘œ๊ธฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ž์—ฐ(NATURAL) ์กฐ์ธ์ด๋ผ๊ณ  ํ•จ
DIVISION
( รท )
ยท X โŠƒ Y์ธ 2๊ฐœ์˜ ๋ฆด๋ ˆ์ด์…˜์—์„œ R(X)์™€ S(Y)๊ฐ€ ์žˆ์„ ๋•Œ, R์˜ ์†์„ฑ์ด S์˜ ์†์„ฑ๊ฐ’์„ ๋ชจ๋‘ ๊ฐ€์ง„ ํŠœํ”Œ์—์„œ S๊ฐ€ ๊ฐ€์ง„ ์†์„ฑ์„ ์ œ์™ธํ•œ ์†์„ฑ๋งŒ์„ ๊ตฌํ•˜๋Š” ์—ฐ์‚ฐ

 

- - - - - - - - ์ฃผ๊ด€์‹ - - - - - - - -

 

44. ์ •๋‹ต :  ROW_NUMBER()

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ROW_NUMBER() : ์ค‘๋ณต๊ฐ’์ด ์žˆ์–ด๋„ ๊ณ ์œ  ๋“ฑ์ˆ˜ ๋ถ€์—ฌ(1์œ„, 2์œ„, 3์œ„, 4์œ„)

RANK() : ์ค‘๋ณต๊ฐ’์€ ์ค‘๋ณต๋“ฑ์ˆ˜, ๋“ฑ์ˆ˜ ๊ฑด๋„ˆ๋œ€(1์œ„, 1์œ„, 3์œ„, 4์œ„)

DENSE_RANK() : ์ค‘๋ณต๊ฐ’์€ ์ค‘๋ณต๋“ฑ์ˆ˜, ๋“ฑ์ˆ˜ ์•ˆ ๊ฑด๋„ˆ๋œ€(1์œ„, 1์œ„, 2์œ„, 2์œ„) *๋™์ผ ๋“ฑ์ˆ˜ ์ˆœ์œ„์— ์˜ํ–ฅ์ด ์—†๋‹ค.

 

45. ์ •๋‹ต :  INTO TEAM_EMP

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : SELECT * INTO ์ƒˆ๋กœ๋งŒ๋“คํ…Œ์ด๋ธ”๋ช… FROM ๋ณต์‚ฌํ• ํ…Œ์ด๋ธ”๋ช…

[์˜ค๋ผํด]
<ํ…Œ์ด๋ธ” ๋ณต์‚ฌํ•  ๋•Œ>
CREATE TABLE ์ƒˆ๋กœ๋งŒ๋“คํ…Œ์ด๋ธ”๋ช… AS
SELECT * FROM ๋ณต์‚ฌํ• ํ…Œ์ด๋ธ”๋ช… [WHERE ์ ˆ]

<ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋งŒ ๋ณต์‚ฌํ•  ๋•Œ>
CREATE TABLE ์ƒˆ๋กœ๋งŒ๋“คํ…Œ์ด๋ธ”๋ช… AS
SELECT * FROM ๋ณต์‚ฌํ• ํ…Œ์ด๋ธ”๋ช… WHERE 1=2 [where์—๋‹ค๊ฐ€ ์ฐธ์ด ์•„๋‹Œ ์กฐ๊ฑด์„ ๋„ฃ์–ด์คŒ]

<ํ…Œ์ด๋ธ”์€ ์ด๋ฏธ ์ƒ์„ฑ๋˜์–ด ์žˆ๊ณ  ๋ฐ์ดํ„ฐ๋งŒ ๋ณต์‚ฌํ•  ๋•Œ>
INSERT INTO ๋ณต์‚ฌํ• ํ…Œ์ด๋ธ”๋ช… SELECT * FROM ๋ณต์‚ฌํ• ํ…Œ์ด๋ธ”๋ช… [WHERE ์ ˆ]

<ํ…Œ์ด๋ธ” ์ด๋ฆ„ ๋ณ€๊ฒฝ>
ALTER TABLE ๊ตฌํ…Œ์ด๋ธ”๋ช… RENAME TO ์‹ ํ…Œ์ด๋ธ”๋ช…

[SQL server]
<ํ…Œ์ด๋ธ” ๋ณต์‚ฌํ•  ๋•Œ>
SELECT * INTO ์ƒˆ๋กœ๋งŒ๋“คํ…Œ์ด๋ธ”๋ช… FROM ๋ณต์‚ฌํ• ํ…Œ์ด๋ธ”๋ช…

 

46. ์ •๋‹ต :  (COL1, COL2),(COL1),()

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : ROLLUP์€ ๊ทธ๋ฃน๋œ ์นผ๋Ÿผ์˜ SUBTOTAL์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋œ๋‹ค.

๊ทธ๋ฃน์˜ ์ˆ˜๊ฐ€ N๊ฐœ ์ผ๋•Œ N+1๊ฐœ์˜ SUBTOTAL์ด ์ƒ์„ฑ๋œ๋‹ค. 

GROUPING SETS์€ ์—ฌ๋Ÿฌ ๊ทธ๋ฃน์งˆ์˜๋ฅผ UNION ALL(ํ•ฉ์ง‘ํ•ฉ)๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์†Œ๊ณ„, ํ•ฉ๊ณ„๋ฅผ ์ง‘๊ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

GROUPING SETS ํ•จ์ˆ˜  GROUP BY UNION ALL์ ˆ
group by grouping sets(a,b,c) group by a union all 
group by union all 
group by c
group by grouping sets(a,b,(b,c)) group by a union all
group by b union all
group by b,c
group by grouping sets((a,b,c)) group by a,b,c
group by grouping sets(a,rollup(b,c)) group by a union all
group by rollup(b,c)
group by rollup(a,b,c) group by (a,b,c) union all
group by (a,b)union all
group by (a) union all
group by ()
group by cube(a,b,c) group by (a,b,c) union all
group by (a,b) union all
group by (a,c) union all
group by (b,c) union all
group by (a) union all
group by (b) union all
group by (c) union all
group by ()

 

47. ์ •๋‹ต :  1

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : COMMIT ์‹œ์ ์œผ๋กœ ROLLBACK(๋˜๋Œ์•„๊ฐ€๊ธฐ) ํ•˜์˜€์œผ๋ฏ€๋กœ ์ถœ๋ ฅ๊ฐ’์€ 1

 

48. ์ •๋‹ต :  24

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : NVL(COL2,0) , COL2 ์ด NULL์ด๋ฉด 0์œผ๋กœ ๋Œ€์ฒด๋˜๊ณ  ํ‰๊ท ์ด ๊ณ„์‚ฐ๋œ๋‹ค.

 

49. ์ •๋‹ต :  16

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : WHERE ์ ˆ์— A.COL1 <> B.COL1 ์ด๋ฏ€๋กœ A.COL1 ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ๋‹ค๋ฅธ์ˆ˜์˜ ๊ฐฏ์ˆ˜๋ฅผ ์นด์šดํŠธํ•œ๋‹ค.

SQL> SELECT COUNT(*) FROM TEST49 A, TEST49_2 B WHERE A.COL1 <> B.COL1;

  COUNT(*)
----------
	16

 

 

50. ์ •๋‹ต :  CROSS JOIN

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : CROSS JOIN์˜ ๊ฒฐ๊ณผ ๊ฐœ์ˆ˜๋Š” ๋‘ ํ…Œ์ด๋ธ”์˜ ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณฑํ•œ ๊ฐœ์ˆ˜๊ฐ€ ๋œ๋‹ค.

CROSS JOIN์€ ์ƒํ˜ธ ์กฐ์ธ์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋ฉฐ, ํ•œ ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰๋“ค๊ณผ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰์„ ์กฐ์ธ์‹œํ‚ค๋Š” ๊ธฐ๋Šฅ์„ ํ•œ๋‹ค.
์ด๋Ÿฌํ•œ CROSS JOIN์„ ์นดํ…Œ์‹œ์•ˆ ๊ณฑ (Cartesian Product)๋ผ๊ณ ๋„ ํ•œ๋‹ค.
SQL> SELECT COUNT(*) FROM TEST49 CROSS JOIN TEST49;

  COUNT(*)
----------
	25

 

 

 

* 30๋ฒˆ, 49๋ฒˆ ๋ฌธ์ œ๋Š” ๋‹ค๋ฅธ๊ธฐ์ถœ๋ฌธ์ œ๋กœ ๊ตฌ์„ฑ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

sqld 38ํšŒ ๊ธฐ์ถœ๋ฌธ์ œ๋ณต์› / ์ •๋‹ต ์ถœ์ฒ˜ : ์œ ํŠœ๋ธŒ ์ด๊ธฐ์ ์˜์ง„๋‹ท์ปด

300x250

์ฝ”๋“œ