
주요 학습내용
1. Aggregate Functions
2. Scalar Functions
3. Subquery

- 사용 databases
select count(distinct police_station) from crime_status
select count(distinct crime_stype) from crime_status;
select distinct crime_stype from crime_status
select sum(case_number) from crime_status where status_type like '발생';
select sum(case_number) from crime_status
where crime_stype = '살인' and status_type = '발생';





- distinct 사용하여 그룹화 한 경우, 정렬 불가능(order by)
조건에 집계함수가 포함되는 경우 where 대신 HAVING 사용
예제) crime_status에서 경찰서별로 총 발생 범죄 건수 검색
mysql>select police_station, sum(case_number) 발생건수 from crime_status
-> where status_type = '발생'
-> group by police_station
-> order by 발생건수 desc
-> limit 10;
mysql> select police_station, status_type, avg(case_number) 평균건수 from crime_status
-> group by police_station, status_type
-> limit 10;
mysql> select police_station, sum(case_number) 범죄건수
-> from crime_status
-> where status_type = '발생'
-> group by police_station
-> having 범죄건수 > 4000;

- 사용할 table
mysql> select ucase(menu), price from sandwich where price > 15;
mysql> select lcase(menu), price from sandwich where price > 15;
🤔 python slice랑 비슷함
mid(column, 시작번호, 길이)
예제) 11위 카페 이름 중 두번째 단어만 조회
# 1) ranking = 11 우선 조회
mysql> select cafe from sandwich where ranking = 11;
# 2) 두번째 단어만 조회
mysql> select mid(cafe, -4, 4) from sandwich where ranking=11;

mysql> select ranking, address, length(address) 주소길이 from sandwich
-> where ranking <= 3
-> order by ranking asc;
- 0: 소수점 첫째 자리
- 1: 소수점 둘째 자리
- -1 : 1의 자리
mysql> select ranking, cafe, menu, round(price, 0) from sandwich
-> order by ranking desc
-> limit 3;
select now();

- FORMAT으로 나온 값 => STRING
- 값 비교 해야할 경우, ROUND 사용

mysql> select 상호, format(가격, 0) from oil_price
-> where round(가격, -3) >= 2000;
🙌
- 하나의 SQL문 안에 포함되어 있는 또 다른 SQL문을 말함
- 메인쿼리가 서브퀴리를 포함하는 종속적인 관계이다
- 서브쿼리는 메인쿼리의 컬럼 사용 가능
- 메인쿼리는 서브쿼리의 컬럼 사용 불가
- Subquery는 괄호로 묶어서 사용
- 단일 행 혹은 복수 행 비교 연산자와 함께 사용 가능
- subquery에서는 order by를 사용 X
사용 TABLES
결과는 하나의 Column이어야 한다
예제) 서울은평경찰서의 강도 검거 건수와 서울시 경찰서 전체의 강도 검거 건수를 조회
mysql> select case_number,
-> (select avg(case_number) from crime_status
-> where crime_stype like '강도' and status_type = '검거') avg
-> from crime_status
-> where police_station like '은평' and crime_stype like '강도' and status_type = '검거';

mysql> select c.police_station, c.crime_stype, c.case_number
-> from crime_status c,
-> (select police_station, max(case_number) count from crime_status
-> where status_type like '발생' group by police_station) m
-> where c.police_station = m.police_station
-> and c.case_number = m.count;
사용 TABLE

mysql> select name from celeb where name = (select host from snl_show where id = 1);
하나 이상의 행을 검색하는 서브쿼리

mysql> select c.name from celeb as c
-> where c.name in (select s.host from snl_show as s where c.job_title like '%영화배우%')
-> order by c.name;

mysql> select name from police_station p
-> where exists
-> (select police_station from crime_status c where p.name = c.reference and case_number > 2000);

mysql> select name from celeb
-> where name = any(select host from snl_show);

mysql> SELECT name from celeb
-> where name = all(select host from snl_show where id = 1);

mysql> select name, sex, agency from celeb
-> where (sex, agency) in (select sex, agency from celeb where name = '강동원');