|
|
박지성이 구매한 도서의 출판사 수 | select count(distinct publisher) from book, orders where book.bookid = orders.bookid and custid = (select custid from customer where name = '박지성'); |
박지성이 구매한 도서의 이름, 가격, 정가와 판매 가격의 차이 | select bookname, price, (price - saleprice) 가격차이 from book, orders where book.bookid = orders.bookid and custid = (select custid from customer where name = '박지성');
// abs 양수만 나오게 하는것 (음수나올경우를 대비해서) select bookname, price, abs(price-saleprice) 차이 from customer, book, orders where customer.custid = orders.custid and book.bookid = orders.bookid and name = '박지성'; |
박지성이 구매하지 않은 도서의 이름 | union 검색한 결과를 합치는 기능 minus 검색한 결과에서 빼는 기능
select bookname from book minus select bookname from book where bookid in (select bookid from orders where custid = (select custid from customer where name = '박지성')); |
마당 서점의 운영자와 경영자가 요구하는 다음 질문에 대한 SQL 문
주문하지 않은 고객의 이름(부속질의 사용) | select name from customer minus select name from customer where cu (서브쿼리이용한것) 주문한 고객의 id를 검색 select distinct custid from orders 본질의 select name from customer where custid notin (select distinct custid from orders); |
주문 금액의 총액과 주문의 평균 금액 | select sum(saleprice) 총액, avg(saleprice) 평균 from orders; |
고객의 이름과 고객별 구매액 | select name, sum(saleprice) 구매액 from customer, orders where customer.custid = orders.custid group by name; |
고객의 이름과 고객이 구매한 도서 목록 | select distinct customer.name, bookname from book, customer, orders where book.bookid = orders.bookid and customer.custid = orders.custid order by name; |
도서의 가격 (Book 테이블)과 판매가격 (Orders 테이블)의 차이가 가장 많은 주문 | select max(price-saleprice) from orders, book where orders.bookid = book.bookid
select * from orders, book where orders.bookid = book.bookid and (price-saleprice) = (차이많은거 구하는거);
select * from orders, book where orders.bookid = book.bookid and (price-saleprice) = (select max(price-saleprice) from orders, book where orders.bookid = book.bookid); |
도서의 판매액 평균보다 자신의 구매액 평균이 더 높은 고객의 이름 | 도서의 판매액 평균 // A select avg(saleprice) from orders; 고객별 평균 구매액 // B select name, avg(saleprice) from orders o, customer c where o.custid = c.custid 0 group by name; : B 중에 B의 avg 가 A보다 더 큰 것 형식 ) select name from (B) where B.avg > (A); 실제 ) select name from (select name, avg(saleprice) avg from orders o, customer c where o.custid = c.custid group by name) B where B.avg > (select avg(saleprice) from orders); |