select 컬럼1, 컬럼2
from 테이블1 left outer join 테이블2
on 조건식;
예시) 고객의 이름과 고객이 주문한 도서의 판매가격을 출력. 주문내역이 없는 고객의 이름도 출력
select name, saleprice
from customer left outer join outers
on customer.custid = outers.custid;
select 컬럼1, 컬럼2
from 테이블1 right outer join 테이블2
on 조건식;
예시) 고객의 이름과 고객이 주문한 도서의 판매가격을 출력. 주문내역이 없는 고객의 이름도 출력
select name, saleprice
from outers right outer join customer
on customer.custid = outers.custid;
nvl(컬럼이름, 대체할 값)
예시) 출판사 별로 총 판매한 도서의 수, 총 판매 금액을 출력. 단, 판매되지 않는 출판사도 포함
select publicsher, count(saleprice), nvl(sum(saleprice), 0)
from book left outer join orders
on book.bookid = orders.bookid
group by publicsher;
예시) 부서 별로 근무자의 수, 평균급여를 출력하세요. 근무자가 없는 부서명도 출력하세요.
select dname, count(eno), nvl(avg(salary), 0)
from dept d left outer join emp e
on d.dno = e.dno
group by dname;
예시) 모든 직원의 이름과 관리자 이름을 출력
select 사원.ename ename, 관리자.ename mname
from emp 사원, emp 관리자
where 사원.mgr = 관리자.eno;
예시) 고객명과 출판사명을 입력받아 해당 고객에 해당 출판사에 출간하는 도서의 구매내역을 출력합니다. 고객번호, 고객명, 출판사명, 도서명, 도서가격, 구매가격, 구매일을 출력. 단, 최근의 구매일 순으로 출력하고 동일할 때에는 도서명 순으로 출력
select c.custid, name, publicsher, b.bookname, price, saleprice, orderdate
from customer c, book b, orders o
where c.custid = o.custid and b.bookid = o.bookid and name = '박지성' and publicsher = '굿스포츠'
order by orderdate desc, b.bookname;
예시) 도서를 구매한 적이 있는 고객의 이름을 출력
<서브쿼리>
select name
from customer
where custid in (select custid from orders);
<조인>
select distinct name
from customer c, orders o
where c.custid = o.custid;
예시) 평균급여보다 더 높은 급여를 받는 직원들의 이름, 급여를 출력하세요.
select ename, salary
from emp
where salary > (select avg(salary) from emp);
예시) '대한미디어'에서 출간하는 도서를 구매한 적이 있는 고객의 이름을 출력
select name
from customer
where custid in ( select custid
from orders
where bookid in ( select bookid
from book
where publicsher = '대한미디어'));
select 컬럼1, 컬럼2,...
from 테이블1
(select 컬럼3 from 테이블2 where 테이블1.컬럼 = 테이블2.컬럼5);
예시) 출판사별로 출판사의 평균 도서 가격보다 더 비싼 도서 이름을 출력
select b1.bookname
from book b1
where b1.price > (select avg(b2.price)
from book b2
where b1.publicsher = b2.publicsher );
예시) 주문하지 않는 고객의 이름을 출력
select name from customer
minus
select name from customer where custid in (select distinct custid from orders);
select 컬럼1, 컬럼2
from 테이블1
(select 컬럼3 from 테이블2 where 테이블1.컬럼1 = 테이블2.컬럼4);
예시) 연습) 주문이 있는 고객이름과 주소를 출력
select name, address
from customer c
where exists (select * from orders o
where c.custid = o.custid);
NAME ADDRESS
---------- --------------------
박지성 영국 맨체스타
김연아 대한민국 서울
장미란 대한민국 강원도
추신수 미국 클리블랜드
박세리 대한민국 대전
(1) select count(distinct publicsher)
from book
where bookid in (select bookid
from orders
where custid = (select custid
from customer
where name = '박지성'));
(2) select bookname, price, (price - saleprice) as 차이
from book b, customer c, orders o
where name = '박지성' and
b.bookid = o.bookid and c.custid = o.custid;
(3) select bookname
from book
where bookid not in (select bookid
from orders
where custid = (select custid
from customer
where name = '박지성'));