extract(추출항목 from 데이터) : 연도(YEAR), 월(MONTH), 일(DAY), 시간(HOUR), 분(MINUTE), 초(SECOND)
select * from product where made between to_date('20190101000000', 'YYYYMMDDHH24MISS') and to_date('20191231235959', 'YYYYMMDDHH24MISS');
-- 또는select*from product where extract(yearfrom made) =2019;
날짜는 문자열이 아니기 때문에 문자열처럼 사용하면 위험하다.
제조일로부터의 유통기한이 6개월 이내인 제품을 조회하는 SQL
날짜간의 계산이 필요
날짜에서 +1은 1일 후를 의미
select*from product where expire - made <=180;
상반기에 제조한 상품 조회하는 SQL
select*from product where extract(monthfrom made) between1and6;
2020년 1월 1일부터 현재까지 제조한 상품 조회하는 SQL
select*from product where made between to_date('20200101000000', 'YYYYMMDDHH24MISS') andsysdate;
정렬 관련
정렬은 조건 등으로 추려진 데이터를 원하는 기준에 따라 나열하는 것을 말하며, 반드시 조건처럼 데이터 집합을 계산하는 명령보다 이후에 나와야 한다.
형식
select .... order by 항목 [asc]-- 또는select .... order by 항목 desc
예시
모든 상품을 가격순으로 정렬하는 SQL
select*from product order by price desc;
모든 상품을 제조일 최신순으로 정렬하는 SQL
select*from product order by made desc;
가격이 2000원 이상인 상품을 1차로 이름순, 2차로 제조일이 빠른 순으로 정렬하는 SQL
select*from product where price >=2000order bynameasc, made asc;-- 또는select*from product where price >=2000order byname, made;