- 도서 테이블(도서번호, 도서이름, 출판사, 가격)
- 고객 테이블(고객번호, 고객이름, 주소, 전화)
- 주문 테이블(주문번호, 고객번호, 도서번호, 구매가격, 구매일)
create table book (
bookid number primary key,
bookname varchar2(50),
publicsher varchar2(50),
price number
);
create table customer (
custid number primary key,
name varchar2(50),
address varchar2(50),
phone varchar2(50)
);
create table orders (
orderid number primary key,
custid number references customer(custid),
bookid number references book(bookid),
saleprice number,
orderdate date
);
custid number references customer(custid);
desc book;
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, '역도 단계별 기술', '굿스포츠', 6000);
insert into book values(7, '야구의 추억', '이상미디어', 20000);
insert into book values(8, '야구를 부탁해', '이상미디어', 13000);
insert into book values(9, '올림픽 이야기', '삼성당', 7500);
insert into book values(10, 'Olympic Chapions', 'Pearson', 13000);
drop table 테이블명;
예시) 도서이름에 '축구'가 포함된 출판사를 검색
select publicsher from book where bookname like '%축구%';
예시) 도서이름의 왼쪽 두 번째 위치에 '구'라는 문자열을 갖는 도서를 검색
select * from book where bookname like '_구%';
예시) [and] 축구에 관한 도서 중 가격이 20,000원 이상인 도서를 검색
select * from book where bookname like '%축구%' and price >= 20000;
예시) [or] 출판사가 '굿스포츠' 혹은 '대한미디어'인 도서를 검색
select * from book where publicsher = '굿스포츠' or publicsher = '대한미디어';
order by 컬럼명 [asc, desc];
예시) 도서를 이름순으로 검색
select * from book order by bookname;
예시) 모든 도서의 정보를 가격이 높은순으로 정렬하여 출력. 단 가격이 동일할 때는 도서명 순으로 정렬
select * from book order by price desc, bookname;
예시) '굿스포츠'나 '대한미디어'나 '이상미디어'에서 출판하는 도서 중에 가격이 7000원 이상인 도서의 정보를 출력하시오. 단, 가격이 높은순으로 출력하되 가격이 동일할 때는 도서명 순으로 출력
select * from book
where publicsher in ('굿스포츠', '대한미디어', '이상미디어') and price >= 7000
order by price desc, bookname;
예시) 고객이 주문한 도서의 총 판매액을 구하시오.
select SUM(saleprice) from orders;
select SUM(saleprice) AS 총매출 from orders; //as 키워드는 생략해도 된다.
예시) 이상미디어에서 출간하는 도서의 수를 출력
select count(bookname) from book where publicsher = '이상미디어';
예시) 고객이 주문한 도서의 총 판매액, 평균값, 최저가, 최고가 출력
select sum(saleprice) Total,
avg(saleprice) Average,
min(saleprice) Minimum,
max(saleprice) Maximum from orders;
예시) 마당서점의 도서 판매 건수를 구하시오.
select count(*) from orders; //모든 도서의 판매 건수
예시) 총 주문건수와 총 주문금액을 출력
select count(*), sum(saleprice) from orders;
select custid, count(*), sum(saleprice) from orders; // 집계함수의 결과는 한 건이기 때문에 다른 컬럼을 함께 출력할 수 없다!
예시) 고객 아이디별로 총 주문건수와 총 주문금액을 출력 ==> 한 건이 아니라 고객 아이디의 수 만큼 출력, 집계함수와 함께 group by 절을 사용
select custid, count(*), sum(saleprice) from orders group by custid;
--> 집계함수를 사용할 때에 group by 절에 나타난 컬럼만이 select절에 올 수 있다.
예시) 구매 가격이 8000원 이상 구매가격에 대하여 구매한 정보에 대하여 고객 아이디별로 총 구매 건수를 출력하세요. 단, 총 구매건수가 2건 이상인 것만 출력
select custid, count(*) from orders
where saleprice >= 8000
group by custid having count(*) >= 2;
(1) select bookname from book where bookid = 1;
(2) select bookname from book where price >= 20000;
(3) select sum(saleprice) from orders where custid = 1;
(4) select count(*) from orders where custid = 1;
(1) select count(*) from book;
(2) select count(distinct publicsher) from book;
(3) select name, address from customer;
(4) select orderid from orders where orderdate between '2022/04/04' and '2022/04/07';
select orderid from orders where orderdate >= '2022/04/04' and orderdate <= '2022/04/07';
(5) select orderid from orders where orderdate not between '2022/04/04' and '2022/04/07';
select orderid from orders where orderdate < '2022/04/04' or orderdate > '2022/04/07';
(6) select name, address from customer where name like '김%';
(7) select name, address from customer where name like '김_아';