SELECT last_name
FROM employees
WHERE salary > (SELECT salary
FROM employees
WHERE last_name = 'Abel');
이름이 'Abel'인 사람보다 급여가 높은 사원의 이름
select
문에 삽입된 select
문.where
, having
, from
절에 사용.in
, all
, any
를 사용.all
은 모든 값에 and 조건.any
는 모든 값에 or 조건select employee_id, last_name, salary
from employees
where (department_id, salary) in (select department_id, max(salary)
from employees
group by department_id)
서브 쿼리에서 여러 개의 컬럼을 선택해서 비교할 수 있다.
select employee_id, last_name, salary
from employees
where department_id in (select department_id
from employees
where salary > (select avg(salary)
from employees)
and last_name like '%u%')
맞게 작성한 코드
select employee_id, last_name, salary
from employees
where department_id in (select department_id
from employees
where salary > (select avg(salary)
from employees
where last_name like '%u%'))
잘못 작성한 코드(이름에 'u'가 들어간 사람들의 평균 급여보다 더 많이 받는 사원들...)
while
: 반복 횟수가 정해지지 않은 경우
do~while
: 무조건 한 번은 실행한 뒤, while
처럼 활용
for
: 반복 횟수가 정해져 있을 때 사용
while(조건식) {
조건이 참인 동안 실행될 문구;
}
true
인 경우 while
문은 실행문구를 영원히 반복한다.do {
조건이 참인 동안 실행될 문구;
}while(조건식);