본문 바로가기

SQL

[Oracle] Oracle 조인

조인(JOIN)

: 두 개 이상의 테이블에서 내가 원하는 데이터를 가져오는 방법

 

주의해야할 점

cartesian product 의 발생 :

①조인 조건이 생략된 경우

②조인 조건이 잘못된 경우

첫번째 테이블 행의 수와 두번째 테이블 행의 수가 곱해져서 출력.

 

equi join(=inner join, simple join, 등가 조인)

select 테이블1.컬럼명1, 테이블2.컬럼명2
from 테이블1, 테이블2 —테이블 별칭 설정 가능
where 테이블1.테이블1,2 공통컬럼=테이블2.테이블1,2 공통컬럼; --조인조건 술어
--n개의 테이블을 조인하려면 조인조건 술어는 n-1개 만들어야 함

[문제35] 사원들의 사원번호, 근무 도시를 출력해주세요.

select e.employee_id , l.city
from employees e, locations l, departments d
where e.department_id = d.department_id and d.location_id = l.location_id;

[문제36] 사원들의 사원번호, 국가 이름을 출력해주세요.

select e.employee_id, c.country_name
from employees e, locations l, departments d, countries c
where e.department_id = d.department_id
and d.location_id = l.location_id
and l.country_id = c.country_id;

outer join

-key값이 일치되는 데이터, key값이 일치되지 않은 데이터도 출력하는 조인.

-한 쪽에만 (+)를 표현한다. (데이터를 전부 출력하려는 테이블이 아닌 곳에 (+) 표현)

-양 쪽에 (+) 표현하면 오류 >>해결방법 

select 컬럼명1, 컬럼명2

from 테이블1, 테이블2

where 테이블1.공통컬럼 = 테이블2.공통컬럼(+);

select e.employee_id, d.department_name
from employees e, departments d
where e.department_id = d.department_id(+);

 

주의해야할 점

select 컬럼명1, 컬럼명2, 컬럼명3

from 테이블1, 테이블2, 테이블3

where 테이블1.공통컬럼 = 테이블2.공통컬럼(+)

and 테이블2. 공통컬럼 = 테이블3.공통컬럼(+);

select e.employee_id, d.department_name, l.city
from employees e, departments d, locations l
where e.department_id = d.department_id(+)
and d.location_id = l.location_id(+);

[문제39] 부서의 관리자 정보를 employee_id, last_name, department_id, department_name 을 표시하기 위한 query 를 작성합니다.

select e.employee_id, e.last_name, d.department_id, d.department_name
from employees e, departments d
where e.employee_id = d.manager_id;

[문제40] commission_pct에 null 이 아닌 사원들의 last_name, commission_pct, department_name 을 출력해주세요.

select e.last_name, e.commission_pct, d.department_name
from employees e, departments d
where e.commission_pct is not null
and e.department_id = d.department_id(+);

[문제41] last_name에 소문자 'a'가 포함된 사원들의 last_name, department_name을 출력해주세요.

select e.last_name, d.department_name
from employees e, departments d
where e.last_name like '%a%'
and e.department_id = d.department_id(+);

self join

: 자신의 테이블을 참조할 때 사용하는 조인 (똑같은 테이블을 복사해서 별칭을 다르게 설정)

select 별칭1.컬럼명1, 별칭2.컬럼명2

from 테이블1 별칭1, 테이블1 별칭2

where 별칭1.공통컬럼 = 별칭2.공통컬럼;


[문제42] 사원들의 last_name, 관리자 last_name을 출력해주세요.

select w.last_name 사원, m.last_name 관리자
from employees w, employees m
where w.manager_id=m.employee_id;

 


non equi join

: equi join(=) 할 수 없는 다른 연산자를 사용하는 조인의 기법.

값을 범위로 조인하려는 경우 많이 사용된다.

select 별칭1.컬럼명1, 별칭2.컬럼명2

from 테이블1 별칭1, 테이블2 별칭2

where 별칭1.공통컬럼 비교연산자 별칭2.공통컬럼;


[문제43] 사원들의 급여의 등급 레이블의 빈도수를 출력해주세요.

select j.grade_level, count(*)
from employees e, job_grades j
where e.salary between j.lowest_sal and j.highest_sal
group by j.grade_level;

[문제44] 사원들의 사번, 급여, 급여등급, 부서이름을 출력하세요. 부서배치를 받지 않는 사원은 제외시켜주세요.

select e.employee_id, e.salary, j.grade_level, d.department_name
from employees e, job_grades j, departments d
where e.salary between j.lowest_sal and j.highest_sal
and e.department_id = d.department_id;

[문제45] 사원들의 사번, 급여, 급여등급, 부서이름, 근무 도시 정보를 출력하세요. 부서배치를 받지 않는 사원도 포함시켜주세요.

select e.employee_id, e.salary, j.grade_level, d.department_name, l.city
from employees e, job_grades j, locations l, departments d
where e.salary between j.lowest_sal and j.highest_sal
and e.department_id = d.department_id(+)
and d.location_id = l.location_id(+);
Recent Posts
Popular Posts
Recent Comments