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

[์˜ค๋‹ต๋…ธํŠธ] 2-2. SQL ํ™œ์šฉ

by kime2 2024. 3. 4.
๐ŸŽˆ๊ณ„์ธตํ˜• ์งˆ์˜์˜ ๊ตฌ์กฐ
select ์นผ๋Ÿผ๋ช…, (level), (connect_by_isleaf)
from ํ…Œ์ด๋ธ”๋ช…
where ์กฐ๊ฑด
start with ์‹œ์ž‘์กฐ๊ฑด -> ์˜ˆ) start with mgr is null / start with mrg = 'D' / start with mrg in(111,222)*์‹œ์ž‘๋…ธ๋“œ 2๊ฐœ
connect by prior ๋ฐฉํ–ฅ 
- (์ˆœ๋ฐฉํ–ฅ) prior ์ž์‹ = ๋ถ€๋ชจ -> connect by prior empno = mgr (์ž์‹์ด ๋จผ์ € ๋‚˜์˜จ ๋ถ€๋ชจ๋‹ค) 
- (์—ญ๋ฐฉํ–ฅ) prior ๋ถ€๋ชจ = ์ž์‹ -> connect by prior mgr = empno
(์ˆœ๋ฐฉํ–ฅ) leve l: ์‹œ์ž‘๋…ธ๋“œroot =1, ์ž์‹๋…ธ๋“œ 2,3,4...
(์ˆœ๋ฐฉํ–ฅ) connect_by_isleaf : ํ•ด๋‹น๋…ธ๋“œ์˜ ํ›„์† ๋…ธ๋“œ๊ฐ€ ์žˆ์œผ๋ฉด 1, ์•„๋‹ˆ๋ฉด 0์œผ๋กœ ํ‘œํ˜„
(์ˆœ๋ฐฉํ–ฅ) connect_by_root : ์‹œ์ž‘๋…ธํŠธ ํ‘œ์‹œ
(์ˆœ๋ฐฉํ–ฅ) sys_connect_by_path(~~) : ์‹œ์ž‘๋…ธ๋“œ๋ถ€ํ„ฐ ํ˜„์žฌ๊นŒ์ง€ ๊ฒฝ๋กœ ํ‘œ์‹œ

(์—ญ๋ฐฉํ–ฅ) leve l: ์‹œ์ž‘๋…ธ๋“œroot=1, ๋ถ€๋ชจ๋…ธ๋“œ 2,3,4...
(์—ญ๋ฐฉํ–ฅ) connect_by_isleaf : ํ•ด๋‹น๋…ธ๋“œ์˜ ํ›„์† ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด 1 (์ฆ‰, root๋ฉด 1, ์•„๋‹ˆ๋ฉด ์ฃ„๋‹ค 0)

order siblings by : ํ˜•์ œ๋…ธ๋“œ(๋™์ผ level)์‚ฌ์ด์—์„œ ์ •๋ ฌ์„ ์ˆ˜ํ–‰

๐ŸŽˆ์„œ๋ธŒ์ฟผ๋ฆฌ
์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ์ปฌ๋Ÿผ์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ฉ”์ธ์ฟผ๋ฆฌ๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์ปฌ๋Ÿผ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค
์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ๋Š” order by์ ˆ์„ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•œ๋‹ค
๋ฉ”์ธ์ฟผ๋ฆฌ ๋ ˆ๋ฒจ์ด 1์ด๊ณ , ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ M์ด๋ฉด ํ•ญ์ƒ ๋ฉ”์ธ์ฟผ๋ฆฌ ๋ ˆ๋ฒจ1๋กœ ๊ฒฐ๊ณผ์ง‘ํ•ฉ์ด ์ƒ์„ฑ๋œ๋‹ค



๐ŸŽˆ๋‹ค์ค‘ํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ
์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฑด์ˆ˜๊ฐ€ 2๊ฑด ์ด์ƒ์ผ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์„ ๋•Œ
1. IN(์„œ๋ธŒ์ฟผ๋ฆฌ) : ์ž„์˜์˜ ๊ฒฐ๊ณผ ์ค‘ ํ•˜๋‚˜๋งŒ ๋งŒ์กฑํ•ด๋„ ์ฐธ
2. ๋น„๊ต์—ฐ์‚ฐ์ž ALL(์„œ๋ธŒ์ฟผ๋ฆฌ) : ๊ฒฐ๊ณผ์˜ ๋ชจ๋“  ๊ฐ’์„ ๋งŒ์กฑํ•ด์•ผ ํ•˜๋Š” ์กฐ๊ฑด
3. ๋น„๊ต์—ฐ์‚ฐ์ž any/some(์„œ๋ธŒ์ฟผ๋ฆฌ) : ๊ฒฐ๊ณผ์˜ ์–ด๋Š ํ•˜๋‚˜์˜ ๊ฐ’์ด๋ผ๋„ ๋งŒ์กฑํ•˜๋ฉด ๋˜๋Š” ์กฐ๊ฑด
4. exist (์„œ๋ธŒ์ฟผ๋ฆฌ) : ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธ, ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฑด์„ ํ•˜๋‚˜๋ผ๋„ ์ฐพ์œผ๋ฉด ๊ฒ€์ƒ‰ ์ค‘์ง€
-select์ ˆ์— ์‚ฌ์šฉ๋œ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๋‹จ์ผํ–‰ ์—ฐ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ผ๊ณ  ํ•˜๋ฉฐ, join์œผ๋กœ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
- from์ ˆ์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๋™์  ๋ทฐ ๋ผ๊ณ ๋„ ํ•˜๋ฉฐ, SQL๋ฌธ์žฅ ์ค‘ ํ…Œ์ด๋ธ” ๋ช…์ด ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ณณ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

๐ŸŽˆ๊ณ ๊ธ‰์ง‘๊ณ„
1.rollup: ์†Œ๊ทธ๋ฃน ๋ณ„ ์†Œ๊ณ„ ๊ณ„์‚ฐ ์ถ”๊ฐ€
select dname, job, count(*)์ง์›์ˆ˜, sum(sal)๊ธ‰์—ฌํ•ฉ
from 
group by rollup (dname,job)
>> ๋ถ€์„œ์ด๋ฆ„๋ณ„ ์ง์—…๋ณ„ ์ง์›์ˆ˜์˜ ํ•ฉ๊ณผ, ๊ธ‰์—ฌํ•ฉ์˜ ์†Œ๊ณ„

2. grouping sets : ์—ฌ๋Ÿฌ ์นผ๋Ÿผ ๊ฐ๊ฐ์— ๋Œ€ํ•ด ๋ฐ˜๋ณต์ ์œผ๋กœ ๊ทธ๋ฃนํ™”
select dname, job, count(*)์ง์›์ˆ˜, sum(sal)๊ธ‰์—ฌํ•ฉ
from 
group by grouping sets (dname, job)
>>> ๋ถ€์„œ์ด๋ฆ„๋ณ„ ์ž‘์›์ˆ˜ํ•ฉ๊ณผ, ๊ธ‰์—ฌํ•ฉ + ์ง์—…๋ณ„ ์ง์›์ˆ˜ํ•ฉ๊ณผ ๊ธ‰์—ฌํ•ฉ (๊ฐ ๋ฐ˜๋Œ€์˜ ์ปฌ๋Ÿผ์€ NULL)

3. cube : ๋‹ค์ฐจ์› ์†Œ๊ณ„ ๊ณ„์‚ฐ ์ถ”๊ฐ€(์ˆœ์„œ๋ฌด๊ด€), ๋ชจ๋“  ์กฐํ•ฉ์˜ ์ง‘๊ณ„๊ณ„์‚ฐ(์‹œ์Šคํ…œ ๋ถ€ํ•˜)
select dname, job, count(*)์ง์›์ˆ˜, sum(sal)๊ธ‰์—ฌํ•ฉ
from 
group by cube (dname, job)

๐ŸŽˆ์œˆ๋„์šฐ ํ•จ์ˆ˜
-์ˆœ์œ„ : rank(๊ณต๋™์ˆ˜์ƒ, 113), dense_rank(๊ณต๋™์ˆ˜์ƒ, 112), row_number(1,2,3)
-์ง‘๊ณ„ : sum, max, min, avg, count 
-ํ–‰ ์ˆœ์„œ : first_value, last_value, lag, lead
-๋น„์œจ : ratio_report, percent_rank, ntitle
-ํ†ต๊ณ„ : corr, stddev, variance

๐ŸŽˆ๋ทฐ ์‚ฌ์šฉ์˜ ์žฅ์ 
1. ๋…๋ฆฝ์„ฑ : ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด๋„ ๋ทฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‘์šฉํ”„๋กœ๊ทธ๋žจ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค
2. ํŽธ๋ฆฌ์„ฑ : ๋ณต์žกํ•จ ์งˆ์˜๋ฅผ ๋ทฐ๋กœ ์ƒ์„ฑํ•˜์—ฌ ๊ด€๋ จ ์งˆ์˜๋ฅผ ๋‹จ์ˆœํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ํ•ด๋‹น ํ˜•ํƒœ์˜ SQL์„ ์ž์ฃผ ์ด์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค
3. ๋ณด์•ˆ์„ฑ : ์ง์›์˜ ๊ธ‰์—ฌ์ •๋ณด์™€ ๊ฐ™์ด ์ˆจ๊ธฐ๊ณ ์ž ํ•œ๋‹ค๋ฉด, ๋ทฐ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ•ด๋‹น ์นผ๋Ÿผ์„ ๋นผ๊ณ  ์ƒ์„ฑํ•˜์—ฌ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ •๋ณด๋ฅผ ๊ฐ์ถœ ์ˆ˜ ์žˆ๋‹ค
- ์ธ๋ฆฌ์ธ๋ทฐ =. ๋™์ ์ธ๋ทฐ = from์ ˆ ์„œ๋ธŒ์ฟผ๋ฆฌ
:์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋งˆ์น˜ ์‹คํ–‰ ์‹œ์— ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋œ ํ…Œ์ด๋ธ”์ธ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ธ๋ผ์ธ๋ทฐ๋Š” SQL๋ฌธ์ด ์‹คํ–‰๋  ๋•Œ๋งŒ ์ž„์‹œ์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ๋™์ ์ธ ๋ทฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ํ•ด๋‹น์ •๋ณด๊ฐ€ ์ €์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค

 

1. ์„œ๋ธŒ์ฟผ๋ฆฌ

๋‹ค์ค‘์ปฌ๋Ÿผ = ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋กœ ์—ฌ๋Ÿฌ ์ปฌ๋Ÿผ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค, ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ์กฐ๊ฑด ์ ˆ์— ์—ฌ๋Ÿฌ ์นผ๋Ÿผ์„ ๋™์‹œ์— ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋‹ค. ์„œ๋ธŒ์ฟผ๋ฆฌ์™€ ๋ฉ”์ธ์ฟผ๋ฆฌ์—์„œ ๋น„๊ตํ•˜๊ณ ์ž ํ•˜๋Š” ์ปฌ๋Ÿผ์˜ ๊ฐœ์ˆ˜์™€ ์ปฌ๋Ÿผ์˜ ์˜์น˜๊ฐ€ ๋™์ผํ•ด์•ผ ํ•œ๋‹ค

 

2. col1๋ณ„ ํ•ฉ๊ณ„์™€ ์ „์ฒดํ•ฉ๊ณ„

#[1] cube
select col1, sum(col2)
from t1
group by cube(col1)
order by 1 asc;

#[2] rollup
select col1, sum(col2)
from t1
group by rollup(col1)
order by 1 asc;

#[3]
select col1, sum(col2) #col1๋ณ„ ๊ฐœ๋ณ„ ํ•ฉ๊ณ„
from t1
group by col1
union all
select null, sum(col2) # ์ดํ•ฉ๊ณ„
from t1
order by 1 asc;

 

1. ์ง‘ํ•ฉ์—ฐ์‚ฐ์ž

  • union ์—ฐ์‚ฐ์ž๋Š” ํ•ฉ์ง‘ํ•ฉ ๊ฒฐ๊ณผ์—์„œ ์ค‘๋ณต๋œ ํ–‰์„ ํ•˜๋‚˜์˜ ํ–‰์œผ๋กœ ๋งŒ๋“ ๋‹ค
  • union all์—ฐ์‚ฐ์ž๋Š” ์ง‘ํ•ฉ ๊ฐ„์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ union๊ณผ ๊ฒฐ๊ณผ๊ฐ€ ๋™์ผํ•˜๋‹ค
  • union ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ SQL์€ ๊ฐ๊ฐ์˜ ์ง‘ํ•ฉ์— group by ์ ˆ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค
  • union ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ SQL์€ ๋งˆ์ง€๋ง‰์œผ๋กœ orderby๋ฅผ ํ•œ๋ฒˆ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค

 

 

2.๊ทธ๋ฃน์—ฐ์‚ฐ

select b.grade, a.job, sum(a.sal), count(*)
from emp a, sal b
where a.sal between b.losal and b.hisal
groupo by grouping sets(grade, (job, grade))
# group by grade, rollup(job)
# group by grade, cube(job)
#์ง‘๊ณ„์ปฌ๋Ÿผ์ด 1๊ฐœ์ผ๋•Œ๋Š” rollup๊ณผ cube๋Š” ๋™์ผ

 

# HRD, MKT๋ถ€์„œ๋ณ„ ์‚ฌ์›๋ฒˆํ˜ธ์™€ ๊ธ‰์—ฌ,  ํ•ด๋‹น๊ธ‰์—ฌ์˜ ๋ถ€์„œ๋น„์ค‘์„ ๊ตฌํ•˜๊ธฐ

๋ถ€์„œ์ฝ”๋“œ ์‚ฌ์›๋ฒˆํ˜ธ ๊ธ‰์—ฌ ๊ธ‰์—ฌ๋น„์ค‘
HRD A004 3500000 0.52
HRD A008 3200000 0.48
HRD A007 4500000 0.52
MKT A003 2340000 0.27
MKT A002 1830000 0.21

 

#[1]
select a.๋ถ€์„œ์ฝ”๋“œ, a.์‚ฌ์›๋ฒˆํ˜ธ, a.๊ธ‰์—ฌ,
	round(ratio_to_report(๊ธ‰์—ฌ) over (partition by ๋ถ€์„œ์ฝ”๋“œ),2) as ๊ธ‰์—ฌ๋น„์ค‘
from(select ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„, ๋ถ€์„œ์ฝ”๋“œ, ๊ธ‰์—ฌ, sum(๊ธ‰์—ฌ) over(partition by ๋ถ€์„œ์ฝ”๋“œ)as ๋ถ€์„œ๊ธ‰์—ฌํ•ฉ
	from ์ง์›
    where ๋ถ€์„œ์ฝ”๋“œ in('MKT','HRD')a
order by a.๋ถ€์„œ์ฝ”๋“œ, a.๊ธ‰์—ฌ desc, a.์‚ฌ์›๋ฒˆํ˜ธ

#[2]
select a.๋ถ€์„œ์ฝ”๋“œ, a.์‚ฌ์›๋ฒˆํ˜ธ, a.๊ธ‰์—ฌ,
	round(๊ธ‰์—ฌ/๋ถ€์„œ๊ธ‰์—ฌํ•ฉ,2) as ๊ธ‰์—ฌ๋น„์ค‘
from(select ์‚ฌ์›๋ฒˆํ˜ธ, ์ด๋ฆ„, ๋ถ€์„œ์ฝ”๋“œ, ๊ธ‰์—ฌ, sum(๊ธ‰์—ฌ) over(partition by ๋ถ€์„œ์ฝ”๋“œ)as ๋ถ€์„œ๊ธ‰์—ฌํ•ฉ
	from ์ง์›
    where ๋ถ€์„œ์ฝ”๋“œ in('MKT','HRD')a
order by a.๋ถ€์„œ์ฝ”๋“œ, a.๊ธ‰์—ฌ desc, a.์‚ฌ์›๋ฒˆํ˜ธ

#[3]
select a.๋ถ€์„œ์ฝ”๋“œ, a.์‚ฌ์›๋ฒˆํ˜ธ, a.๊ธ‰์—ฌ,
	round(๊ธ‰์—ฌ/b.๋ถ€์„œ๊ธ‰์—ฌํ•ฉ,2) as ๊ธ‰์—ฌ๋น„์ค‘
from ์ง์›a , (select ๋ถ€์„œ์ฝ”๋“œ, sum(๊ธ‰์—ฌ) as ๋ถ€์„œ๊ธ‰์—ฌํ•ฉ
			from ์ง์›
            where ๋ถ€์„œ์ฝ”๋“œ in('MKT','HRD')
            group by ๋ถ€์„œ์ฝ”๋“œ)b
where a.๋ถ€์„œ์ฝ”๋“œ = b.๋ถ€์„œ์ฝ”๋“œ
and a.๋ถ€์„œ์ฝ”๋“œ in('MKT','HRD')
order by a.๋ถ€์„œ์ฝ”๋“œ, a.๊ธ‰์—ฌ desc, a.์‚ฌ์›๋ฒˆํ˜ธ

 

 

3. ๊ณ„์ธตํ˜• ๊ตฌ์กฐ

  • start with ์ ˆ์€ ๊ณ„์ธต๊ตฌ์กฐ์˜ ์‹œ์ž‘์ ์„ ์ง€์ •ํ•˜๋Š” ๊ตฌ๋ฌธ
  • order siblings by ์ ˆ์€ ํ˜•์ œ๋…ธ๋“œ์‚ฌ์ด์—์„œ ์ •๋ ฌ์„ ์ง€์ •ํ•˜๋Š” ๊ตฌ๋ฌธ
  • ์ˆœ๋ฐฉํ–ฅ์ „๊ฐœ๋ž€ ๋ถ€๋ชจ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ์ž์‹๋…ธ๋“œ๋ฐฉํ–ฅ์œผ๋กœ ์ „๊ฐœํ•˜๋Š” ๊ฒƒ
  • ๋ฃจํŠธ๋…ธ๋“œ์˜ level์€ 1์ด๋‹ค
  • SQL server์—์„œ์˜ ๊ณ„์ธตํ˜• ์งˆ์˜๋ฌธ์€ CTE(common table expression)๋ฅผ ์žฌ๊ท€ ํ˜ธ์ถœํ•จ์œผ๋กœ์จ ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ์ „๊ฐœํ•œ๋‹ค
  • SQL server์—์„œ์˜ ๊ณ„์ธตํ˜• ์งˆ์˜๋ฌธ์€ ์•ต์ปค๋งด๋ฒ„๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๊ธฐ๋ณธ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ๋งŒ๋“ค๊ณ  ์ด๋ฃจ ์žฌ๊ท€๋งด๋ฒ„๋ฅผ ์ง€์†์ ์œผ๋กœ ์‹คํ–‰ํ•œ๋‹ค
  • ์˜ค๋ผํด ๊ณ„์ธตํ˜• ์งˆ์˜๋ฌธ์—์„œ where์ ˆ์€ ๋ชจ๋“  ์ „๊ฐœ๋ฅผ ์ง„ํ–‰ํ•œ ํ›„ ํ•„ํ„ฐ์กฐ๊ฑด์œผ๋กœ์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ถ”์ถœํ•œ๋‹ค
  • ์˜ค๋ผํด ๊ณ„์ธตํ˜• ์งˆ์˜๋ฌธ์—์„œ priorํ‚ค์›Œ๋“œ๋Š” connect by, select, where์ ˆ์—์„œ๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค
tab1 sql ๊ตฌ์กฐ
select c3
from tab1
start with c2 is null -> c2๊ฐ€ ๋„๊ฐ’์ธ A์—์„œ ์‹œ์ž‘
connect by prior c1 = c2 ->๋ถ€๋ชจ = ์ž์‹(์—ญ๋ฐฉํ–ฅ)
order siblings by c3 desc -> c3์—์„œ ๋™์ผ ์‹œ ์ •๋ ฌ ์˜ค๋ฆ„์ฐจ์ˆœ
a ->b
   ->c  
         -> d
c1 c2 c3
1 null A
2 1 B
3 1 C
4 2 d

๊ฒฐ๊ณผ

c3
A
C(C์™€ B๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ)
B
D

 

4. ์„œ๋ธŒ์ฟผ๋ฆฌ

  • ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋ณต์ˆ˜ ํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” IN, ALL, ANY๋“ฑ์˜ ๋ณต์ˆ˜ ํ–‰ ๋น„๊ต ์—ฐ์‚ฐ์ž์™€ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค
  • -> in,all,any๋“ฑ์˜ ๋ณต์ˆ˜ํ–‰ ๋น„๊ต ์—ฐ์‚ฐ์ž๋Š” ๋‹จ์ผํ–‰ ์„œ๋ธŒ์ฟผ๋ฆฌ์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค
  • ๋‹ค์ค‘์นผ๋Ÿผ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์นผ๋Ÿผ์ด ๋ฐ˜ํ™˜๋˜์–ด ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ์กฐ๊ฑด๊ณผ ๋น„๊ต๋˜๋Š”๋ฐ SQL server์—์„œ๋Š” ํ˜„์žฌ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค
  • ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๋‹จ์ผํ–‰ ๋˜๋Š” ๋ณต์ˆ˜ํ–‰ ๋น„๊ต์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค
  • ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” select, from, having, order by์ ˆ ๋“ฑ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค
  • ์—ฐ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ๋ฉ”์ธ์ฟผ๋ฆฌ ์นผ๋Ÿผ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ํ˜•ํƒœ์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ๋‹ค
  • ๋น„์—ฐ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ์ฃผ๋กœ ๋ฉ”์ธ์ฟผ๋ฆฌ์— ๊ฐ’์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ
  • ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ์ œ๊ณต๋  ์ˆ˜๋„ ์žˆ๊ณ , ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋ฉ”์ธ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋กœ๋„์ œ๊ณต๋  ์ˆ˜ ์žˆ๋‹ค

5. ์œˆ๋„์šฐํ•จ์ˆ˜

  • percent-rank = ํŒŒํ‹ฐ์…˜๋ณ„ ์œˆ๋„์šฐ๋ฅผ ์ œ์ผ ๋จผ์ € ๋‚˜์˜ค๋Š” ๊ฒƒ์„ 0์œผ๋กœ, ์ œ์ผ ๋Šฆ๊ฒŒ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ 1๋กœ ํ•˜์—ฌ, ๊ฐ’์ด ์•„๋‹Œ ํ–‰์˜ ์ˆœ์„œ๋ณ„ ๋ฐฑ๋ถ„์œจ์„ ๊ตฌํ•œ๋‹ค(ํŒŒํ‹ฐ์…˜๋ณ„ 0~1)
  • dense_rank = ์ˆœ์œ„๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋กœ ๋™์ผํ•œ ์ˆœ์œ„๋ฅผ ํ•˜๋‚˜์˜ ๋“ฑ์ˆ˜๋กœ ๋ณธ๋‹ค(1,1,2,3)
  • cume_dist = ํŒŒํ‹ฐ์…˜๋ณ„ ์œˆ๋„์šฐ์˜ ์ „์ฒด๊ฑด์ˆ˜์—์„œ ํ˜„์žฌ ํ–‰๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฑด์ˆ˜์— ๋Œ€ํ•œ ๋ˆ„์  ๋ฐฑ๋ถ„์œจ
  • rank = ํŒŒํ‹ฐ์…˜๋‚ด์˜ orderby์— ์˜ํ•œ ์ˆœ์œ„๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋กœ, ๋™์ผํ•œ ์ˆœ์œ„์— ๋Œ€ํ•ด์„œ๋Š” ๋™์ผํ•œ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌ(1,1,3,4)
  • ratio_to_report = ํŒŒํ‹ฐ์…˜ ๋‚ด์˜ ์ฃผ์–ด์ง„ ์นผ๋Ÿผ์˜ ๊ฐ’์˜ ํ•ฉ๊ณ„์— ๋Œ€ํ•œ ๋ฐฑ๋ถ„์œจ์„ ์†Œ์ˆ˜์ ์œผ๋กœ ๋‚˜ํƒ€๋ƒ„
  • ntile = ํŒŒํ‹ฐ์…˜๋ณ„ ์ „์ฒด ๊ฑด์ˆ˜๋ฅผ ์ฃผ์–ด์ง„ ์ธ์ž๋กœ n๋“ฑ๋ถ„ํ•œ ๊ฒฐ๊ณผ

6. ๋ทฐ

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

 

7. revoke์™€ grant

  • ์–ด๋–ค ์‚ฌ์šฉ์ž๊ฐ€ with grant option๊ณผ ํ•จ๊ป˜ ๊ถŒํ•œ์„ ํ—ˆ๊ฐ€๋ฐ›์•˜์œผ๋ฉด ๊ทธ ์‚ฌ์šฉ์ž๋Š” ํ•ด๋‹น ๊ถŒํ•œ์„ with grant option์œ ๋ฌด์™€ ๊ด€๊ณ„์—†์ด ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž์—๊ฒŒ ํ—ˆ๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค
  • public์„ ์‚ฌ์šฉํ•˜๋ฉด ์ž์‹ ์—๊ฒŒ ํ—ˆ๊ฐ€๋œ ๊ถŒํ•œ์„ ๋ชจ๋‘ ์‚ฌ์šฉ์ž๋“ค์—๊ฒŒ ํ—ˆ๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค
  • revoke๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ถŒํ•œ์„ ์ทจ์†Œํ•˜๋ฉด ๊ถŒํ•œ์„ ์ทจ์†Œ๋‹นํ•œ ์‚ฌ์šฉ์ž๊ฐ€ with grant option์„ ํ†ตํ•ด์„œ ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž์—๊ฒŒ ํ—ˆ๊ฐ€ํ–ˆ๋˜ ๊ถŒํ•œ๋“ค๋„ ๋ชจ๋‘ ์—ฐ์‡„์ ์œผ๋กœ ์ทจ์†Œ๋œ๋‹ค
  • revoke๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ถŒํ•œ์„ ์ทจ์†Œํ•  ๋•Œ ๊ทธ ๊ถŒํ•œ์„ ํ—ˆ๊ฐ€ํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ๊ถŒํ•œ์„ ์ทจ์†Œํ•  ์ˆ˜ ์žˆ๋‹ค

B_user๊ฐ€ ํ•˜๋‹จ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”

- GRANT SELECT, UPDATE ON A_user.TB_A to user_B

update A_user.TB_A # update
set col1 = 'AAA' 
where col2=3 # where + select ์‚ฌ์šฉ

 

8.

select empno, sal
from emp
where sal >= (select max(sal) # ๋‹ค์ค‘ํ–‰
	from emp
    grouop by deptno)
  • ์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ๋‹จ์ผํ–‰ ๋˜๋Š” ๋ณต์ˆ˜ํ–‰ ๋น„๊ต์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋‹จ์ผ ํ–‰ ๋น„๊ต์—ฐ์‚ฐ์ž๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜๋“œ์‹œ 1์ดํ•˜์—ฌ์•ผ ํ•˜๊ณ , ๋ณต์ˆ˜ํ–‰ ์—ฐ์‚ฐ์ž๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ๊ฑด์ˆ˜์™€ ์ƒ๊ด€์—†๋‹ค

 

select ์ƒํ’ˆ๋ถ„๋ฅ˜์ฝ”๋“œ,
	avg(์ƒํ’ˆ๊ฐ€๊ฒฉ) as ์ƒํ’ˆ๊ฐ€๊ฒฉ,
    count(*) over(order by avg(์ƒํ’ˆ๊ฐ€๊ฒฉ) range between 1000 preceding and 1000 folwing) as์œ ์‚ฌ๊ฐœ์ˆ˜
from ์ƒํ’ˆ
group by ์ƒํ’ˆ๋ถ„๋ฅ˜์ฝ”๋“œ
  • group by์ ˆ์˜ ์ง‘ํ•ฉ์„ ์›๋ณธ์œผ๋กœ ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ window function ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด group by์ ˆ๊ณผ window๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด๋„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค
  • ์œ ์‚ฌ๊ฐœ์ˆ˜ ์ปฌ๋Ÿผ์€ ์ƒํ’ˆ๋ถ„๋ฅ˜์ฝ”๋“œ๋กœ grouping ๋œ ์ง‘ํ•ฉ์„ ์›๋ณธ์ง‘ํ•ฉ์œผ๋กœํ•˜์—ฌ ์ƒํ’ˆ๋ถ„๋ฅ˜์ฝ”๋“œ๋ณ„ ํ‰๊ท ์ƒํ’ˆ๊ฐ€๊ฒฉ์„ ์„œ๋กœ ๋น„๊ตํ•˜์—ฌ ํ˜„์žฌ ์ฝํžŒ ์ƒํ’ˆ๋ถ„๋ฅ˜์ฝ”๋“œ์˜ ํ‰๊ท ๊ฐ€๊ฒฉ ๋Œ€๋น„ -1000 ~ 1000์‚ฌ์ด์— ์กด์žฌํ•˜๋Š” ์ƒํ’ˆ๋ถ„๋ฅ˜์ฝ”๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•œ ๊ฒƒ

9. ์ˆœ์œ„ํ•จ์ˆ˜

  • ์ˆœ์œ„ํ•จ์ˆ˜์— order by๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ˆœ์œ„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ํ˜น์€ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค
  • rank() : ์ค‘๋ณต๊ฐ’์€ ์ค‘๋ณต๋“ฑ์ˆ˜, ๋“ฑ์ˆ˜๊ฑด๋„ˆ๋œ€
  • dense_rank() : ์ค‘๋ณต๊ฐ’์€ ์ค‘๋ณต๋“ฑ์ˆ˜, ๋“ฑ์ˆ˜ ์•ˆ ๊ฑด๋„ˆ๋œ€, ๋™์ผ๋“ฑ์ˆ˜ ์ˆœ์œ„์— ์˜ํ–ฅ์ด ์—†๋‹ค
  • row_number() : ์ค‘๋ณต๊ฐ’์ด ์žˆ์–ด๋„ ๊ณ ์œ  ๋“ฑ์ˆ˜ ๋ถ€์—ฌ

10. ์กฐ์ธ

1) NL ์กฐ์ธ

  • ์ข์€๋ฒ”์œ„์— ์œ ๋ฆฌ
  • ์œ ๋ฆฌ์ˆœ์ฐจ์  ์ฒ˜๋ฆฌํ•˜๋ฉฐ, random access์œ„์ฃผ
  • ํ›„ํ–‰ํ…Œ์ด๋ธ”์—๋Š” ์กฐ์ธ์„ ์œ„ํ•œ ์ธ๋ฑ์Šค๊ฐ€ ์ƒ์„ฑ๋˜์–ด์•ผ ํ•จ

 

2) sort merge join

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

 

3) hash

  • ๋Œ€๋Ÿ‰์ด ๋ฐ์ดํ„ฐ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๊ณ , ์ฟผ๋ฆฌ์ˆ˜ํ–‰ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ๋Œ€์šฉ๋Ÿ‰์— ์ ํ•ฉ
  • nl๊ณผ sort์˜ ์กฐ์ธ๋ฌธ์ œ์  ํ•ด๊ฒฐ
  • ์˜ค๋ฒ„ํ—ค๋“œ ๋ฐœ์ƒ ๊ฐ€๋Šฅ