Coding/데이터베이스

[오라클] 마당서점 복습하기2 : 조인함수,서브쿼리사용

찡콩찡 2022. 9. 5. 15:06
--1. 고객별로 주문한 모든 도서의 총 판매액을 구하고, 고객별로 정렬하시오
select name,sum(saleprice)
from customer, orders
where customer.custid =orders.custid
group by customer.name
order by customer.name;

--2.고객의 이름과 고객이 주문한 도서의 이름을 구하시오
select name,bookname
from customer c,book b, orders o 
where c.custid=o.custid
and b.bookid=o.bookid;



select name,bookname
from customer natural join orders natural join book;

select name,bookname 
from customer join orders using(custid) join book using(bookid);

select name,bookname
from customer c join orders o on(c.custid=o.custid)
join book b on(o.bookid=b.bookid);

--3. 고객의 이름, 주문한 도서의 이름, 단 도서 가격이 15000원 이상인 것
select name, bookname, price 
from customer natural join orders natural join book
where price>=15000;

--4.판매된 도서 이름과 고객 이름 주문한 날짜
select bookname,name,orderdate
from orders natural join book  natural join customer;


--5.판매되지 않은 도서이름
select bookname
from book
where bookid not in(select bookid 
                    from orders);
                    
select bookname
from book
where bookid in (select bookid from book
                minus select bookid from orders);

--6.도서를 구매한 고객의 이름과 도서명
select name,bookname
from customer natural join orders book natural join book;

--7.도서를 구매하지 않은 고객의 이름
select name 
from customer
where custid not in (select custid from orders);

--8.도서를 구매하지 않은 고객을 포함하여 고객의 이름과 그 고객이 주문한 도서의 판매가격
select name,saleprice
from orders o right outer join customer c
on c.custid=o.custid;

--9.판매하지 않은 도서까지 포함해서 도서명과 주문된 도서의 정가
select bookname,price
from book b left outer join orders o on b.bookid=o.bookid;

--10.가장 싼 도서
select bookname
from book
where price=(select min(price)from book);

--11.가장 비싼 도서
select bookname 
from book
where price=(select max(price)from book);

--12.판매된 도서 중 판매가가 가장 싼 도서
--1)먼저 판매가가 가장 작은 것의 판매가
select min(saleprice) from orders;
--2)이 판매가격에 해당되는 도서번호 찾기
select bookid 
from orders
where saleprice = (select min(saleprice) from orders);
--3)해당되는 도서명 찾기
select bookname 
from book 
where bookid in (select bookid 
                  from orders
                  where saleprice = (select min(saleprice) 
                  from orders));

--13.제일 많이 팔린 도서명 
--1) 도서별 팔린 갯수
select max(count(*)) 
from orders
group by bookid;

--2)최고 많이 팔린 도서번호와 팔린 권수 찾기
select bookid, count(*)
from orders
group by bookid
having count(*) = (select max(count(*))
                   from orders
                   group by bookid);
                   
--14. 제일 많이 팔린 도서명, 판매권수
select bookname, count(*)
from book natural join orders 
group by bookname
having count(*) = (select max(count(*))
                   from orders
                  group by bookid);