CH3. Working with Multiple Tables

sky.dev·2025년 4월 22일

SQL CookBook

목록 보기
4/5
post-thumbnail

챕터3. 다중 테이블 작업

  1. 행 집합을 다른 행 위에 추가하기

나타내고자 하는 결과셋

ENAME_AND_DNAME DEPTNO
--------------- ----------
CLARK 10
KING 10
MILLER 10
----------
ACCOUNTING 10
RESEARCH 20
SALES 30
OPERATIONS 40

  1. 연관된 여러 행 결합하기

SELECT e.ename, d.loc
FROM emp e, dept d
WHERE e.deptno = d.deptno
AND e.deptno = 10;

==> 동등조인
조인 조건이 동등 조건에 기반을 두는 조인
내부 조인은 조인의 원래 형태, 반환된 각 행은 각 테이블의 데이터가 포함된다

  1. 두 테이블의 공통 행 찾기

create view V
as
select ename,job,sal
from emp
where job = 'CLERK'
select * from V

ENAME JOB SAL


SMITH CLERK 800
ADAMS CLERK 1100
JAMES CLERK 950
MILLER CLERK 1300

해답
MySQL & SQL Server

select e.empno,e.ename,e.job,e.sal,e.deptno
from emp e, V
where e.ename = v.ename
and e.job = v.job
and e.sal = v.sal

JOIN 사용하는 방법
select e.empno,e.ename,e.job,e.sal,e.deptno
from emp e join V
on ( e.ename = v.ename
and e.job = v.job
and e.sal = v.sal);

DB2, Oracle, and PostgreSQL

select empno,ename,job,sal,deptno
from emp
where (ename,job,sal) in (
select ename,job,sal from emp
intersect
select ename,job,sal from V);

  1. 다른 테이블 행과 일치하지 않는 행 검색하기

select d.*
from dept d left outer join emp e
on (d.deptno = e.deptno)
where e.deptno is null;

외부 조인 하고나서 일치하지 않는 행만 유지하는 방식으로 작동

0개의 댓글