Oracle 문법 정리 - 서브쿼리
◆서브 쿼리
메인이 아닌 쿼리 = 서브쿼리
서브 쿼리 - 1)단일 행 쿼리
2)다중 행 쿼리
1)단일 행 서브쿼리
Abel의 급여보다 많거나 같은 사원들
select last_name, salary from employees where salary >= (select salary from employees where last_name = 'Abel'); |
Taylor와 같은 직업이고 Taylor보다 높은 급여를 받는 사원
select last_name, job_id, salary from employees where job_id = (select job_id from employees where last_name = 'Taylor') and salary > (select salary from employees where last_name = 'Taylor'); |
2)다중 행 서브쿼리
any
: 서브 쿼리에서 반환되는 값 들 중 하나라도 조건을 만족한다면 띄움
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'; |
all
: 서브 쿼리에서 반환되는 모든 값과 비교
select employee_id, last_name, job_id, salary from employees where salary < all (select salary from employees where job_id = 'IT_PROG') and job_id <>'IT_PROG'; |
exists
: 테이블에 특정 행이 있는지 여부에 따라 결과가 달라짐
SELECT * FROM departments WHERE NOT EXISTS (SELECT * FROM employees WHERE employees.department_id=departments.department_id); |
※group by 와 select문의 distinct 알아보기