Function | Description |
---|---|
COUNT | 총 개수를 계산해주는 함수 |
SUM | 합계를 계산해주는 함수 |
AVG | 평균을 계산해주는 함수 |
MIN | 가장 작은 값을 찾아주는 함수 |
MAX | 가장 큰 값을 찾아주는 함수 |
FIRST | 첫 번째 결과값을 리턴하는 함수 |
LAST | 마지막 결과값을 리턴하는 함수 |
SELECT COUNT(column)
FROM tablename
WHERE conditon;
SELECT COUNT(*)
FROM police_station;
SELECT COUNT(DISTINCT police_station)
FROM crime_station;
SELECT COUNT(DISTINCT crime_type)
FROM crime_status;
SELECT SUM(column)
FROM tablename
WHERE condition;
SELECT SUM(case_number)
FROM crime_status
WHERE status_type='발생';
SELECT SUM(case_numbr)
FROM crime_status
WHERE status_type='발생' AND crime_type LIKE '살인';
SELECT AVG(column)
FROM tablename
WHERE condition;
평균 폭력 검거 건수는?
SELECT AVG(case_number)
FROM crime_status
WHERE crime_type LIKE '폭력' AND status_type='검거';
SELECT police_station, crime_type, status_type, case_number
FROM crime_status
WHERE crime_type LIKE '폭력' AND status_type='검거';
SELECT MIN(column)
FROM table
WHERE condition;
SELECT MIN(case_number)
FROM crime_status
WHERE crime_type LIKE '강도' AND status_type='발생;
SELECT MAX(column)
FROM table
WHERE condition;
SELECT MAX(case_number)
FROM crime_status
WHERE crime_type LIKE '살인' AND status_type LIKE '검거';
SELECT column2, column2, ...
FROM table
WHERE condition
GROUP BY column1, column2, ...
ORDER BY column1, column2, ...;
SELECT police_sation
FROM crime_status
GROUP BY police_station
ORDER BY police_station
LIMIT 5;
SELECT DISTINCT police_sation
FROM crime_status LIMIT 5;
SELECT police_station, sum(case_number) 발생건수
FROM crime_status
WHERE status_type LIKE '발생'
GROUP BY police_station
ORDER BY 발생건수 DESC
LIMIT 5;
SELECT column1, column2, ...
FROM table
WHERE condition
GROUP BY colunn1, column2, ...
HAVING condition (Aggregate Functions)
ORDER BY column1, column2, ...
SELECT police_sation, sum(case_number) couont
FROM crime_status
WHERE status_type LIKE '발생'
GROUP BY police_station
HAVING count > 4000;
SELECT police_station, AVG(case_number)
FROM crime_status
WHERE (crime_type LIKE '폭력' OR crime_type LIKE '절도')
AND status_type LIKE '발생'
GROUP BY police_station
HAVING AVG(case_number) >= 2000;