FROM절에는 여러 개의 테이블을 지정하는 것이 가능하다.
테이블이 아니더라도, 테이블 형태, 즉 열과 행으로 구성된 데이터 집합이면 모두 FROM절에 지정이 가능하다.
→ 뷰(VIEW), 서브쿼리(SUBQUERY)등이 이에 해당된다.
지금까지의 SQL 문법 순서
SELECT 열1, 열2, ..., 열N
FROM 테이블1, 테이블2, ..., 테이블 N
WHERE 조건식
GROUP BY 그룹식
HAVING 그룹조건식
ORDER BY 정렬식
SELECT *
FROM EMP, DEPT
ORDER BY EMPNO;
테이블 이름.열 이름
SELECT *
FROM EMP, DEPT
WHERE EMP.DEPTNO = DEPT.DEPTNO
ORDER BY EMPNO;
FROM 테이블 이름1 별칭1, 테이블 이름2 별칭2 . . .
SELECT *
FROM EMP E, DEPT D
WHERE E.DEPTNO = D.DEPTNO
ORDER BY EMPNO;
SELECT E.EMPNO, E.ENAME, E.JOB, E.MGR, E.HIREDATE,
E.SAL, E.COMM, E.DEPTNO,
D.DNAME, D.LOC
FROM EMP E, DEPT D
WHERE D.DEPTNO = E.DEPTNO;
SELECT EMPNO, ENAME, DEPTNO, DNAME, LOC
FROM EMP E, DEPT D
WHERE E.DEPTNO = D.DEPTNO;
SELECT E.EMPNO, E.ENAME, E.DEPTNO, D.DNAME, D.LOC
FROM EMP E, DEPT D
WHERE E.DEPTNO = D.DEPTNO
AND E.SAL >= 3000;
WHERE절의 조건식을 사용해 테이블을 조인할 때,
반드시 각 테이블을 정확히 연결하는 조건식이 최소한 전체 테이블 수보다 하나 적은 수 만큼은 있어야 한다.
⇒ WHERE {조건식*(전체 테이블 수 -1)}
SELECT E.EMPNO, E.ENAME, E.JOB, E.MGR, E.HIREDATE, E.SAL, E.COMM, E.DEPTNO, S.GRADE, S.LOSAL, S.HISAL
FROM EMP E, SALGRADE S
WHERE E.SAL BETWEEN S.LOSAL AND S.HISAL;
SELECT E.EMPNO,E.ENAME, E.MGR,
E2.EMPNO AS MGR_EMPNO,
E2.ENAME AS MGR_ENAME
FROM EMP E, EMP E2
WHERE E.MGR = E2.EMPNO
ORDER BY E.EMPNO;
왼쪽 외부 조인(LEFT OUTER JOIN) | WHERE TABLE1.COL1 = TABLE2.COL1(+) |
---|
오른쪽 외부 조인(RIGHT OUTER JOIN) | WHERE TABLE2.CO1(+) = TABLE2.COL1 |
---|
SELECT E.EMPNO,E.ENAME, E.MGR,
E2.EMPNO AS MGR_EMPNO,
E2.ENAME AS MGR_ENAME
FROM EMP E, EMP E2
WHERE E.MGR = E2.EMPNO(+)
ORDER BY E.EMPNO;
SELECT E.EMPNO,E.ENAME, E.MGR,
E2.EMPNO AS MGR_EMPNO,
E2.ENAME AS MGR_ENAME
FROM EMP E, EMP E2
WHERE E.MGR(+)= E2.EMPNO
ORDER BY E.EMPNO;
SELECT E.EMPNO, E.ENAME, E.JOB, E.MGR,
E.HIREDATE, E.SAL, E.COMM,
DEPTNO,
D.DNAME, D.LOC
FROM EMP E NATURAL JOIN DEPT D
ORDER BY DEPTNO, E.EMPNO;
- 기존 등가 조인과 다르게 조인 기준열을 SELECT절에 명시할 때 테이블 이름을 붙이면 안되는 특성이 있다.
- 조인 조건이 WHERE절에 있는 기존 조인 방식과 달리, SQL-99방식은 FROM절에 조인 키워드를 사용하는 형태로 작성한다.
FROM TABLE1 JOIN TABLE2 USING (조인에 사용한 기준열)
- NATURAL JOIN과 마찬가지로 조인 기준 열로 명시된 열은 SELECT 절에서 테이블 이름을 붙이지 않고 작성한다.
SELECT E.EMPNO, E.ENAME, E.JOB, E.MGR, E.HIREDATE, E.SAL, E.COMM,
DEPTNO, D.DNAME, D.LOC
FROM EMP E JOIN DEPT D USING(DEPTNO)
WHERE SAL >= 3000
ORDER BY DEPTNO, E.EMPNO;
FROM TABLE1 JOIN TABLE2 ON (조인 조건식)
SELECT E.EMPNO, E.ENAME, E.JOB, E.MGR,
E.HIREDATE, E.SAL, E.COMM, E.DEPTNO,
D.DNAME, D.LOC
FROM EMP E JOIN DEPT D ON (E.DEPTNO = D.DEPTNO)
WHERE SAL <= 3000
ORDER BY E.DEPTNO, EMPNO;
SELECT E1.EMPNO, E1.ENAME, E1.MGR,
E2.EMPNO AS MGR_EMPNO,
E2.ENAME AS MGR_ENAME
FROM EMP E1 LEFT OUTER JOIN EMP E2 ON (E1.MGR = E2.EMPNO)
ORDER BY E1.EMPNO;
SELECT E1.EMPNO, E1.ENAME, E1.MGR,
E2.EMPNO AS MGR_EMPNO,
E2.ENAME AS MGR_ENAME
FROM EMP E1 RIGHT OUTER JOIN EMP E2 ON (E1.MGR = E2.EMPNO)
ORDER BY E1.EMPNO;
SELECT E1.EMPNO, E1.ENAME, E1.MGR,
E2.EMPNO AS MGR_EMPNO,
E2.ENAME AS MGR_ENAME
FROM EMP E1 FULL OUTER JOIN EMP E2 ON (E1.MGR = E2.EMPNO)
ORDER BY E1.EMPNO;
⇒ 조인 조건식과 출력 행을 선정하는 조건식을 구별할 수 있으므로 여러 테이블을 조인해야 하는 복잡한 SELECT문에서 SQL-99조인의 장점이 드러난다.
FROM TABLE1, TABLE2, TABLE3
WHERE
TABLE1.COL = TABLE2.COL
AND
TABLE2.COL = TABLE3.COL
FROM TABLE1
JOIN TABLE2 ON (조건식)
JOIN TABLE3 ON (조건식)
SELECT D.DEPTNO, D.DNAME, E.EMPNO, E.ENAME, E.SAL
FROM EMP E, DEPT D
WHERE D.DEPTNO = E.DEPTNO
AND SAL > 2000
ORDER BY D.DEPTNO;
SELECT DEPTNO, D.DNAME, E.EMPNO, E.ENAME, E.SAL
FROM EMP E JOIN DEPT D USING(DEPTNO)
WHERE SAL > 2000
ORDER BY DEPTNO;
SELECT D.DEPTNO, D.DNAME, TRUNC(AVG(E.SAL)),
MAX(E.SAL), MIN(E.SAL), COUNT(*) AS CNT
FROM EMP E, DEPT D
WHERE E.DEPTNO = D.DEPTNO
GROUP BY D.DEPTNO, D.DNAME
ORDER BY D.DEPTNO;
SELECT DEPTNO, D.DNAME, TRUNC(AVG(E.SAL)),
MAX(E.SAL), MIN(E.SAL), COUNT(*) AS CNT
FROM EMP E NATURAL JOIN DEPT D
GROUP BY DEPTNO, D.DNAME
ORDER BY DEPTNO;
SELECT D.DEPTNO, D.DNAME, E.EMPNO, E.ENAME, E.JOB, E.SAL
FROM EMP E, DEPT D
WHERE E.DEPTNO(+) = D.DEPTNO
ORDER BY E.DEPTNO, E.ENAME;
SELECT D.DEPTNO, D.DNAME, E.EMPNO, E.ENAME, E.JOB, E.SAL
FROM EMP E RIGHT OUTER JOIN DEPT D ON E.DEPTNO = D.DEPTNO
ORDER BY E.DEPTNO, E.ENAME;
SELECT D.DEPTNO, D.DNAME,
E.EMPNO, E.ENAME, E.MGR, E.SAL, E.DEPTNO,
S.LOSAL, S.HISAL, S.GRADE,
E.MGR AS MGR_EMPNO, E2.ENAME AS MGR_ENAME
FROM EMP E ,DEPT D,
EMP E2, SALGRADE S
WHERE
E.DEPTNO(+) = D.DEPTNO
AND
E.SAL BETWEEN S.LOSAL(+) AND S.HISAL(+)
AND
E.MGR = E2.EMPNO(+)
ORDER BY D.DEPTNO, E.EMPNO;
SELECT D.DEPTNO, D.DNAME,
E.EMPNO, E.ENAME, E.MGR, E.SAL, E.DEPTNO,
S.LOSAL, S.HISAL, S.GRADE,
E.MGR AS MGR_EMPNO, E2.ENAME AS MGR_ENAME
FROM EMP E
RIGHT OUTER JOIN DEPT D ON(E.DEPTNO = D.DEPTNO)
LEFT OUTER JOIN EMP E2 ON(E.MGR = E2.EMPNO)
LEFT OUTER JOIN SALGRADE S ON
(E.SAL BETWEEN S.LOSAL AND S.HISAL)
ORDER BY D.DEPTNO, E.EMPNO;
SELECT MAX(DATETIME) AS "시간"
FROM ANIMAL_INS;
SELECT MIN(DATETIME)
FROM ANIMAL_INS;
SELECT COUNT(*)
FROM ANIMAL_INS;
SELECT COUNT(DISTINCT NAME) AS COUNT
FROM ANIMAL_INS;
SELECT ANIMAL_TYPE, COUNT(*)
FROM ANIMAL_INS
GROUP BY ANIMAL_TYPE
ORDER BY ANIMAL_TYPE;
SELECT NAME, COUNT(*) AS COUNT
FROM ANIMAL_INS
WHERE NAME IS NOT NULL
GROUP BY NAME HAVING COUNT(*) >=2
ORDER BY NAME;
SELECT TO_CHAR(DATETIME, 'HH24') AS HOUR, COUNT(*) AS COUNT
FROM ANIMAL_OUTS
WHERE TO_CHAR(DATETIME, 'HH24') BETWEEN 9 AND 19
GROUP BY TO_CHAR(DATETIME, 'HH24')
ORDER BY TO_CHAR(DATETIME, 'HH24');
SELECT H.HOUR, NVL(CORE.COUNT, 0)
FROM (SELECT (TO_CHAR(DATETIME, 'HH24')) AS HOUR,
COUNT(*) AS COUNT
FROM ANIMAL_OUTS
GROUP BY (TO_CHAR(DATETIME, 'HH24'))
ORDER BY (TO_CHAR(DATETIME, 'HH24'))
) CORE,
(SELECT LEVEL-1 AS HOUR FROM dual CONNECT BY LEVEL<=24) H
WHERE CORE.HOUR(+) = H.HOUR
ORDER BY H.HOUR;