group by절 제외하고 서브쿼리 다 사용가능.
select last_name, job_id, salary
from employees
where salary =
(select min(salary) from employees);
select department_id, min(salary)
from employees
group by department_id
having min(salary) >
(select min(salary)
from employees
where department_id = 50);
select a.last_name, a.salary, a.department_id, b.salavg
from employees a join
(select department_id, avg(salary) salavg
from employees
group by department_id) b
on a.department_id = b.department_id
and a.salary > b.salavg;
select a, last_name, a.salary, a.department_id, b.salavg
from employees a join
(select department_id, avg(salary) salavg
from employees
group by department_id) b
on a.department_id = b.department_id
and a.salary > b.salavg;
select last_name, job_id, salary
from employees
where job_id =
(select job_id
from employees
where last_name = 'Bell')
and salary>
(select salary
from employees
where last_name = 'Bell');
select employee_id, last_name
from employees
where salary =
(select min(salary)
from employees
group by department_id);
- 서브쿼리에 다중행 반환되어 오류남
- 단일행 비교연산자 =을 다중행 비교연산자 in으로 변경 하면 됨
select employee_id, last_name
from employees
where salary in
(select min(salary)
from employees
group by department_id);
select last_name , job_id
from employees
where job_id =
(select job_id
from employees
where last_name ='Haas');
Haas라는 직원 없음. null값 넘어감
다중행 비교연산자 | 의미 | (==) |
---|---|---|
=any | (=, OR) | in (=, OR) |
>any | (>, OR) 최소값보다 큰지 | |
>=any | (>=, OR) 최소값보다 크거나 같은지 | |
<any | (<, OR) 최대값보다 작은지 | |
<=any | (<=, OR) 최대값보다 작거나 같은지 | |
<>any | (<>, OR) | 잘 사용 안함 |
=all | (=, AND) | 잘 사용 안함 |
>all | (>, AND) 최대값보다 큰지 | |
>=all | (>=, AND) 최대값보다 크거나 같은지 | |
<all | (<, AND) 최소값보다 작은지 | |
<=all | (<=, AND) 최소값보다 작거나 같은지 | |
<>all | (<>, AND) | not in(<>, AND) |
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';
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';
select emp.last_name
from employees emp
where emp.employee_id not in
(select mgr.manager_id
from employees mgr
where manager_id is not null);
select employee_id, manager_id, department_id
from employees
where manager_id in
(select manager_id
from employees
where employee_id in(174,141))
and department_id in
(select department_id
from employees
where employee_id in(174,141))
and employee_id not in(174, 141);
select employee_id, manager_id, department_id
from employees
where (manager_id, department_id) in
(select manager_id, department_id
from employees
where employee_id in(174,199))
and employee_id not in (174,199);
select first_name, department_id, salary
from employees
where (salary, department_id) in
(select min(salary), department_id
from employees
group by department_id)
order by department_id;
select employee_id, last_name
from employees
where department_id in
(select department_id
from employees
where last_name like '%u%');
select employee_id, last_name, salary
from employees
where salary >
(select avg(salary) from employees)
and department_id in
(select department_id
from employees
where last_name like '%u%');