본문 바로가기
SQL 문제 풀기/2. SQLD 기출 : 2024.3.01~2024.4.25

<SQLD 26> outer 조인에 대한 기출문제

by HYEHYE_SON 2024. 3. 26.
728x90

매일 SQLD 기출 문제 풀기 챌린지


 


문제26. OUTER JOIN 문제

다음은 Oracle 데이터베이스에서 사용하는 OUTER JOIN의 예제이다. 

빈칸(    ?    )을 채워 Oracle의 OUTER JOIN을 표준 ANSI SQL로 변경하시오.

 

 

[Oracle OUTER JOIN]

SELECT * 
 FROM EMP, DEPT
 WHERE EMP.DEPTNO(+) = DEPT.DEPTNO;

 

[ANSI JOIN]

SELECT * 
 FROM EMP (   ?   ) DEPT
 ON (EMP.DEPTNO = DEPT.DEPTNO);

 

 


해설해보기

ORACLE에서는
값이 없는 쪽에 (+) sign을 부텨주는 반면,

표준 ANSI SQL은
값이 없는 쪽이 outer라고 보면 된다.
따라서 값이 있는 쪽이 오른쪽이기 때문에 

정답은 
RIGHT OUTER JOIN이다.


직접 실행해보기

 

< Oracle JOIN >

SELECT * 
 FROM EMP, DEPT
 WHERE EMP.DEPTNO(+) = DEPT.DEPTNO;

 

< ANSI JOIN >

SELECT * 
 FROM EMP RIGHT OUTER JOIN DEPT
 ON (EMP.DEPTNO = DEPT.DEPTNO);

 

 

같은 결과를 출력 하는 것을 확인할 수 있다.


관련 스크립트

<관련스크립트>

alter session set nls_Date_format='RR/MM/DD';
drop table emp;
drop table dept;


CREATE TABLE DEPT
       (DEPTNO number(10),
        DNAME VARCHAR2(14),
        LOC VARCHAR2(13) );


INSERT INTO DEPT VALUES (10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO DEPT VALUES (20, 'RESEARCH',   'DALLAS');
INSERT INTO DEPT VALUES (30, 'SALES',      'CHICAGO');
INSERT INTO DEPT VALUES (40, 'OPERATIONS', 'BOSTON');

CREATE TABLE EMP (
 EMPNO               NUMBER(4) NOT NULL,
 ENAME               VARCHAR2(10),
 JOB                 VARCHAR2(9),
 MGR                 NUMBER(4) ,
 HIREDATE            DATE,
 SAL                 NUMBER(7,2),
 COMM                NUMBER(7,2),
 DEPTNO              NUMBER(2) );


INSERT INTO EMP VALUES (7839,'KING','PRESIDENT',NULL,'81-11-17',5000,NULL,10);
INSERT INTO EMP VALUES (7698,'BLAKE','MANAGER',7839,'81-05-01',2850,NULL,30);
INSERT INTO EMP VALUES (7782,'CLARK','MANAGER',7839,'81-05-09',2450,NULL,10);
INSERT INTO EMP VALUES (7566,'JONES','MANAGER',7839,'81-04-01',2975,NULL,20);
INSERT INTO EMP VALUES (7654,'MARTIN','SALESMAN',7698,'81-09-10',1250,1400,30);
INSERT INTO EMP VALUES (7499,'ALLEN','SALESMAN',7698,'81-02-11',1600,300,30);
INSERT INTO EMP VALUES (7844,'TURNER','SALESMAN',7698,'81-08-21',1500,0,30);
INSERT INTO EMP VALUES (7900,'JAMES','CLERK',7698,'81-12-11',950,NULL,30);
INSERT INTO EMP VALUES (7521,'WARD','SALESMAN',7698,'81-02-23',1250,500,30);
INSERT INTO EMP VALUES (7902,'FORD','ANALYST',7566,'81-12-11',3000,NULL,20);
INSERT INTO EMP VALUES (7369,'SMITH','CLERK',7902,'80-12-09',800,NULL,20);
INSERT INTO EMP VALUES (7788,'SCOTT','ANALYST',7566,'82-12-22',3000,NULL,20);
INSERT INTO EMP VALUES (7876,'ADAMS','CLERK',7788,'83-01-15',1100,NULL,20);
INSERT INTO EMP VALUES (7934,'MILLER','CLERK',7782,'82-01-11',1300,NULL,10);


commit;

 
 
 
 
 
 
 
 
 
 

반응형