create table book( bookid int primary key, bookname varchar(100), publisher varchar(100), price int);
create table orders( orderid int primary key, custid int, bookid int, saleprice int, orderdate date);
create table customer( custid int primary key, name varchar(100), address varchar(100), phone varchar(100));
alter table orders add constraint fk_bookid foreign key(bookid) references book(bookid);
alter table orders add constraint fk_custid foreign key(custid) references customer(custid);
INSERT INTO BOOK VALUES (1, '컴퓨터의 역사', '아이티', 7000);
INSERT INTO BOOK VALUES (2, '컴퓨터는 무엇일까', '굿북', 13000);
INSERT INTO BOOK VALUES (3, '컴퓨터의 이해', '비트아이티', 22000);
INSERT INTO BOOK VALUES (4, '자바 이론', '비트아이티', 35000);
INSERT INTO BOOK VALUES (5, '데이터베이스 교본', '아이티', 8000);
INSERT INTO BOOK VALUES (6, 'HTML 기술', '아이티', 6000);
INSERT INTO BOOK VALUES (7, '스프링의 개념', '에비씨미디어', 20000);
INSERT INTO BOOK VALUES (8, '스프링을 부탁해', '에비씨빛미디어', 13000);
INSERT INTO BOOK VALUES (9, '파이썬 이야기', '코딩빔', 7500);
INSERT INTO BOOK VALUES (10, 'python story', 'Pearson', 13000);
INSERT INTO CUSTOMER VALUES (1, '박건우', '인천시 송도', '010-1111-2222');
INSERT INTO CUSTOMER VALUES (2, '김선해', '서울시 종로구', '010-2111-2222');
INSERT INTO CUSTOMER VALUES (3, '장지혜', '서울시 용산구', '010-3111-2222');
INSERT INTO CUSTOMER VALUES (4, '추오섭', '경기도 의정부', '010-4111-2222');
INSERT INTO CUSTOMER VALUES (5, '박승철', '서울시 마포구', NULL);
INSERT INTO ORDERS VALUES (1, 1, 1, 6000, '2022-05-01');
INSERT INTO ORDERS VALUES (2, 1, 3, 21000, '2022-05-03');
INSERT INTO ORDERS VALUES (3, 2, 5, 8000, '2022-05-03');
INSERT INTO ORDERS VALUES (4, 3, 6, 6000, '2022-05-04');
INSERT INTO ORDERS VALUES (5, 4, 7, 20000, '2022-05-05');
INSERT INTO ORDERS VALUES (6, 1, 2, 12000, '2022-05-07');
INSERT INTO ORDERS VALUES (7, 4, 8, 13000, '2022-05-07');
INSERT INTO ORDERS VALUES (8, 3, 10, 12000, '2022-05-08');
INSERT INTO ORDERS VALUES (9, 2, 10, 7000, '2022-05-09');
INSERT INTO ORDERS VALUES (10, 3, 8, 13000, '2022-05-10');
-- 1. 김선해 고객의 전화번호를 찾아라.
select phone from customer where name='김선해';
-- 2. 모든 도서의 이름과 가격을 검색해라.
select bookname, price from book;
-- 3. 모든 도서의 도서번호, 도서이름, 출판사, 가격을 검색해라.
select * from book;
-- 4. 도서 테이블에 있느느 모든 출판사를 검색해라. (중복제거해서)
select distinct(publisher) from book;
-- 5. 가격이 20000원 미만인 도서를 검색해라.
select bookid, bookname from book where price<20000;
-- 6. 가격이 10000원 이상 20000 이하인 도서를 검색해라.
select bookid, bookname from book where price between 10000 and 20000;
-- 7. 출판사가 ‘아이티’ 혹은 ‘비트아이티’ 인 도서를 검색해라.
select bookid, bookname from book where publisher in('아이티','비트아이티');
-- 8. 출판사가 ‘아이티’ 혹은 ‘비트아이티’ 아닌 도서를 검색해라.
select bookid, bookname from book where publisher not in('아이티','비트아이티');
-- 9. ‘컴퓨터의 역사’를 출간한 출판사를 검색해라.
select publisher from book where bookname='컴퓨터의 역사';
-- 10. 도서이름에 ‘컴퓨터’가 포함된 출판사를 검색해라.
select publisher from book where bookname like'%컴퓨터%';
-- 11. 도서이름의 왼쪽 두번 째에 ‘퓨’라는 문자열을 갖는 도서를 검색해라.
select bookid, bookname from book where bookname like'_퓨%';
-- 12. 컴퓨터에 관한 도서 중 가격이 20000원 이상인 도서를 검색해라.
select bookid, bookname from book where bookname like'%컴퓨터%' and price>=20000;
-- 13. 도서를 이름순으로 검색해라.
select *from book order by bookname;
-- 14. 도서를 가격순으로 검색하고, 가격이 같으면 이름순으로 검색해라.
select *from book order by price, bookname;
-- 15. 도서를 가격의 내림차순으로 검색해라. 가격이 같다면 출판사의 오름차순으로 검색해라.
select from book order by price desc, publisher;
-- 16. 고객이 주문한 도서의 총 판매액을 구해라.
select sum(saleprice) from orders;
-- 17. 2번 김선해 고객이 주문한 도서의 총 판매액을 구해라.
select sum(saleprice) from orders where custid=2;
-- 18. 고객이 주문한 도서의 총 판매액, 평균값, 최저가, 최고가를 구해라.
select sum(saleprice),avg(saleprice),min(saleprice),max(saleprice) from orders;
-- 19. 23서점의 도서 판매 건수를 구해라.
select count() from orders;
-- 20. 고객별로 주문한 도서의 총 수량과 총 판매액을 구해라.
select name, count(o.custid), sum(saleprice) from orders o, customer c where o.custid = c.custid group by name;
-- 21. 가격이 8000원 이상인 도서를 구매한 고객에 대하여 고객별 주문 도서의 총 수량을 구해라. 단, 두 권 이상 구매한 고객만 구해라.
select name, c.custid, count(*) as totalbuying from orders o, customer c where o.custid = c.custid and saleprice>=8000 group by custid having count(*)>=2;
----------------------------------------------
SELECT
custid, COUNT(*) AS BUY
FROM
(SELECT
ord.custid,
ord.bookid,
ord.saleprice,
bk.bookname,
bk.price,
cust.name
FROM
orders ord, book bk, customer cust
WHERE
ord.bookid = bk.bookid
AND ord.custid = cust.custid
AND bk.price >= 8000) AA
GROUP BY custid
HAVING COUNT(*) >= 2;
-- 22. 고객과 고객의 주문에 관한 데이터를 모두 보여라.
select *from customer c, orders o where c.custid=o.custid;
-- 23. 고객과 고객의 주문에 관한 데이터를 고객번호 순으로 정렬하여 보여라.
select *from customer c, orders o where c.custid=o.custid order by c.custid;
-- 24. 고객의 이름과 고객이 주문한 도서의 판매가격을 검색해라.
select name, saleprice from customer c , orders o where c.custid=o.custid;
-- 25. 고객별로 주문한 모든 도서의 총 판매액을 구하고, 고객별로 정렬해라.
select c.custid, name, sum(saleprice) from customer c , orders o where c.custid=o.custid group by c.custid order by c.custid;
-- 26. 고객의 이름과 고객이 주문한 도서의 이름을 구해라.
select name, bookname, b.bookid from orders o, customer c, book b where c.custid=o.custid and o.bookid=b.bookid;
-- 27. 가격이 20000원인 도서를 주문한 고객의 이름과 도서의 이름을 구해라.
select name, bookname from orders o, customer c, book b where c.custid=o.custid and o.bookid=b.bookid and price=20000;
-- 28. 도서를 구매하지 않은 고객을 포함하여 고객의 이름과 고객이 주문한 도서의 판매가격을 구해라.
select [c.name](<http://c.name/>), o.saleprice, c.custid, b.price from customer c left outer join orders o on c.custid=o.custid left outer join book b on o.bookid=b.bookid;
-- 29. 가장 비싼 도서의 이름을 구해라.
select bookname, price from book where price=(select max(price) from book);
-- 30. 도서를 구매한 적이 있는 고객의 이름을 검색해라.
select distinct(name) from orders o, customer c where c.custid=o.custid;
-- 31. 비트아이티에서 출판한 도서를 구매한 고객의 이름을 보여라.
select name,bookname,publisher from book b, customer c, orders o where c.custid=o.custid and b.bookid=o.bookid and publisher='비트아이티';
-- 32. 출판사별로 출판사의 평균 도서 가격보다 비싼 도서를 구해라.
select bookname, price from book where avg(price) group by publisher;
--------------------------------------------------------------------------------
select b.bookname, b.price, b_avg.publisher, b_avg.avg_price from book b,
(select publisher, avg(price) as avg_price from book
group by publisher) b_avg
where b.publisher=b_avg.publisher
and b.price > b_avg.avg_price;
-- 33. Book테이블에 새로운 도서 ‘공학 도서’를 삽입해라. 공학 도서는 더샘에서 출간했으며 가격을 40000원이다.
insert into book values(11,'공학 도서','더샘',40000);
-- 34. Book테이블에 새로운 도서 ‘공학 도서’를 삽입해라. 공학 도서는 더샘에서 출간했으며 가격은 미정이다.
insert into book values(12,'공학 도서','더샘',null);
-- 35. Customer테이블에서 고객번호가 5인 고객의 주소를 ‘서울시 서초구’로 변경해라.
update customer set address='서울시 서초구' where custid=5;
-- 36. Customer테이블에서 박승철 고객의 주소를 김선해 고객의 주소로 변경해라.
update customer set address='서울시 종로구' where name='박승철';
--------------------------------------------------------------------
update customer as cust
join customer as cust2 on cust2.name='김선해'
set cust.address=cust2.address
where cust.name='박승철';
-- 37. 아이티에서 출판한 도서의 제목과 제목의 글자수를 확인해라.
select bookname, char_length(bookname) from book where publisher='아이티';
-- 38. 23서점의 고객 중에서 같은 성(이름 성)을 가진 사람이 몇 명이나 되는지 성별 인원수를 구해라.
select substr(name, 1,1),count(*) from customer group by substr(name,1,1);
-----------------------------------------------------------------------------------------------------
select Familyname, count(*) from (select substr(name,1,1) as Familyname from customer) A group by Familyname;
-- 39. 23서점은 주문일로부터 10일 후 매출을 확정한다. 각 주문의 확정일자를 구해라.
select orderdate, date_add(orderdate, interval 10 day) as'확정일자' from orders ;
-----------------------------------------------------------------------------------------
select orderdate, (orderdate+10)as confirmdate from orders;
-- 40. 23서점이 2022년 5월 7일에 주문받은 도서의 주문번호, 주문일, 고객번호, 도서번호를 모두 보여라. 주문일은 ‘yyyy-mm-dd요일’형태로 표시한다.
select orderid, orderdate, custid, bookid from orders where orderdate='2022-05-07';
------------------------------------------------------------------------------------------
select orderid, date_format(orderdate, '%y-%m-%d-%w요일'), custid, bookid from orders where orderdate='2022-05-07';
-- 41. 이름, 전화번호가 포함된 고객목록을 보여라. 단, 전화번호가 없는 고객은 ‘연락처없음’으로 표시해라.
select name, ifnull(phone,'연락처없음') from customer;
-- 42. 평균 주문금액 이하의 주문에 대해 주문번호와 금액을 보여라.
select orderid, saleprice from orders where saleprice<=(select avg(saleprice)from orders);
-- 43. 각 고객의 평균 주문금액보다 큰 금액의 주문 내역에 대해 주문번호, 고객번호, 금액을 보여라.
select orderid, custid, saleprice from orders where saleprice > all(select avg(saleprice)from orders group by custid);
-- 44. 서울시에 거주하는 고객에게 판매한 도서의 총판매액을 구해라.
select sum(saleprice) from orders o, customer c where o.custid=c.custid and address like '서울%';
----------------------------------------------------------------------------------------------------------------
select sum(saleprice) from orders where custid in(select custid from customer where address like '서울%');
-- 45. Customer테이블에서 고객번호가 5인 고객을 삭제해라.
delete from customer where custid=5;