1. ์์ ๊ฐ์ releation schema๋ฅผ ๊ฐ๋ cust, book, orders ํ ์ด๋ธ์ ์์ฑํ๋ผ.
create table cust{
cid int primary key,
name varchar(20),
addr varchar(20),
hp varchar(20)};
create table book{
bid int primary key,
title varchar(20),
publisher varchar(20),
price int};
create table orders{ ##ํํ ์
๋ ฅํ๊ณ ์์ฑํด์ผ ํจ
oid int primary key,
cid int,
bid int,
odate varchar(20),
foreign key(cid) references cust(cid),
foreign key(bid) references book(bid)};
2. 3๊ฐ์ ํ ์ด๋ธ์ ์์ ๊ฐ์ด tuple์ ์ ๋ ฅํ๋ผ.
insert into cust values
(1,'hong','anyang','010-999-1111'),
(2,'kim','seoul','010-555-3456'),
(3,'kang','busan','010-333-7894'),
(4,'park','anyang','010-321-5678');
insert into book values
(1,'database','mac',34000),
(2,'java','hanbit',29000),
(3,'c++','abc',23000),
(4,'python','hanbit',41000),
(5,'data structure','mac',12000);
insert into book(bid,title,publisher) values (6,'network','abc') ##NULL์ ๋ฃ๊ณ ์ถ์ ๊ฒฝ์ฐ ์ด๋ ๊ฒ ์ง์ ํด์
set foreign_key_checks = 0;
insert into orders values
(1,1,1,'2020-03-15'),
(2,1,2,'2020-03-16'),
(3,1,3,'2020-04-15'),
(4,2,1,'2021-05-05'),
(5,2,2,'2021-05-06'),
(6,3,4,'2022-01-03'),
(7,100,4,'2022-03,05);
set foreign_key_checks = 1;
3. ๋ชจ๋ ๋์ ๋ชฉ๋ก์ ๊ฒ์ํ๋ผ.
select *
from book;
4. ‘anyang’ ์ฌ๋ ๊ณ ๊ฐ์ ๊ฒ์ํ๋ผ.
select *
from cust
where addr = 'anyang';
5. ๊ฐ๊ฒฉ์ด 3๋ง์ ์ด์ 4๋ง์ ์ดํ์ธ ๋์๋ฅผ ๊ฒ์ํ๋ผ.
select *
from book
where price >= 30000 and price <= 40000;
6. hanbit ์ถํ์ฌ์์ ์ถ๊ฐํ ์ฑ ์ค ๊ฐ๊ฒฉ์ด 4๋ง์ ์ด์์ธ ๋์๋ฅผ ๊ฒ์ํ๋ผ.
select *
from book
where publisher = 'hanbit' and price >= 40000;
7. ๋์๋ช ์ ‘data’๊ฐ ํฌํจ๋ ๋์ ๋ชฉ๋ก์ ๊ฒ์ํ๋ผ.
select *
from book
where title like '%data%';
8. ๋์๋ช ์ 2๋ฒ์งธ ๋ฌธ์๊ฐ ‘a’์ธ ๋์ ๋ชฉ๋ก์ ๊ฒ์ํ๋ผ.
select *
from book
where title like '_a%';
9. ๊ฐ๊ฒฉ์ด ์ ํด์ง์ง ์์ ๋์ ๋ชฉ๋ก์ ๋ณด์ฌ๋ผ.
select *
from book
where price is null;
10. ๊ณ ๊ฐ๋ช ์ผ๋ก ๋ด๋ฆผ์ฐจ์ํ์ฌ ๊ณ ๊ฐ๋ชฉ๋ก์ ๋ณด์ฌ๋ผ.
select *
from cust
order by name desc;
11. ๊ณ ๊ฐ์๋ก ๋ด๋ฆผ์ฐจ์, ๊ณ ๊ฐ๋ช ์ผ๋ก ์ค๋ฆ์ฐจ์ํ์ฌ ๊ณ ๊ฐ๋ชฉ๋ก์ ๋ณด์ฌ๋ผ.
select *
from cust
order by cid desc, name asc;
12. ์ฃผ์๋ณ ๊ฑฐ์ฃผํ๋ ๊ณ ๊ฐ ์๋ฅผ ๋ณด์ฌ๋ผ.
select addr, count(addr)
from cust
group by addr;
13. ๊ฑฐ์ฃผํ๋ ๊ณ ๊ฐ ์๊ฐ 2๋ช ์ด์์ธ ์ง์ญ๊ณผ ๊ณ ๊ฐ์๋ฅผ ๋ณด์ฌ๋ผ.
select addr, count(addr)
from cust
group by addr
having count(addr) >= 2;
14. ๊ฐ๊ฒฉ์ด 3๋ง์ ์ด์์ธ ์ฑ ์์ 2๊ถ ์ด์ ์ถ๊ฐํ ์ถํ์ฌ์ ์ฑ ์๋ฅผ ๋ณด์ฌ๋ผ.
select publisher, count(publisher)
from book
where price >= 30000
group by publisher
having count(publisher) >= 2;
15. cust, orders ํ ์ด๋ธ์ ์นดํฐ์ ํ๋ก๋ํธ๋ฅผ ์ํํ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ๋ผ.
select *
from cust, orders;
16. book,orders ํ ์ด๋ธ์ ์นดํฐ์ ํ๋ก๋ํธ๋ฅผ ์ํํ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ด๋ ๋์๋ช ์ผ๋ก ์ค๋ฆ์ฐจ์ํ์ฌ ๋ณด์ฌ๋ผ.
select *
from book, orders
order by title asc;
17. cust, orders, book ํ ์ด๋ธ์ ์นดํฐ์ ํ๋ก๋ํธ๋ฅผ ์ํํ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ๋ผ.
select *
from cust, book, orders;
18. ์ฃผ๋ฌธ๋ ๋์์ oid, title, odate๋ฅผ ๋ณด์ด์ธ์.
select oid, title, odate
from book, orders
where book.bid = orders.bid;
19. ์ฃผ๋ฌธํ ๊ณ ๊ฐ์ oid, name, addr์ ๋ณด์ด์ธ์.
select oid, name, addr
from cust, orders
where cust.cid = orders.cid;
20. ์์ ์ฌ๋ ๊ณ ๊ฐ์ ์ฃผ๋ฌธ๋ด์ญ(oid,name,title,addr,odate)๋ฅผ ๋ณด์ด์ธ์.
select oid, name, title, addr, odate
from cust, book, orders
where cust.cid = orders.cid and book.bid = orders.bid and addr = 'anyang';
21. ‘hanbit’ ์ถํ์ฌ์ ์ฃผ๋ฌธ๋ด์ญ(oid,name,title,addr,odate)์ ๋ณด์ด์ธ์.
select oid, name, title, addr, odate
from cust, book, orders
where cust.cid = orders.cid and book.bid = orders.bid and publisher = 'hanbit';
22. ์ฃผ๋ฌธํ ๊ณ ๊ฐ์ ์ฃผ๋ฌธ๋ด์ญ์ ๋ณด์ด๋, ์ฃผ๋ฌธํ์ง ์์ ๊ณ ๊ฐ์ ์ ๋ณด๋ ๋ณด์ด์ธ์.(oid,cid,name,odate)
select oid, cust.cid, name, odate
from cust left outer join orders on cust.cid = orders.cid;
23. ์ฃผ๋ฌธ๋ ์ฑ ์ ์ฃผ๋ฌธ๋ด์ญ์ ๋ณด์ด๋, ์ฃผ๋ฌธ๋์ง ์์ ์ฑ ์ ์ ๋ณด๋ ๋ณด์ด์ธ์.(oid,cid,title,odate)
select oid, book.bid, title, odate
from book left outer join orders on book.bid = orders.bid;
24. ์ฃผ๋ฌธํ ๊ณ ๊ฐ์ ์ฃผ๋ฌธ๋ด์ญ์ ๋ณด์ด๋ ํํดํ ๊ณ ๊ฐ์ ์ฃผ๋ฌธ๋ด์ญ๋ ๋ชจ๋ ๋ณด์ด์ธ์.(oid,cid,name,odate)
select oid, cust.cid, name, odate
from cust right outer join orders on cust.cid = orders.cid;
25. ์ฃผ๋ฌธํ ๊ณ ๊ฐ์ ์ฃผ๋ฌธ๋ด์ญ์ ๋ณด์ด๋, ์ฃผ๋ฌธํ์ง ์์ ๊ณ ๊ฐ์ ์ ๋ณด๋ ๋ณด์ด๊ณ , ํํดํ ๊ณ ๊ฐ์ ์ฃผ๋ฌธ๋ด์ญ๋ ๋ชจ๋ ๋ณด์ด์ธ์.(oid,cid,name,odate)
select oid, cid, name, odate
from cust left outer join orders on cust.cid = orders.cid
union
select oid, cid, name, odate
from cust right outer join orders on cust.cid = orders.cid;
1. ์ฃผ๋ฌธํ ๊ณ ๊ฐ์ name, addr์ ๋ณด์ด์์ค(๋ถ์์ง์)
select name, addr
from cust
where cid in (
select cid
from orders
where cust.cid = orders.cid);
2. ์ฃผ๋ฌธํ์ง ์์ ๊ณ ๊ฐ์ name, addr์ ๋ณด์ด์์ค(outer join, is null ์ฌ์ฉ)
select name, addr
from cust
where cid in (
select cid
from cust left outer join orders on cust.cid = orders.cid
where orders.cid is null);
3. ์ฃผ๋ฌธํ ๊ณ ๊ฐ์ name, addr์ ๋ณด์ด์์ค(์๊ด๋ถ์์ง์: exists ์ฌ์ฉ)
select name, addr
from cust
where exists (
select *
from orders
where cust.cid = orders.cid);
4. ์ฃผ๋ฌธํ์ง ์์ ๊ณ ๊ฐ์ name, addr์ ๋ณด์ด์์ค(์๊ด๋ถ์์ง์: not exists ์ฌ์ฉ)
select name, addr
from cust
where not exists (
select *
from orders
where cust.cid = orders.cid);