[SQL] 데이터 조인 - FULL OUTER JOIN

·2025년 7월 7일
0

SQL

목록 보기
81/126

💡 반드시 알아야하는 조인 문법 2가지

1. 오라클 조인 문법

1. equi join
2. non equi join
3. outer join
4. self join

2. 1999 ANSI(American National Standard Institute) 문법

1. on 절을 사용한 조인
2. using 절을 사용한 조인
3. natural 조인
4. left/right/full outer 조인
5. cross 조인

문제1. 위의 결과를 출력하기 위해서 다음과 같이 오라클 조인문법을 사용하면 결과가 나오는지 확인하시오

-- 에러발생
select  e.ename, e.job, e.sal, d.loc 
 from emp  e,  dept  d
 where  e.deptno (+)= d.deptno (+);
 
 
 
 ORA-01468: outer-join된 테이블은 1개만 지정할 수 있습니다

https://docs.oracle.com/error-help/db/ora-01468/01468. 00000 -  "a predicate may reference only one outer-joined table"
*Cause:    A predicate in the WHERE clause had two columns from
           different tables with "(+)".
*Action:   Change the WHERE clause so that each predicate has a
           maximum of one outer-join table.
3, 21열에서 오류 발생

문제2. 위의 결과를 1999 ANSI 문법인 FULL OUTER JOIN 으로 수행하시오

select e.ename, e.job, e.sal, d.loc
 from emp e full outer join dept d
 on (e.deptno = d.deptno);

복습문제1. grouping sets 를 이용해서 부서번호, 부서번호별 토탈월급을 출력하는데 맨 밑에 전체 토탈월급을 출력하시오

select deptno, sum(sal)
 from emp
 group by grouping sets((deptno), ());

복습문제2. 부서위치, 부서위치별 토탈월급을 출력하시오

select d.loc, sum(e.sal)
 from emp e, dept d
 where e.deptno = d.deptno
 group by d.loc;

문제. 아래의 결과를 다음과 같이 가로로 출력하시오

select sum(decode(d.loc, 'CHICAGO', e.sal, null)) CHICAGO,
       sum(decode(d.loc, 'DALLAS', e.sal, null)) DALLAS,
       sum(decode(d.loc, 'NEW YORK', e.sal, null)) "NEW YORK",
       sum(decode(d.loc, 'BOSTON', e.sal, null)) BOSTON
 from emp e, dept d
 where e.deptno = d.deptno;
 
 
 -- 또는
 select sum(decode(d.loc, 'CHICAGO', e.sal, null)) CHICAGO,
       sum(decode(d.loc, 'DALLAS', e.sal, null)) DALLAS,
       sum(decode(d.loc, 'NEW YORK', e.sal, null)) "NEW YORK",
       sum(decode(d.loc, 'BOSTON', e.sal, null)) BOSTON
 from emp e join dept d
 on (e.deptno = d.deptno);
 
 
-- 또는
 select sum(decode(d.loc, 'CHICAGO', e.sal, null)) CHICAGO,
       sum(decode(d.loc, 'DALLAS', e.sal, null)) DALLAS,
       sum(decode(d.loc, 'NEW YORK', e.sal, null)) "NEW YORK",
       sum(decode(d.loc, 'BOSTON', e.sal, null)) BOSTON
 from emp e join dept d
 using (deptno);
 
 
-- 또는
-- pivot문 별칭 사용 X
select *
 from ( select d.loc, e.sal
        from emp e, dept d
        where e.deptno = d.deptno
      )
 pivot(sum(sal) for loc in('CHICAGO', 'DALLAS', 'NEW YORK', 'BOSTON'));

0개의 댓글