Aggregate Functions (집계함수) 중 COUNT
와 SUM
은 여기서 자세히 알아볼 수 있다.
AVG
는 숫자 column의 평균을 계산해주는 함수다.
AVG 문법
SELECT AVG(column) FROM tablename WHERE condition;
Example 1: 평균 폭력 검거 건수를 구하기
select avg(case_number) from crime_status
where crime_type='폭력' and status_type='검거';
select police_station, crime_type, status_type, case_number
from crime_status
where crime_type='폭력' and status_type='검거';
Example 2: 중부경찰서 범죄 평균 발생 건수를 구하기
select avg(case_number)
from crime_status
where police_station='중부' and status_type='발생';
select police_station, crime_type, status_type, case_number
from crime_status
where police_station='중부' and status_type='발생';
MIN
은 숫자 column 중 가장 작은 값을 찾아주는 함수다.
MIN 문법
SELECT MIN(column) FROM table WHERE condition;
Example 1: 강도 발생 건수가 가장 적은 경우 몇건인지 찾기
select min(case_number)
from crime_status
where crime_type='강도' and status_type='발생';
select police_station, crime_type, status_type, case_number
from crime_status
where crime_type='강도' and status_type='발생';
Example 2: 중부경찰서에서 가장 낮은 검거 건수 찾기
select min(case_study)
from crime_status
where police_station='중부' and status_type='검거';
select police_station, crime_type, status_type, case_number
from crime_status
where police_station='중부' and status_type='검거';
MAX
는 숫자 column 중 가장 큰 값을 찾아주는 함수다.
MAX 문법
SELECT MAX(column) FROM tablename WHERE condition;
Example 1: 살인이 가장 많이 검거된 건수 찾기
select max(case_number)
from crime_status
where crime_type='살인' and status_type='검거';
select police_station, crime_type, status_type, case_number
from crime_status
where crime_type='살인' and status_type='검거';
Example 2: 강남 경찰서에서 가장 많이 발생한 범죄 건수 찾기
select max(case_number)
from crime_status
where police_station='강남' and status_type='발생';
select police_station, crime_type, status_type, case_number
from crime_status
where police_station='강남' and status_type='발생';