select
where
group by
order by
쿼리가 실행되는 순서: select * from 이름 → group by → select → order by
ex) select * from users
group by name
select name from users
group by name
select name, count(*) from users
group by name
ex2) 성이 신씨인 고객의 수를 알고싶다
select * from users
group by name
select name from users
group by name
select name, count(*) as cnt from users
group by name
이렇게 하면 성씨별로 고객의 수를 알 수 있음 (이미 성으로 분류가 되었기 때문)
최소값
select 범주가 담긴 필드명, min(최솟값을 알고 싶은 필드명) from 테이블명 group by 범주가 담긴 필드명;
최대값
select 범주가 담긴 필드명, max(최대값을 알고 싶은 필드명) from 테이블명 group by 범주가 담긴 필드명;
평균
select 범주가 담긴 필드명, avg(평균값을 알고 싶은 필드명) from 테이블명 group by 범주가 담긴 필드명;
동일범주 합계
select 범주가 담긴 필드명, sum(합계값을 알고 싶은 필드명) from 테이블명 group by 범주가 담긴 필드명;
select name, count() from users
group by name
order by count() desc;
desc는 내림차순이며 없을 시는 자동으로 오름차순 (굳이 넣고싶다면 asc를 넣으면 됨)
Where와 함께 쓸 때
select payment_method, count(*) from orders
where course_title = “웹개발 종합반”
group by payment_method;