SQL 23, 24

이재훈·2024년 2월 10일
1

Zerobase_DA3_SQL

목록 보기
13/16

Aggregate Functions (집계함수)

여러칼럼 혹은 테이블 전체 칼럼으로부터 하나의 결과값을 반환하는 함수
COUNT : 총 갯수를 계산해주는 함수
SUM : 합계를 계산해 주는 함수
AVG : 평균을 계산해 주는 함수
MIN : 가장 작은 값을 찾는 함수
MAX : 가장 큰 값을 찾는 함수
FIRST : 첫번째 결과값을 리턴하는 함수
LAST : 마지막 결과값을 리턴하는 함수

COUNT

COUNT SELECT COUNT(column)
FROM tablename
WHERE condition;

SELECT COUNT(*) FROM crime_status; 데이터 값이 몇개인지 카운트

예제1) crime_stsatus에서 경찰서는 총 몇곳인지?

1번. SELECT COUNT(distinct police_station) FROM crime_status;
2번. SELECT distinct police_station FROM crime_status;

위 두 명령어의 차이는?

1번은 테이블에 있는 경찰서의 갯수를 보여주고, 2번은 테이블에 있는 경찰서의 목록을 보여준다.

SUM

예제1 ) 범죄 총 발생건수는?
select SUM(case_number) from crime_status where status_type = "발생";

예제2) 살인의 총 발생건수는?

select SUM(case_number) from crime_status where crime_stype = "살인" AND status_type = "발생";

예제3) 중부 경찰서에서 검거된 총 범죄 건수는 ?

select SUM(case_number) from crime_status where police_station = "중부" AND status_type = "검거";

문제1) police_station에서 경찰서는 총 몇개이고, 각각 경찰서 이름은 무엇인지 확인하세요.

select count(distinct name) from police_station;
select distinct name from police_station;

문제2) crime_status애서 status_type은 총 몇개이고, 각각 타입은 무엇인지 확인하세요.

select count(status_type) from crime_status;
select distinct status_type from crime_status;

문제3) 종로경찰서와 남대문경찰서의 강도 발생건수의 합을 구하세요.

select SUM(case_number) from crime_status where
crime_stype = "강도" AND
(police_station = "종로" OR police_station = "남대문");

문제4) 폭력 범죄의 검거 건수의 합을 구하세요.

select SUM(case_number) from crime_status
where crime_stype = "폭력" AND
status_type = "검거" ;

AVG

숫자칼럼의 평균을 계산해주는 함수

SELECT AVG(column)
FROM tablenamse
WHERE condition;

예제1) 평균 폭력 검거 건수는?

select AVG(case_number)
from crime_status
where crime_stype = "폭력" AND status_type = "검거";

예제2) 중부경찰서 범죄 평균 발생 건수

select AVG(case_number) from crime_status
where police_station = "중부" AND status_type ="발생";

MIN

숫자 칼럼 중 가장 작은 값을 찾아주는 함수

SELECT MIN(column)
FROM tablename
WHERE condition

예제1) 강도 발생건수가 가장 적은 경우 몇건?

SELECT MIN(case_number)
FROM crime_status
WHERE crime_stype = "강도" AND status_type = "발생";

예제2) 중부경찰서에서 가장 낮은 검거 건수는?

SELECT MIN(case_number)
FROM crime_status
WHERE police_station = "중부" AND status_type = "검거";

MAX

숫자 칼럼 중 가장 큰 값을 찾아주는 함수

SELECT MAX(column)
FROM tablename
WHERE condition

예제1) 살인이 가장 많이 검거된 건수는?

select MAX(case_number)
from crime_status
where crime_stype = "살인" AND status_type = "검거";

예제2) 강남경찰서에서 가장 많이 발생한 건수는?

select MAX(case_number)
from crime_status
where police_station = "강남" AND status_type = "발생";

문제1) 살인의 평균 발생 건수를 검색하고 확인
select AVG(case_number) from crime_status
where crime_stype = "살인" AND status_type = "발생";

문제2) 서초경찰서의 범죄별 평균 검거건수를 검색하고 확인
select AVG(case_number) FROM crime_status
where police_station = "서초" AND status_type = "검거";
select police_station, crime_stype,status_type,case_number from crime_status
where police_station = "서초" AND status_type = "검거";

문제3) 구로경찰서와 도봉경찰서의 평균 살인 검거건수를 검색하고 확인
select AVG(case_number) from crime_status
where (police_station = "구로" OR police_station = "도봉") AND (crime_stype = "살인" AND status_type = "검거");
select * from crime_status where (police_station = "구로" OR police_station = "도봉") AND
(crime_stype = "살인" AND status_type = " 검거 ");

문제4) 광진경찰서에서 가장 낮은 범죄 검거건수를 검색하고 확인
select MIN(case_number) from crime_status
where police_station = "광진";
select * from crime_status where police_station = "광진";

문제5) 성북경찰서에서 가장 낮은 범죄 발생건수를 검색하고 확인
select MIN(case_number) from crime_status where police_station = "성북";
select * from crime_status where police_station = "성북";

문제6) 영등포경찰서의 가장 높은 범죄 발생 건수를 검색하고 확인
select MAX(case_number) from crime_status where police_station = "영등포";
select * from crime_status where police_station = "영등포";

문제7) 절도검거가 가장 많은 건수를 검색하고 확인
select MAX(case_number) from crime_status where crime_stype = "절도" AND status_type = "검거";
select * from crime_status where crime_stype = "절도" AND status_type = "검거";

profile
💻 To be a Data analyst

0개의 댓글