๐ธ๏ธ Group by
- ์ํ๋ ์ ํ๋ณ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ทธ๋ฃนํ ํด์ฃผ๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ค.
- ๋์ผํ ๋ฒ์ฃผ๋ฅผ ๊ฐ๋ ๋ฐ์ดํฐ๋ฅผ ํ๋๋ก ๋ฌถ์ด์, ๋ฒ์ฃผ๋ณ ํต๊ณ๋ฅผ ๋ด์ฃผ๋ ๊ฒ.
- ~๋ณ
๐โ๏ธ Group by๊ฐ ์คํ๋๋ ์์
from -> group by -> select
1) from orders: orders ํ
์ด๋ธ์ ๋ฐ์ดํฐ ์ ์ฒด๋ฅผ ๊ฐ์ ธ์จ๋ค.
2) group by payment_method: ํ
์ด๋ธ ๋ฐ์ดํฐ์์ ๊ฐ์ payment_method๋ฅผ ๊ฐ๋ ๋ฐ์ดํฐ ํฉ์ณ์ค๋ค.
3) select payment_method: payment_method ๋ณ๋ก ๊ทธ๋ฃนํ ๋ ๊ทธ๋ฃน๋ค์ ์ถ๋ ฅํด์ค๋ค.
- ๊ฒฐ์ ์๋จ๋ณ๋ก ๊ทธ๋ฃนํ ํด์ ๋ณด์ฌ์ค.
select payment_method from orders
group by payment_method;
- ๋์ผํ ๋ฒ์ฃผ์ ๊ฐ์ ๊ตฌํ๊ธฐ count(*)
select ๋ฒ์ฃผ๋ณ๋ก ์ธ์ด์ฃผ๊ณ ์ถ์ ํ๋๋ช
, count(*) from ํ
์ด๋ธ๋ช
group by ๋ฒ์ฃผ๋ณ๋ก ์ธ์ด์ฃผ๊ณ ์ถ์ ํ๋๋ช
;
- ๊ฒฐ์ ์๋จ๋ณ ์ฃผ๋ฌธ์ ์ดํฉ์ ๋ณด์ฌ์ค.
select payment_method, count(*) from orders
group by payment_method;
- ๋์ผํ ๋ฒ์ฃผ์์ ์ต๋ max(), ์ต์ min(), ํ๊ท avg(), ์ดํฉ sum() ๊ตฌํ๊ธฐ
- ์ฃผ์ฐจ๋ณ ์ข์์์ ์ต์, ์ต๋, ํ๊ท ๊ฐ๊ณผ ์ดํฉ์ ๋ณด์ฌ์ค
# ์ต์
select week, min(likes) from checkins
group by week
# ์ต๋
select week,max(likes) from checkins
group by week;
# ํ๊ท
select week, avg(likes) from checkins
group by week;
# ์ดํฉ
select week, sum(likes) from checkins
group by week;
๐งน Order by
- ๊น๋ํจ ์ ๋ ฌ์ด ํ์ํ ๋ ์ฌ์ฉ
- ๋ด๋ฆผ์ฐจ์, ์ค๋ฆ์ฐจ์ ๋ฑ
๐โ๏ธ Order by๊ฐ ์คํ๋๋ ์์
from -> group by -> select -> order by
1) from orders: orders ํ
์ด๋ธ์ ๋ฐ์ดํฐ ์ ์ฒด๋ฅผ ๊ฐ์ ธ์จ๋ค.
2) group by payment_method: ํ
์ด๋ธ ๋ฐ์ดํฐ์์ ๊ฐ์ payment_method๋ฅผ ๊ฐ๋ ๋ฐ์ดํฐ ํฉ์ณ์ค๋ค.
3) select payment_method: payment_method ๋ณ๋ก ๊ทธ๋ฃนํ ๋ ๊ทธ๋ฃน๋ค์ ์ถ๋ ฅํด์ค๋ค.
4) order by count(*): ํฉ์ณ์ง ๋ฐ์ดํฐ์ ๊ฐ์์ ๋ฐ๋ผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํด์ค๋ค.
# ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
select payment_method, count(*) from orders
group by payment_method
order by count(*)
- ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ desc
# ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
select payment_method, count(*) from orders
group by payment_method
order by count(*) desc;
๐ต๏ธ Where ์ ๊ณผ ํจ๊ป ์ฐ๊ธฐ
- '์น๊ฐ๋ฐ ์ข ํฉ๋ฐ'์ '๊ฒฐ์ ์๋จ๋ณ ์ฃผ๋ฌธ๊ฑด์' ์ธ์ด๋ณด๊ธฐ
๐โ๏ธ ์คํ๋๋ ์์
from -> where -> group by -> select -> order by
1) from orders: orders ํ
์ด๋ธ์ ๋ฐ์ดํฐ ์ ์ฒด๋ฅผ ๊ฐ์ ธ์จ๋ค.
2) where course_title='์น๊ฐ๋ฐ ์ข
ํฉ๋ฐ': ์น๊ฐ๋ฐ ์ข
ํฉ๋ฐ ๋ฐ์ดํฐ๋ง ๋จ๊ฒจ์ค๋ค.
3) group by payment_method: ํ
์ด๋ธ ๋ฐ์ดํฐ์์ ๊ฐ์ payment_method๋ฅผ ๊ฐ๋ ๋ฐ์ดํฐ ํฉ์ณ์ค๋ค.
4) select payment_method, count(*): payment_method ๋ณ๋ก ํฉ์ณ์ง ๋ฐ์ดํฐ๊ฐ ๊ฐ๊ฐ ๋ช ๊ฐ๊ฐ ํฉ์ณ์ง ๊ฒ์ธ์ง ์ธ์ด์ค๋ค.
5) order by count(*): ํฉ์ณ์ง ๋ฐ์ดํฐ์ ๊ฐ์์ ๋ฐ๋ผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํด์ค๋ค.
select payment_method, count(*) from orders
where course_title = '์น๊ฐ๋ฐ ์ข
ํฉ๋ฐ'
group by payment_method
order by count(*);
- '๋ค์ด๋ฒ ์ด๋ฉ์ผ ์ฌ์ฉ ์ ์ ' ์ค ์น๊ฐ๋ฐ ์ข ํฉ๋ฐ์ ์ ์ฒญํ ์ฃผ๋ฌธ์ ๊ฒฐ์ ์๋จ๋ณ ์ฃผ๋ฌธ ๊ฑด์์ ๊ฐ๋ค์ ๋ด๋ฆผ์ฐจ์์ผ๋ก ๋ณด์ฌ์ค.
1) from orders: orders ํ ์ด๋ธ์ ๋ฐ์ดํฐ ์ ์ฒด๋ฅผ ๊ฐ์ ธ์จ๋ค.
2) where email like '%naver.com' and course_title = '์น๊ฐ๋ฐ ์ข ํฉ๋ฐ'
: email์ 'naver.com' ํฌํจ๋๋ ๋ฐ์ดํฐ๋ค ์ค '์น๊ฐ๋ฐ ์ข
ํฉ๋ฐ' ๋ฐ์ดํฐ๋ง ๋จ๊ฒจ์ค๋ค.
3) group by payment_method: ํ
์ด๋ธ ๋ฐ์ดํฐ์์ ๊ฐ์ payment_method๋ฅผ ๊ฐ๋ ๋ฐ์ดํฐ ํฉ์ณ์ค๋ค.
4) select payment_method, count(*): payment_method ๋ณ๋ก ํฉ์ณ์ง ๋ฐ์ดํฐ๊ฐ ๊ฐ๊ฐ ๋ช ๊ฐ๊ฐ ํฉ์ณ์ง ๊ฒ์ธ์ง ์ธ์ด์ค๋ค.
5) order by count(*) desc: ํฉ์ณ์ง ๋ฐ์ดํฐ์ ๊ฐ์์ ๋ฐ๋ผ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํด์ค๋ค.
select payment_method, count(*) from orders
where email like '%naver.com' and course_title = '์น๊ฐ๋ฐ ์ข
ํฉ๋ฐ'
group by payment_method
order by count(*) desc;