## 조인
create table dept
as
select * from hr.dept;
create table locations
as
select * from hr.locations;
create table job_grades
as
select * from hr.job_grades;
--------------------------------------------------------------------
## 조인
- 테이블을 옆으로 합치는 것
- 공통된 컬럼을 조인
- 오라클 전용 구문, ansi 표준 구문
## 조인의 종류
1. cross join
2. equi join
3. non-equi join
4. outer join
5. self join
1. cross join
=> 조인조건 없음
=> 모든 경우의 수 조회 = 조인테이블1 행수 * 조인테이블2 행수
2. equi-join
=> 조인조건 있음
=> 조인테이블1.컬럼명 = 조인테이블명2.컬럼명
=> 조인조건에 맞는 레코드만 조회 : 조인테이블에 모두 존재해야만 조회 대상
3. outer-join
=> 조인조건 있음
=> 조인테이블1.컬럼명 = 조인테이블명2.컬럼명(+)
=> 조인조건에서 누락된 레코드도 조회 대상
=> 데이터가 존재하지 않는 조인측에 (+) 기호를 표기
=> outer-join시에는 null 데이터로 간주
4. self-join
=> 같은 테이블 참조
=> 테이블에 대한 알리아스를 지정해서 어떤 목적의 테이블인지 명시적 지정
5. non equi-join
=> 조인조건이 '=' 연산자가 아닌 조인
## 오라클 구문
## 조인 조건 where 절 기술
## equi join
사번, 이름 부서명
select *
from emp, dept
where emp.department_id = dept.department_id ;
select EMPLOYEE_ID, last_name, department_name
from emp, dept
where emp.department_id = dept.department_id ;
-- 테이블의 별칭 사용시 as 쓰면 안된다!
select e.EMPLOYEE_ID, e.last_name, d.department_name
from emp e, dept d
where e.department_id = d.department_id ;
## 표준 구문
## 조인조건을 from 절 기술
## equi join
사번, 이름 부서명
- 두 테이블에 동일한 컬럼이 있는 경우
select e.EMPLOYEE_ID, e.last_name, d.department_name
from emp e join dept d using ( department_id ) ;
- 두 테이블에 동일한 컬럼이 있는 경우
select e.EMPLOYEE_ID, e.last_name, d.department_name
from emp e join dept d on ( e.department_id = d.department_id ) ;
## 오라클 구문
## outer join : left outer join, right outer join
## full outer join 지원하지 않음
## left outer join
select e.EMPLOYEE_ID, e.last_name, d.department_name
from emp e, dept d
where e.department_id = d.department_id(+) ;
## right outer join
select e.EMPLOYEE_ID, e.last_name, d.department_name
from emp e, dept d
where e.department_id(+) = d.department_id ;
## 표준 구문
## outer join : left outer join, right outer join , full outer join
## outer 생략가능
## left outer join : ***
select e.EMPLOYEE_ID, e.last_name, d.department_name
from emp e left outer join dept d using ( department_id ) ;
## right outer join
select e.EMPLOYEE_ID, e.last_name, d.department_name
from emp e right outer join dept d using ( department_id ) ;
## full outer join
select e.EMPLOYEE_ID, e.last_name, d.department_name
from emp e full outer join dept d using ( department_id ) ;
## 여러테이블 조인
a, b, c
select *
from a,b,c
where a.aa=b.bb and b.bb=c.cc ;
select *
from ( a join b using(aa) ) join c on ( b.bb=c.cc ) ;
## self join
사번, 사원이름, 상관이름
select e.employee_id as 사번, e.last_name as 사원이름, m.last_name as 상관이름
from emp e, emp m
where e.manager_id = m.employee_id ;
## non equi join
이름, 월급, 월급레벨
select e.last_name, e.salary, j.GRADE_LEVEL
from emp e, JOB_GRADES j
where e.salary between j.LOWEST_SAL and j.HIGHEST_SAL ;
'데이터베이스(SQL)' 카테고리의 다른 글
데이터베이스 4일차_서브쿼리(oracle, sql) (0) | 2021.05.03 |
---|---|
데이터베이스 3일차_조인(2)(oracle, sql) (1) | 2021.05.03 |
데이터베이스 3일차_다중행함수(oracle, sql) (0) | 2021.05.03 |
데이터베이스 2일차_sql 검색, 단일행함수(2)(oracle, sql) (0) | 2021.05.03 |
데이터베이스 2일차_sql 검색, 단일행함수(oracle, sql) (0) | 2021.05.03 |