-- SUB QUERY
-- QUERY문 안에 QUERY문이다
-- 한개의 행에서 결과 값이 반환되는 커리문
-- SELECT 단일행 단위컬럼 SELECT NAME FROM EMP
-- FROM 단일행 단일, 다중컬럼 SELECT NAME EMPNO FROM EMP
-- WHERE 단일행 단일, 다중컬럼 SELECT NAME EMPNO FROM EMP
-- SELECT 방식
SELECT EMPLOYEE_ID, FIRST_NAME, SALARY,
(SELECT SUM(SALARY) FROM EMPLOYEES),
(SELECT COUNT(*) FROM EMPLOYEES)
FROM EMPLOYEES;
-- FROM
SELECT EMPLOYEE_ID,FIRST_NAME,FIRST_NAME,SALARY
FROM (SELECT EMPLOYEE_ID,FIRST_NAME,SALARY
FROM EMPLOYEES
WHERE LAST_NAME = 'King');
-- WHERE
SELECT
*
FROM EMPLOYEES
WHERE SALARY > (SELECT AVG(SALARY) FROM EMPLOYEES);
-- IN ANY == OR
SELECT FIRST_NAME, SALARY, JOB_ID
FROM EMPLOYEES
WHERE JOB_ID IN (SELECT JOB_ID FROM JOBS);
SELECT A.EMPLOYEE_ID, A.FIRST_NAME, A.JOB_ID, B.JOB_ID,
B."급여합계", B."인원수"
FROM EMPLOYEES A,
(SELECT JOB_ID, SUM(SALARY) AS "급여합계", COUNT(*) AS "인원수"
FROM EMPLOYEES
GROUP BY JOB_ID ) B
WHERE A.JOB_ID = B.JOB_ID;
-- 부서별로 가장 적게 받는 사원;과 같은 급여를 받는 사원의 정보를 출력
SELECT EMPLOYEE_ID,LAST_NAME,SALARY,DEPARTMENT_ID
FROM EMPLOYEES
WHERE SALARY IN (SELECT MIN(SALARY) FROM EMPLOYEES GROUP BY DEPARTMENT_ID);
-- 부서별로 가장 적게 받는 사원의 정보 출력
SELECT DEPARTMENT_ID,EMPLOYEE_ID,LAST_NAME,SALARY
FROM EMPLOYEES
WHERE (DEPARTMENT_ID, SALARY) IN
(SELECT DEPARTMENT_ID, MIN(SALARY)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID)
ORDER BY DEPARTMENT_ID;
SELECT DEPARTMENT_ID, SALARY
FROM EMPLOYEES;
'Oracle Database > SQL' 카테고리의 다른 글
SQL 테이블 쿼리 (0) | 2018.06.26 |
---|---|
SQL 특수Query (0) | 2018.06.26 |
SQL 쿼리문 JOIN (0) | 2018.06.26 |
SQL 쿼리문 기본 (0) | 2018.06.26 |
SQL 쿼리 그룹 (0) | 2018.06.26 |