본문 바로가기

Programming/Database

데이터베이스 5강

#Sub Query

  문제 : Abel이라는 사원의 급여보다 급여가 많은 사원은 누구인가?
          Abel이라는 사원이 받는 급여가 얼마인가를 찾음

  SELECT last_name, salary
  FROM employees
  WHERE last_name = 'Abel'

  Abel의 급여를 11000으로 확인

  SELECT last_name, salary
  FROM employees
  WHERE salary > 11000
------------------------------------------------
  SELECT last_name, salary
  FROM employees
  WHERE salary > ( SELECT salary
                      FROM employees
                      WHERE last_name = 'Abel')


SUB Query는 값을 대체함
FROM절에서 SUB Query 유용하게 씀

괄호로 묶음
비교 조건의 오른쪽
TOP-N 분석 (상위 N개)
단일 행 서브 쿼리 : 단일 행 연산자 사용 > = <
다중 행 서브 쿼리 : 다중 행 연산자 사용 IN ( , , , , )

서브 쿼리가 반환하는 타입과 연산 타입을 맞춤

#단일 행 서브쿼리 실행
SELECT last_name, job_id, salary
FROM employees
WHERE job_id = (SELECT job_id
                      FROM employees
                      WHERE employee_id = 141)
AND salary > (SELECT salary
                  FROM employees
                  WHERE employee_id =143);

# 서브쿼리에서 그룹함수 사용
SELECT last_name, job_id, salary
FROM employees
WHERE salary = (SELECT MIN(salary)
                     FROM employees);

# HAVING절에 서브쿼리 사용
SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary) > (SELECT MIN(salary)
                             FROM employees
                             WHERE department_id = 50);



# 서브쿼리를 사용하면 조인을 사용하여 외래키를 참조할 필요가 없음
SELECT e.last_name, e.salary
FROM employees e, departments d
WHERE e.department_id = d.department_id
AND d.department_name = 'IT'

SELECT last_name, salary
FROM employees
WHERE department_id = (SELECT department_id
                                FROM departments
                                WHERE department_name = 'IT')


# 다중 행 서브 쿼리에 다중 행 연산자
SELECT employee_id, last_name
FROM employees
WHERE salary IN (SELECT MIN(salary)
FROM employees
GROUP BY department_id);



# 10 20 30 40 50 60 
  (30, 40) 기준 출력

  IN (30, 40)
 >
  30, 40

  > ANY(30, 40) : 최소값보다 큰값 (30보다 큰)
 >
  40, 50 ,60
 
  < ANY(30, 40) : 최대값보다 작은값
 >
  10, 20 ,30

  > ALL(30, 40) : 전부보다 큰 값
 >
  50, 60

  < ALL(30, 40) : 전부보다 작은값
 >
  10, 20

SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary < ANY
                          (SELECT salary
                           FROM employees
                           WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';

# 예제 : Abel과 동일한 부서근무하는 모든 사원들의 사번 고용날짜
  SELECT employee_id, hire_date
  FROM employees
  WHERE department_id = ( SELECT department_id
                                   FROM employees
                                   WHERE last_name = 'Abel');
  AND last_name != 'Abel';

# 예제 : 회사의 전체 평균 급여보다 더 급여를 많이 받는 사원들의 사번 및 이름을 출력하시오.
SELECT employee_id, last_name
FROM employees
WHERE salary > ( SELECT AVG(salary)
                      FROM employees);

# 이름에 u가 포함이 되는 사원들과 동일 부서에 근무를 하는 모든 사원들의 사번 및 이름을 출력하시오.
SELECT employee_id, last_name
FROM employees
WHERE department_id IN ( SELECT department_id
                                 FROM employees
                                 WHERE last_name LIKE '%u%');

# 시애틀에 사는 사람 중 커미션을 받지 않는 모든 사람들의 이름, 부서 명 지역 Id를 출력하시오.

SELECT e.last_name, d.department_name, d.location_id
FROM employees e, departments d
WHERE e.department_id = d.department_id
AND d.location_id = (SELECT location_id
                           FROM locations
                           WHERE city = 'Seattle')
AND e.commission_pct IS NULL;



SELECT e.last_name, d.department_name, l.location_id
FROM employees e, departments d, locations l
WHERE e.department_id = d.department_id
AND d.department_id = l.department_id
AND l.city = 'Seattle'
AND commission_pct IS NULL;

'Programming > Database' 카테고리의 다른 글

데이터베이스 7강  (0) 2020.03.10
데이터베이스 6강  (0) 2020.03.10
데이터베이스 4강  (0) 2020.03.10
데이터베이스 3강  (0) 2020.03.10
데이터베이스 2강  (0) 2020.03.10