
15$가 넘는 메뉴를 대문자로 조회
select ucase(menu) from sandwich where price>15;
5$가 안되는 메뉴 소문자로 조회
select lcase(menu) from sandwich where price<5;
select mid(string,start_position,length);
- string : 원본 문자열
- start : 문자열 반환 시작 위치 (첫글자를 1, 마지막 글자는 -1)
- length : 반환할 문자열 길이
1번위치에서 4글자 조회
select mid('this is mid test',1,4);
11위 카페 이름 중 두번째 단어만 조회 - 6번 위치에서 4글자
select mid(cafe,6,4) from sandwich where rangkin=11;
select length('this is len test');
select round(number,decimals_place);
- number : 반올림 대상
- decimals : 반올림 할 소수점 위치 (option) 위치 지정 안할 시 소수점 (0)에서 반올림
1단위 위치는 -1
sandwich 테이블에서 소수점 자리는 반올림 해서 1달러 단위까지만 표시
select rand,price,round(price) from sandwich order by rand desc limit 3;

select now();
select format(number, decimal_place);
- number : 숫자
- decimals : 표시할 소수점 위치
select format(12345.6789,0);

oil_price 테이블에서 가격이 백원단위에서 반올림 했을 때 2000원 이상인 경우 천원 단위 콤마 넣어서 조회
select format(가격,0) from oil_price where round(가격,-3) >=2000;
select column1,(select column2 from table2 where condition)
from table1
where condition;
서울 은평경찰서의 강도 검거 건수와 서울시 경찰서 전체의 평균 강도 검거 건수 조회
select case_number,(select avg(case_number) from crime_status where crime_stype like '강도' and status_type like '검거') avg
from crime_status
where police_station like '은평' and crime_stype like '강도' and status_type like '검거';

select a.column,b.column
from table1 a, (select column1,column2 from table2) b
where condition;
경찰서 별로 가장 많이 발생한 범죄 건수와 범죄 유형을 조회
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;

select column_names
from table_name
where column_name=(select column_name from table_name where conditon)
order by column_name;
celeb 에서 snl_show 에서 host와 이름이 같은 값 찾기
select name from celeb where name=(select host from snl_show);

select name from celeb where name=(select host from snl_show where id=1);

2-1. multiple row + IN : 서브 쿼리 결과 중에 포함 될 때
select col
from table
where col IN (select col from table where condition) order by col;
snl에 출연한 영화배우 조회
select host
from snl_show
where host IN (select name from celeb where JOB_TITLE like '%영화배우%');

2-2. multiple row + exists : 서브 쿼리 결과에 값이 있으면 반환
select col from table
where exists (select col from table where condition)
order by col;
범죄 검거 혹은 발생 건수가 2000건 보다 큰 경찰서 조회
select name
from police_station p
where exists (select police_station from crime_status c where p.name=c.reference and case_number>2000);

2-3 multiple row + Any : 서브쿼리 결과 중 최소한 하나라도 만족하면
select col from table
where col = any (select col from table where condition) order by col;
snl에 출연한 적이 있는 연예인 이름 조회
select name from celeb
where name=any(select host from snl_show);

2-4 multiple row + ALL : 서브 쿼리 결과를 모두 만족하면 (비교연산자 사용)
select col from table
where col=all(select col from table where condition) order by col;
snl에 출연한 적이 있는 연예인 이름 조회
select name from celeb
where name=all(select host from snl_show where id=1);

select col
from table a
where (a.column1,a.column2,..) IN (select b.column1,b.column2,... from table b where a.column_name = b.column_name) order by col;
강동원과 성별, 소속사가 같은 연예인의 이름,성별,소속사를 조회
select name,sex,agency
from celeb
where (sex,agency) IN (select sex,agency from celeb where name='강동원');

select max(가격),(select avg(가격) from oil_price where 셀프='Y')
from oil_price
where 상표='SK에너지'
select o.상호,o.상표,s.max_price from oil_price o, (select 상표,max(가격) max price from oil_price group by 상표) s where o.상표=s.상표 and o.가격=s.max_price;
select 상호,가격
from oil_price
where 가격> (select avg(가격) from oil_price);
select 이름,주유소,주유일 from refueling where 주유소 IN (select 상호 from oil_price where 가격>(select avg(가격) from oil_price));
select r.이름,o.상호,o.상표,r.금액,o.가격
from oil_price o, (select 이름,주유소,금액 from refueling where 금액>=100000) r
where o.상호=r.주유소;