์ฐ์ต๋ฌธ์ , ์ค์ต๋ฌธ์ ๊ณต๋ถ ๋ฐฉ๋ฒ
1. ๋ต์ง ์๋ณด๊ณ ๋ง๋ฌด๋ฆฌ
2. ๋ต์ง ๋ณด๊ณ ์ฑ์ (์ค๋ต ๋ถ๋ถ ์ฒดํฌ)
3. ์ดํด๊ฐ ๊ฐ์ง ์๋ ๋ถ๋ถ q ํ์ ํ ๊ตฌ๊ธ๋ง, ๋ฒจ๋ก๊ทธ ์ ๋ฆฌ
8-1) ํ๊ท ๊ธ์ฌ ์ด์์ ๋ฐ๋ '๋ชจ๋ ' ์ฌ์์ ์ฌ์ ๋ฒํธ, ์ฑ ๋ฐ ๊ธ์ฌ๋ฅผ ์ถ๋ ฅํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
๊ธ์ฌ์ ์ค๋ฆ์ฐจ์์ผ๋ก ๊ฒฐ๊ณผ๋ฅผ ์ ๋ ฌํฉ๋๋ค.
select employee_id, last_name, salary
from employees
where salary > (select avg(salary) from employees)
order by salary
8-2) '์ฑ์ ๋ฌธ์ "u"๊ฐ ํฌํจ๋' ์ฌ์๊ณผ ๊ฐ์ '๋ถ์'์ ๊ทผ๋ฌดํ๋ ๋ชจ๋ ์ฌ์์ ์ฌ์ ๋ฒํธ์ ์ฑ์ ์ถ๋ ฅํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
select employee_id, last_name
from employees
where department_id in (select department_id from employees where last_name like '%u%');
8-3) ๋ถ์ ์์น ID(location_id) ๊ฐ 1700์ธ ์ฌ์์ ์ฑ, ๋ถ์ ID ๋ฐ ์์ ID๋ฅผ ์ถ๋ ฅํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ์ธ์
select last_name, department_id, job_id
from employees
where department_id in (select department_id from departments where location_id = 1700);
8-4) king(last_name) ์๊ฒ ๋ณด๊ณ ํ๋ ๋ชจ๋ ์ฌ์์ ์ฑ๊ณผ ๊ธ์ฌ๋ฅผ ํ์ํ๋ ๋ณด๊ณ ์๋ฅผ ์์ฑ
=๋งค๋์ ๊ฐ king์ธ ๋ชจ๋ ์ฌ์์ last_name, salary ์์ฑ
select last_name, salary
from employees
where manager_id in (select employee_id from employees where lower(last_name) = 'king');
8-5) executive ๋ถ์์ ์ฌ์์ ๋ํด ๋ถ์ID, ์ฑ ๋ฐ ์ง๋ฌด ID๋ฅผ ์ถ๋ ฅํ๋ ์ฟผ๋ฆฌ ์์ฑ
select department_id, last_name, job_id
from employees
where department_id in (select department_id from departments where lower(department_name) = 'executive');
8-6) 'ํ๊ท ๋ณด๋ค ๋ง์ ๊ธ์ฌ๋ฅผ ๋ฐ๊ณ ' ์ฑ(last_name)์ ๋ฌธ์ "u"๊ฐ ํฌํจ๋ ์ฌ์์ด '์ํ ๋ถ์์์' ๊ทผ๋ฌดํ๋ ์ฌ์ ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ์ธ์.
์ค๋ต)
select employee_id, last_name, salary
from employees
where department_id in (select department_id from employees where last_name like '%u%')
and salary > (select avg(salary) from employees);
cf. 8-2) '์ฑ์ ๋ฌธ์ "u"๊ฐ ํฌํจ๋' ์ฌ์๊ณผ '๊ฐ์ ๋ถ์์ ๊ทผ๋ฌดํ๋' '๋ชจ๋ ์ฌ์'์ ์ฌ์ ๋ฒํธ์ ์ฑ์ ์ถ๋ ฅํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
8-7) ์ปค๋ฏธ์ ์ ๋ฐ๋ ์ฌ์์ ๋ถ์ ๋ฒํธ ๋ฐ ๊ธ์ฌ '๋ ๋ค ์ผ์นํ๋' ์ฌ์ ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ์ธ์ (๋ค์ค์ด ์๋ธ์ฟผ๋ฆฌ)
select last_name, department_id, salary
from employees
where (department_id, salary) in (select department_id , salary from employees where commission_pct is not null);
8-8) ๊ด๋ฆฌ์๊ฐ ์๋ ์ฌ์์ ์ถ๋ ฅํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ์ธ์ (not in ์ฐ์ฐ์ ํ์ฉ)
select last_name
from employees
where employee_id
not in (select manager_id from employees where manager_id is not null);
8-9) '์ฌ์์ด ์๋' ๋ชจ๋ ๋ถ์์ ๋ถ์๋ฒํธ(department_id), ๋ถ์๋ช (department_name)์ ์ถ๋ ฅํ๋์ฟผ๋ฆฌ๋ฌธ์ ์์ฑํ์์ค
select department_id, department_name, manager_id, location_id
from departments
where department_id in (select department_id from departments where employee_id is null);
์ค๋ต)
select *
from departments
where department_id not in (select department_id from employees where department_id is not null);
8-10) ์ด๋ ค์
์์ ๋ถ์์ ํ๊ท ๊ธ์ฌ๋ณด๋ค ๋ง์ด ๋ฐ๋ ์ฌ์์ ์ถ๋ ฅํ๋ ์ฟผ๋ฆฌ ์์ฑ .
์ฌ์์ ์ฑ(last_name), ๊ธ์ฌ(salary), ๋ถ์ ID (department_id), '์์ ๋ถ์์ ํ๊ท ๊ธ์ฌ'(cf. ์ ์ฒด ๋ถ์์ ํ๊ท ๊ธ์ฌ) ํ์.
ํ๊ท ๊ธ์ฌ ์์ผ๋ก ์ ๋ ฌ, ํ๊ท ๊ธ์ฌ๋ ์์์ ์๋ 2์๋ฆฌ๋ก ๋ฐ์ฌ๋ฆผํ์ฌ ํ์
์ถ๋ ฅ ํ as ์ฌ์ฉ
select ename, salary, deptno,
select(avg(sal) from employees where department_id in (select department_id from employees)) dept_avg
from employees
where sal > (select avg(sal) from employees) ;
์ค๋ต) ํน์ ๋ถ์ ์์ ->์
ํ ์กฐ์ธ ์ํ ํ
์ด๋ธ ๋ถ๋ฆฌ, ์๋ฆฌ์์ค ์ง์
select e.last_name ename, e.salary salary, e.department_id deptno, round(avg(a.salary),2) dept_avg
from employees e, employees a
where e.department_id = a.department_id
and e.salary > (select avg(salary) from employees where department_id = e.department_id)
group by e.last_name e.salary e.department_id
order by avg(a.salary);
8-11) ๊ด๋ฆฌ์๊ฐ ์๋ ์ฌ์ ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๋ ์ฟผ๋ฆฌ๋ฅผ exists ์ฐ์ฐ์๋ฅผ ์ด์ฉํด ์์ฑ
select employee_id, last_name
from employees
where employee_id not in (select manager_id from employees where manager_id is not null);
select employee_id, last_name
from employees
where not exists (select 1 from employees);
์ค๋ต)
select employee_id, last_name
from employees o
where not exists (select 1 from employees e where o.employee_id = e.employee_id);
8-12) ๋ชจ๋ ์ฌ์์ ์ฌ์ ID, ์ฑ ๋ฐ ๋ถ์ ์ด๋ฆ์ ํ์ํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค. ๋ถ์ ์ด๋ฆ ์์๋ก ์ ๋ ฌํฉ๋๋ค. ์ค์นผ๋ผ ์๋ธ์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ๋ถ์ ์ด๋ฆ์ ํ์ํ์์ค.
select employee_id, last_name,
(select department_name from departments where ) department
from employees;
์ค๋ต)
select employee_id, last_name,
(select department_name from departments d where d.department_id = e.department_id) department
from employees e
order by department ;
8-13) ๋ถ์์ ์ด ๊ธ์ฌ๊ฐ ํ์ฌ ์ ์ฒด ์ด๊ธ์ฌ์ 8๋ถ์ 1(1/8)์ ์ด๊ณผํ๋ ๋ถ์์ ์ด๋ฆ๊ณผ ํด๋น ๋ถ์์ ์ด ๊ธ์ฌ๋ฅผ ํ์ํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค. ๋ถ์๋ณ ์ด ๊ธ์ฌ๋ฅผ ๊ตฌํ๋ ๋ถ๋ถ์ with ์ ๋ก ์์ฑํฉ๋๋ค.
with summary
(select department_name, sum(d.sal) dept_total
from departments d
where sal > (select sum(sal)*1/8 from employees e where e.department_id = d.department_id)
select department_name, dept_total from summary;
์ค๋ต)
with summary as
(select d.department_name, sum(e.salary) dept_total
from employees e, departments d
where e.department_id = d.department_id
group by d.department_name)
select department_name, dept_total
from summary
where sum(e.salary)> (select sum(dept_total)*1/8 from summary);