Join 과 Sub Query

강상은·2023년 12월 3일
0

Oracle

목록 보기
5/7

JOIN

  • from 절에 지정한 테이블에는 select 절의 열에 (as) 사용한 것 처럼 별칭을 지정할 수 있다.

select * from emp e, dept d where e.deptno = d.deptno;

select * from emp e, dept d where e.deptno = d.deptno order by e.empno;




JOIN을 이해하기 위해서 수기 작성...

외부조인 ( full, left, right )

outer(full,left,right) 조인 ((customer.custid = orders.custid 조건을 만족하지 않더라도 다 나와라)

  • 테이블에 조회는 되지만 데이터 값이 없는 항목까지 같이 검색할때 outer 조인을 사용
  • on : ~한 상태로 해라
  • 도서를 구매하지 않은 고객을 포함하여 고객의 이름과 고객이 주문한 도서의 판매 가격을 구하시오.

select * from customer full outer join orders on customer.custid = orders.custid; --default

select * from customer left outer join orders on customer.custid = orders.custid;

select * from customer right outer join orders on customer.custid = orders.custid;

inner 조인 (customer.custid = orders.custid 조건을 만족하는 행만 나와라)

select * from customer inner join orders on customer.custid = orders.custid;

SUB QUERY(부속 질의)

부속 질의란?

  • 하나의 sql문 안에 다른 sql문이 중첩된 nested 질의를 말함
  • 다른 테이블에서 가져온 데이터로 현재 테이블에 있는 정보를 찾거나 가공할 때 사용
  • 보통 데이터가 대량일 때 데이터를 모두 합쳐서 연산하는 조인보다 필요한 데이터만 찾아서 공급해주는 부속 질의가 성능이 더 좋음
  • 주질의(main query)와 부속질의(sub query)로 구성

join(테이블 합), subquery(부속 질의)

부속 질의 형태

select 필드 from 테이블 where 조건;

select 필드 from 테이블 where ();

select bookid from book where price >=20000;

sub query형식

select saleprice from orders where bookid in (select bookid from book where price >=20000);

-박지성(고객)의 총 구매(주문)액 (박지성의 고객번호는 1번으로 놓고 작성)

select * from customer, orders where customer.custid = orders.custid and customer.custid = 1;

select sum(saleprice) from customer, orders where customer.custid = orders.custid and customer.custid = 1;

0개의 댓글

관련 채용 정보