SQL/ORACLE SQL

JOIN : 조인

HicKee 2022. 11. 3. 16:02

JOIN 

다른 테이블의 정보를 불러오고자 할 때
여러 개의 테이블에 저장된 데이터를 한꺼번에 조회할 필요성이 있을 경우
교차 곱의 형태로 불러오게 됨 (카디시안 곱)

 

EQUI JOIN : 이퀴조인 

     내부 조인이라고도 한다

     표준은 아니다(오라클에서만 가능)

 

문법

select table1.column,table2.column
from table1,table2
where table1.column1=table2.column2

예제

select *
from employee,department
where employee.dno = department.dno;
select employee.eno,employee.ename,department.dname,employee.dno
from employee,department
where employee.dno = department.dno and employee.eno =7788;

테이블 명에 별칭을 사용할 수도 있다

select e.eno,e.ename,d.dname,e.dno
from employee e,department d
where e.dno = d.dno and e.eno =7788;

 

NATURAL JOIN

      중복된 값이 있으면 자동으로 걸러준다(중복 제거)
      중복된 값이 없으면 오류

 

문법

select table1.column,table2.column
from table1 natural join table2

예제

select e.eno,e.ename,d.dname,dno --마지막 칼럼이 만약 e.dno 를 쓰면 오류
from employee e natural join department d
where e.eno =7788

 

JOIN ~ USING

     using 절에 조인이 되는 대상이 되는 칼럼을 지정

 

문법

select table1.column,table2.column
from table1 join table2
using(column);

예제

select e.eno,e.ename,d.dname,dno
from employee e join department d
using(dno)
where e.eno =7788;

 

JOIN ~ ON

      임의의 조건을 지정하거나 조인할 칼럼을 지정

 

문법

select table1.column,table2.column
from table1 join table2
on table1.column1 = table2.column2;

예제

select e.eno,e.ename,d.dname,e.dno
from employee e join department d
on e.dno = d.dno
where e.eno =7788;

 

NON-EQUI JOIN

      두 개의 테이블 간에 칼럼 값들이 서로 정확하게 일치하지 않는 경우

      where 절에 < , between A and B( = 조건이 아닐때)  >  연산자를 사용

 

예제

select ename, salary ,grade
from employee,salgrade
where salary between losal  and hisal;

 

3개의 테이블을 조인

select e.ename,d.dname,e.salary,s.grade
from employee e,department d ,salgrade s
where e.dno = d.dno
and salary between losal  and hisal;

 

SELF JOIN

      자신의 테이블과의 조인

      하나의 테이블에 있는 칼럼 끼리 연결해야하는 조인이 필요한경우

 

예제

select employees.ename as "사원이름",manager.ename as "상관이름"
from employee employees, employee manager
where employees.manager = manager.eno;

 

OUTER JOIN

      양측의 칼럼 값중의 하나가 NULL이지만 조인 결과로 출력 할 필요가 있을 경우

 

문법

select table1.column,table2.column
from table1, table2
where table1.column(+) = table2.column
또는
where table1.column = table2.column(+)

예제

select employees.ename ||'의 상관은 '||manager.ename
from employee employees join employee manager
on employees.manager = manager.eno(+);

 

OUTER JOIN ( ANSI )

      NULL 위치에 따라 왼쪽은 LEFT 오른쪽 RIGHT 

 

문법

select table1.column,table2.column
from table1 [LEFT,RIGHT,FULL] table2
on table1.column1 = table2.column2;

 

예제

--left outer join
select employees.ename ||'의 직속상관은 '||manager.ename
from employee employees left outer join employee manager
on employees.manager = manager.eno;