[SQL] Aggregate Function(집계함수)

김주하·2024년 3월 13일

집계함수

count : 갯수

select count(*) 
from police_station;

select count(distinct crime_type) 
from crime_status;

sum : 합

select sum(case_number) 
from crime_status 
where status_type='발생';

avg : 숫자컬럼의 평균 계산

select avg(case_number) 
from crime_status 
where status_type='발생' and police_station like '중부';

min : 가장 작은 값

select min(case_number) 
from crime_status 
where crime_type = '강도' and status_type = '발생';

max : 가장 큰 값

select max(case_number)
from crime_status
where crime_type = '살인';

group by : 그룹화하여 데이터 조회

select police_station, sum(case_number) 발생건수
from crime_status
where status_type = '발생'
group by police_station
order by 발생건수 desc;

having : 조건에 집계함수가 포함되는 경우 where 대신 having 사용

select police_station, sum(case_number) count
from crime_status
where status_type = '발생'
group by police_station
having count > 4000

0개의 댓글