๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ‡ฉ๐Ÿ‡ฆ๐Ÿ‡น๐Ÿ‡ฆ๐Ÿ‡ง๐Ÿ‡ฆ๐Ÿ‡ธ๐Ÿ‡ช

SQL ๋ฐ์ดํ„ฐ ์กฐ์ž‘ ์–ธ์–ด(DML), ์ง‘ํ•ฉ์—ฐ์‚ฐ, ์™ธ๋ถ€์กฐ์ธ, ์ง‘๊ณ„ํ•จ์ˆ˜, group by, having

<์ง‘ํ•ฉ ์—ฐ์‚ฐ>

๊ด€๊ณ„๋Œ€์ˆ˜์˜ ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ธ ํ•ฉ์ง‘ํ•ฉ, ๊ต์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ์— ํ•ด๋‹นํ•˜๋Š” ์—ฐ์‚ฐ์ž

- union

- intersect

- minus

โ€‹

ํ˜•์‹ : <select๋ฌธ-1> <์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž> <select๋ฌธ-2>

์กฐ๊ฑด : <select๋ฌธ-1>๊ณผ <select๋ฌธ-2>์—์„œ ์„ ํƒ๋œ ํ•„๋“œ์˜ ๊ฐœ์ˆ˜์™€ ๋Œ€์‘๋˜๋Š” ํ•„๋“œ์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ์„œ๋กœ ๊ฐ™์•„์•ผํ•จ

โ€‹

UNION

์˜ˆ) ๋ชจ๋“  ํ•™์ƒ์˜ ์ด๋ฆ„๊ณผ ๊ต์ˆ˜์˜ ์ด๋ฆ„์„ ํ•จ๊ป˜ ์ถœ๋ ฅ

select name from student
union
select name from professor

union ์—ฐ์‚ฐ์ž๋Š” ์—ฐ์‚ฐ ๊ฒฐ๊ณผ์— ํฌํ•จ๋œ ์ค‘๋ณต๋œ ๋ ˆ์ฝ”๋“œ๋ฅผ ์ œ๊ฑฐํ•จ

โœ” ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ณ  ์‹ถ์ง€ ์•Š๋‹ค๋ฉด union all ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉ

โ€‹

INTERSECT

์˜ˆ) ์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ ํ•™์ƒ๋“ค ์ค‘ ์ตœ์†Œ ํ•œ ๊ณผ๋ชฉ์—์„œ 'A+' ํ•™์ ์„ ๋ฐ›์€ ํ•™์ƒ๋“ค์˜ ํ•™๋ฒˆ์„ ๊ฒ€์ƒ‰

select  s.stu_id
from    student s, department d, takes t
where   s.dept_id = d.dept_id and s.stu_id = t.stu_id and d.dept_name ='์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ' and t.grade = 'A+'

 

'์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ'์— ๋‹ค๋‹ˆ๋Š” ํ•™์ƒ๋“ค์˜ ํ•™๋ฒˆ๊ณผ takes ํ…Œ์ด๋ธ”์—์„œ ํ•™์ ์ด 'A+'์ธ ํ•™์ƒ๋“ค์˜ ํ•™๋ฒˆ์˜ ๊ต์ง‘ํ•ฉ์„ ๊ตฌํ•จ

select  stu_id
from    student s, department d
where   s.dept_id = d.dept_id and d.dept_name = '์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ'
intersect
select  stu_id from takes where grade = 'A+'

 

MINUS

์˜ˆ) ์‚ฐ์—…๊ณตํ•™๊ณผ ํ•™์ƒ๋“ค ์ค‘์—์„œ ํ•œ๋ฒˆ๋„ 'A+'๋ฅผ ๋ฐ›์ง€ ๋ชปํ•œ ํ•™์ƒ๋“ค์˜ ํ•™๋ฒˆ์„ ๊ฒ€์ƒ‰

select stu_id from student s, department d
where  s.dept_id = d.dept_id and dept_name = '์‚ฐ์—…๊ณตํ•™๊ณผ'
minus
select stu_id from takes
where  grade = 'A+'

 

์™ธ๋ถ€ ์กฐ์ธ(outer join)

์˜ˆ) ๋ชจ๋“  ๊ต๊ณผ๋ชฉ์— ๋Œ€ํ•ด ๊ต๊ณผ๋ชฉ๋ช…, ํ•™์ ์ˆ˜, ๊ฐœ์„ค๋…„๋„, ๊ฐœ์„คํ•™๊ธฐ๋ฅผ ๊ฒ€์ƒ‰

select  title, credit, year, semester
from    course, class
where   course.course_id = class.course_id

 

์™ผ์ชฝ ์™ธ๋ถ€ ์กฐ์ธ(left outer join)

- ์—ฐ์‚ฐ์ž์˜ ์™ผ์ชฝ์— ์œ„์น˜ํ•œ ํ…Œ์ด๋ธ”์˜ ๊ฐ ๋ ˆ์ฝ”๋“œ์— ๋Œ€ํ•ด์„œ ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์— ์กฐ์ธ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ์—๋„ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์— ํฌํ•จ

โœ” ๊ฒฐ๊ณผ ๋ ˆ์ฝ”๋“œ์—์„œ ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋‚˜๋จธ์ง€ ํ•„๋“œ์—๋Š” null ์ด ์‚ฝ์ž…๋จ

โ€‹

์˜ค๋ฅธ์ชฝ ์™ธ๋ถ€ ์กฐ์ธ์€ ์™ผ์ชฝ ์™ธ๋ถ€ ์กฐ์ธ๊ณผ ๋ฐ˜๋Œ€

โ€‹

์™„์ „ ์™ธ๋ถ€ ์กฐ์ธ(full outer join)

- ์–‘์ชฝ ํ…Œ์ด๋ธ”์—์„œ ์„œ๋กœ ์ผ์น˜ํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ, ํ•ด๋‹น ๋ ˆ์ฝ”๋“œ๋“ค๋„ ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ”์— ํฌํ•จ์‹œํ‚ค๋ฉฐ, ์ด๋•Œ ๋‚˜๋จธ์ง€ ํ•„๋“œ์— ๋Œ€ํ•ด์„œ๋Š” ๋ชจ๋‘ null ์„ ์‚ฝ์ž…

์˜ˆ)

select  title, credit, year, semester
from    course full outer join class
using   (course_id)

 

์ง‘๊ณ„ ํ•จ์ˆ˜

 

COUNT

ํ˜•์‹ : count(<ํ•„๋“œ ์ด๋ฆ„>) or count(distinct<ํ•„๋“œ ์ด๋ฆ„>)

- ํ•ด๋‹น ํ•„๋“œ์— ๊ฐ’์ด ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€ ์ถœ๋ ฅ

- distinct ์‚ฌ์šฉ ์‹œ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐ

- <ํ•„๋“œ ์ด๋ฆ„>์— '*'๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐ

โ€‹

์˜ˆ) 3ํ•™๋…„ ํ•™์ƒ์ด ๋ช‡ ๋ช…์ธ์ง€ ์ถœ๋ ฅ

select  count(*)
from    student
where   year = 3

์˜ˆ) ์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ์˜ ํ•™์ƒ ์ˆ˜๋ฅผ ์ถœ๋ ฅ

select  count(*)
from    student s, department d
where   s.dept_id = d.dept_id and d.dept_name = '์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ'

 

SUM

ํ˜•์‹ : sum (<ํ•„๋“œ ์ด๋ฆ„>)

- ์ง€์ •๋œ ํ•„๋“œ์— ์ €์žฅ๋œ ๊ฐ’์˜ ํ•ฉ์„ ๊ณ„์‚ฐ

โ€‹

์˜ˆ) ์ „์ฒด ๊ต์ˆ˜๋“ค์˜ ์žฌ์ง ์—ฐ์ˆ˜์˜ ํ•ฉ

select  sum(2019 - year_emp)
from    professor

 

AVG

ํ˜•์‹: avg(<ํ•„๋“œ ์ด๋ฆ„>)

- ์ง€์ •๋œ ํ•„๋“œ์— ์ €์žฅ๋œ ๊ฐ’๋“ค์˜ ํ‰๊ท ์„ ๊ณ„์‚ฐ

โ€‹

์˜ˆ) ์ „์ฒด ๊ต์ˆ˜์˜ ํ‰๊ท  ์žฌ์ง ์—ฐ์ˆ˜๋ฅผ ์ถœ๋ ฅ

select  avg(2019 - year_emp)
from    professor

 

MIN, MAX

ํ˜•์‹ : max(<ํ•„๋“œ ์ด๋ฆ„>) , min(<ํ•„๋“œ ์ด๋ฆ„>)

- ์ง€์ •๋œ ํ•„๋“œ ๊ฐ’๋“ค ์ค‘ ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•จ

โ€‹

์˜ˆ) 'ACCOUNTING' ๋ถ€์„œ์— ์†ํ•œ ์ง์›๋“ค ์ค‘ ์ตœ๋Œ€ ๊ธ‰์—ฌ๊ฐ€ ์–ผ๋งˆ์ธ์ง€ ์ถœ๋ ฅ

select  max(sal)
from    emp e, dept d
where   e.deptno = d.deptno and dname = 'ACCOUNTING'

 

โ• select ์ ˆ์— ์ง‘๊ณ„ ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋  ๊ฒฝ์šฐ ๋‹ค๋ฅธ ํ•„๋“œ๋Š” ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ

โ€‹

GROUP BY

ํ˜•์‹ : group by <ํ•„๋“œ ๋ฆฌ์ŠคํŠธ>

- group by ์ ˆ์€ select ๋ฌธ์—์„œ where ์ ˆ ๋‹ค์Œ์— ์œ„์น˜

- group by ์ ˆ์— ์ง€์ •๋œ ํ•„๋“œ์˜ ๊ฐ’์ด ๊ฐ™์€ ๋ ˆ์ฝ”๋“œ๋“ค๋ผ๋ฆฌ ๊ทธ๋ฃน์„ ์ง€์–ด ๊ฐ ๊ทธ๋ฃน๋ณ„๋กœ ์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ

โ€‹

์˜ˆ) ํ•™๊ณผ(๋ฒˆํ˜ธ)๋ณ„๋กœ ์†Œ์† ํ•™์ƒ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ์ถœ๋ ฅ

select dept_id, count(*)
from   student
group by dept_id

โ• group by ์ ˆ์— ํฌํ•จ๋˜์ง€ ์•Š์€ ํ•„๋“œ๋Š” select ์ ˆ์—๋„ ํฌํ•จ๋  ์ˆ˜ ์—†์Œ

โ€‹

์˜ˆ) ํ•™๊ณผ ๋ฒˆํ˜ธ ๋Œ€์‹  ํ•™๊ณผ ์ด๋ฆ„์ด ์ถœ๋ ฅ๋˜๋„๋ก ์ˆ˜์ •

select dept_name, count(*)
from   student s, department d
where  s.dept_id = d.dept_id
group by dept_name

 

HAVING

- ๊ทธ๋ฃน์— ๋Œ€ํ•œ ์กฐ๊ฑด์„ ๋ช…์‹œํ•  ๋•Œ ์‚ฌ์šฉ

- ๊ทธ๋ฃน์— ๋Œ€ํ•œ ์กฐ๊ฑด์€ where ์ ˆ์—์„œ ํ‘œํ˜„ํ•˜์ง€ ๋ชปํ•˜๋ฏ€๋กœ having ์ ˆ์„ ์ด์šฉํ•ด์•ผํ•จ

ํ˜•์‹ : having <์ง‘๊ณ„ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์กฐ๊ฑด>

โ€‹

์˜ˆ) ์ง์› ์ˆซ์ž๊ฐ€ 5๋ช… ์ด์ƒ์ธ ๋ถ€์„œ์— ๋Œ€ํ•ด ๋ถ€์„œ๋ณ„ ์ง์›์ˆ˜, ํ‰๊ท  ๊ธ‰์—ฌ, ์ตœ๋Œ€ ๊ธ‰์—ฌ, ์ตœ์†Œ ๊ธ‰์—ฌ๋ฅผ ์ถœ๋ ฅ

select  dname, count(*), avg(sal), max(sal), min(sal)
from    emp e, dept d
where   e.deptno = d.deptno
group by dname
having  count(*) >= 5

 

where ์ ˆ๊ณผ having ์ ˆ, group by์ ˆ์„ ๋ชจ๋‘ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

1. from์ ˆ์— ๋ช…์‹œ๋œ ํ…Œ์ด๋ธ”๋“ค์„ ์กฐ์ธ

2. where์ ˆ์— ๋ช…์‹œ๋œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๋“ค์„ ๊ฒ€์ƒ‰

3. group by์ ˆ์— ๋ช…์‹œ๋œ ํ•„๋“œ์˜ ๊ฐ’์ด ๊ฐ™์€ ๋ ˆ์ฝ”๋“œ๋“ค๋ผ๋ฆฌ ๊ทธ๋ฃน์„ ์ง€์–ด ์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ์ ์šฉ

4. ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ทธ ์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ ๊ฒฐ๊ณผ๋“ค ์ค‘์—์„œ having ์ ˆ์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒฐ๊ณผ๋งŒ ์„ ํƒ

5. select์ ˆ์— ์ง€์ •๋œ ํ•„๋“œ ๋ฐ ์ง‘๊ณ„ ํ•จ์ˆ˜ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ ์ถœ๋ ฅ

โ€‹

null์˜ ์ฒ˜๋ฆฌ

ํ˜•์‹: <ํ•„๋“œ ์ด๋ฆ„> is null, <ํ•„๋“œ ์ด๋ฆ„> is not null