Group by

그룹별 이라는 단어가 나오면 무조건 groupby 해주기

연습문제

SQL 연습문제 4-1

회원(users) 테이블에서 전체 유저의 평균연령을 조회하세요.

# 연습문제 4-1
select avg(age)
from `thelook_ecommerce.users`

SQL 연습문제 4-2

회원(users) 테이블에서 여성 유저의 평균연령을 조회하세요.

# 연습문제 4-2
select avg(age)
from `thelook_ecommerce.users`
where gender = 'F'

SQL 연습문제 4-3

회원(users) 테이블에서 국가별 가입자수를 조회하세요.

# 연습문제 4-3
select country, count(id)
from `thelook_ecommerce.users`
group by country

SQL 연습문제 4-4

회원(users) 테이블에서 남성 유저의 국가별 가입자 수를 조회하세요.

SELECT COUNTRY, COUNT(ID)
FROM `thelook_ecommerce.users`
WHERE gender = 'M'
GROUP BY COUNTRY

SQL 연습문제 4-5

회원(users) 테이블에서 가입기간(created_at)이 2020년도 1월인 유저의 국가별 가입자 수 (country_user_count)를 조회하세요.

SELECT country, COUNT(ID) AS country_user_count
FROM `thelook_ecommerce.users`
WHERE created_at >= '2020-01-01'
and created_at < '2020-02-01'
group by country
SELECT country, COUNT(ID) AS country_user_count
FROM `thelook_ecommerce.users`
WHERE created_at >= '2020-01-01'
and created_at < '2020-02-01'
group by country