-- 전체테이블 확인
select * from book;
select * from customer;
select * from orders;



-- 도서번호가 1인 도서의 이름
select bookname from book where bookid = 1;

-- 가격이 20,000원 이상인 도서의 이름
select bookname from book where price >= 20000;

-- 박지성의 총구매액
select sum(saleprice) from customer, orders where customer.custid=orders.custid and customer.name like '박지성';

-- 박지성이 구매한 도서의 수
select count(*) from customer, orders where customer.custid=orders.custid and customer.name like '박지성';

-- 마당서점 도서의 총 개수
select count(*) from book;

-- 마당서점에 도서를 출고하는 출판사의 총개수
select count(distinct publisher) from book ;

-- 모든 고객의 이름, 주소
select name,address from customer;

-- 2024년 7월4일~ 7월7일 사이에 주문받은 도서의 주문번호
select orderid from orders where orderdate between '20240704' and '20240707';

-- 2024년 7월4일~7월7일 사이에 주문받은 도서를 제외한 도서의 주문번호
select orderid from orders where orderdate not between '2024-07-04' and '2024-07-07';
select orderid from orders where orderdate not between str_to_date('20240704','%Y%m%d') and str_to_date('20240707','%Y%m%d');

-- 성이 "김" 씨인 고객의 이름과 주소
select name,address from customer where name like '김%'

-- 성이 "김" 씨이고 이름이 "아"로 끝나는 고객의 이름과 주소
select name,address from customer where name like '김%아';

-- 박지성이 구매한 도서의 출판사 수
select count(distinct publisher) from book,customer,orders where customer.custid=orders.custid and orders.bookid=book.bookid and customer.name like '박지성';

-- 박지성이 구매한 도서와 이름, 가격, 정가와 판매가격의 차이
select bookname,price,price-saleprice from book,customer,orders where customer.custid=orders.custid and orders.bookid=book.bookid and customer.name like '박지성';

-- 박지성이 구매하지 않은 도서의 이름
select bookname from book b1 where not exists (select bookname from customer, orders where orders.bookid=b1.bookid and orders.custid=customer.custid and customer.name like '박지성');

-- 주문하지 않은 고객의 이름(부속질의 사용)
select name from customer where name not in (select name from customer, orders where customer.custid=orders.custid);

-- 주문 금액의 총액과 주문의 평균 금액
select sum(saleprice) 총액, avg(saleprice) 평균총액 from orders;

-- 고객의 이름과 고객별 구매액
select name, sum(saleprice) from customer, orders where customer.custid=orders.custid group by name;

-- 고객의 이름과 고객이 구매한 도서 목록
select name,book.bookname from customer, orders, book where customer.custid=orders.custid and orders.bookid=book.bookid;

-- 도서의 가격(Book 테이블)과 판매가격(orders 테이블) 의 차이가 가장 많은 주문
select * from book,orders where book.bookid=orders.bookid and price-saleprice=(select max(price-saleprice) from book, orders where book.bookid=orders.bookid);

-- 도서의 판매액 평균보다 자신의 구매액 평균이 더 높은 고객의 이름
select name, avg(saleprice) from customer, orders where customer.custid=orders.custid group by name having avg(saleprice) > (select avg(saleprice) from orders);

1안
-- 두 개 이상의 서로 다른 출판사에서 도서를 구매한 고객의 이름
select c.name, count(distinct publisher)
from customer c, orders o, book b
where c.custid=o.custid and o.bookid=b.bookid
group by c.name
having count(distinct publisher) >= 2;
2안
-- 두 개 이상의 서로 다른 출판사에서 도서를 구매한 고객의 이름
select c.name, count(distinct publisher)
from customer c
join orders o on c.custid=o.custid
join book b on o.bookid=b.bookid
group by c.name
having count(distinct publisher) >=2;
