ํน์ ํ๋๋ณ๋ก ๊ฐ์๋ฅผ ์กฐํํ๋ค.
select ํ๋, count(*) from ํ
์ด๋ธ
group by ํ๋
ex) group by + where
select ํ๋, count(*) from ํ
์ด๋ธ
where ์กฐ๊ฑดํ๋ = ์กฐ๊ฑด
group by ํ๋
์ฌ๋ฌ ์ง๊ณ์ฟผ๋ฆฌ์ ํฉ๊ป ์ฌ์ฉํ๋ค.
count
sum
min
max
avg
ex) ํ๋๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ (๊ธฐ๋ณธ๊ฐ= ์ค๋ฆ์ฐจ์ asc)
select * from ํ
์ด๋ธ
order by ํ๋
ex) ํ๋๋ณ๋ก ๋ฌถ์์ ๋ ๊ฐ์๋ฅผ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌํ๋ค.
select ํ๋, count(*) from ํ
์ด๋ธ
group by ํ๋
order by count(*) desc
select * from A_table a
left join B_table b
on a.user_id = b.user_id
select * from A_table a
[inner] join B_table b
on a.user_id = b.user_id
ex)
ordersํ
์ด๋ธ๊ณผ usersํ
์ด๋ธ์ ์ด์ฉํด์ ๋ค์ด๋ฒ ์ด๋ฉ์ผ์ ์ฌ์ฉํ๋ ์ ์ ์ ์ฑ์จ๋ณ ์ฃผ๋ฌธ๊ฑด์ ์กฐํ (user_id๋ฅผ fk๋ก ๊ฐ์ง๋ค.)
select u.name, count(*) as count from orders o
join users u
on o.user_id = u.user_id
where u.email like '%naver.com'
group by u.name;
ex)
๊ฒฐ์ ์๋จ ๋ณ user์ point ํ๊ท ๊ฐ ์กฐํํ๊ธฐ (์์์ 3๋ฒ์งธ๊น์ง ํ์)
(orders์ point_users ํ
์ด๋ธ ์ด์ฉ fk: user_id)
select o.payment_method, round(avg(pu.point),3) from orders o
join point_users pu
on o.user_id = pu.user_id
group by o.payment_method
ex)
course์ ์ด๋ฆ ๋ณ๋ก week ๋ณ ์ฒดํฌ์ธ ์ ์กฐํ
courses ํ
์ด๋ธ๊ณผ checkins ํ
์ด๋ธ ์ด์ฉ (fk: course_id)
[ 2๊ฐ ํ๋๋ก ๊ทธ๋ฃนํ ๋ฐ ์ ๋ ฌ ]
select c1.title, c2.week, count(*) as cnt
from courses c1
join checkins c2
on c1.course_id = c2.course_id
group by c1.title, c2.week
order by c1.title, c2.week
์ ์ฟผ๋ฆฌ์์ 8์ 1์ผ ์ดํ ์ฃผ๋ฌธํ ๊ณ ๊ฐ์ ๊ฐ์๋ง ์กฐํ ์ถ๊ฐ
์ด 3๊ฐ ํ
์ด๋ธ์ join (course, checkins, orders)
select c1.title, c2.week, count(*) as cnt
from courses c1
join checkins c2 on c1.course_id = c2.course_id
join orders o on c2.user_id = o.user_id
where o.created_at >= '2021-08-01'
group by c1.title, c2.week
order by c1.title, c2.week
select * from ํ
์ด๋ธ
where ํ๋ in (๊ฐ1, ๊ฐ2, ๊ฐ3)
select์ Subquery
select c.checkin_id,
c.user_id,
c.likes,
(
select avg(likes) from checkins
where user_id = c.user_id
)
from checkins c
Subquery๋ฅผ join์ ๋์์ผ๋ก ์ฌ์ฉํ๊ธฐ
[ user_id๋ณ ์ข์์๋ฅผ ๋ฐ์ ํ๊ท ์ ์กฐํํ๋ ์ฟผ๋ฆฌ ]
[ ์ด ์ฟผ๋ฆฌ์ ๊ฒฐ๊ณผ๋ฅผ join์ ๋์์ผ๋ก ์ฌ์ฉํ ์ ์๋ค. ]
select user_id, avg(likes) as avg_likes from checkins
group by user_id
select pu.user_id, pu.point, u.avg_likes
from point_users pu
join (
select user_id, avg(likes) as avg_likes from checkins
group by user_id
) as u
on pu.user_id = u.user_id
where์ ์ ์กฐ๊ฑด์ผ๋ก Subquery ์ฌ์ฉํ๊ธฐ
[ ์ ์ฒด point์ ํ๊ท ๋ณด๋ค ํฐ point๋ฅผ ๊ฐ์ง user๋ง ์กฐํ ]
select * from point_users pu
where pu.point > (
select avg(point) from point_users
)
[ ์ ์กฐ๊ฑด์์ ์ด์จ ์ฑ์ ๊ฐ์ง ์ ์ ๋ผ๋ ์กฐ๊ฑด์ ์ถ๊ฐ ]
[ join์ผ๋ก ํด๊ฒฐ ]
select * from point_users pu
where pu.point > (
select avg(point) from point_users pu
join users u
on pu.user_id = u.user_id
where u.name like '์ด%'
)
[ ์ ๋ฌธ์ ๋ฅผ in์ฟผ๋ฆฌ๋ฅผ ์ด์ฉ ]
[ ์๋ธ์ฟผ๋ฆฌ ์์ ๋ ์๋ธ์ฟผ๋ฆฌ๊ฐ ์๋ ํํ ]
select * from point_users pu
where pu.point > (
select avg(point) from point_users
where user_id in (
select user_id from users where name like '์ด%'
)
)
in ์ฟผ๋ฆฌ๋ฅผ ์ด์ฉํ Subquery
select * from ํ
์ด๋ธ1
where ํ๋ in (
select ํ๋ from ํ
์ด๋ธ2
where ์กฐ๊ฑดํ๋ = ์กฐ๊ฑด
)
with์ ์ฌ์ฉ
select *
from (
select ํ๋ from ํ
์ด๋ธ1
...
) a
join
(
select ํ๋ from ํ
์ด๋ธ2
...
) b on a.id = b.id
join
(
select ํ๋ from ํ
์ด๋ธ3
...
) c on a.id = c.id
์์ ๊ฐ์ด Subquery์ ๊ฒฐ๊ณผ๋ฅผ ๋ง์น ํ
์ด๋ธ์ฒ๋ผ ์ฌ์ฉํ๋ ๊ฒ์ with๋ฅผ ํตํด ๊ฐ๋ฅํ๋ค.
์๋ธ์ฟผ๋ฆฌ๋ฅผ ํ
์ด๋ธ์ฒ๋ผ ์ฌ์ฉํ๊ธฐ ์ํด with๋ฅผ ์ด์ฉํด์ ์์๋ก ์ด๋ฆ์ ๋ถ์ฌํ๋ ๋๋?
with tableA as (
select ํ๋ from ํ
์ด๋ธ1
...
), tableB as (
select ํ๋ from ํ
์ด๋ธ2
...
), tableC as (
select ํ๋ from ํ
์ด๋ธ3
...
)
select *
from tableA a
join tableB b on a.id = b.id
join tableC c on a.id = c.id
[ ๊น____๋ ]
์ค๋์ ๋ฝ์ธํธ๋ group by, join, subquery
์ค๋์ SQL์ ์ฌ๊ธฐ๊น์ง