-- ex 2-3)์ง์์์ฌ๊ฐ ์๋ ์ฌ์์ ํฌํจํ์ฌ -- ๋ชจ๋ ์ฌ์์ ์ง์์์ฌ๊ฐ ๋์ค๋๋ก ํ๊ณ ์ถ๋ค. --์ฌ๋ฒ(employee_id), first_name, ์ง์์์ฌ์ฌ๋ฒ(manager_id), -- ์ง์์์ฌfirst_name์ ์ฌ๋ฒ์์ผ๋ก ์ถ๋ ฅํ๋ผ. (correlated subquery์ด์ฉ)select employee_id, first_name, manager_id,
(select first_name from employees where e.manager_id = employee_id)
from employees e
orderby1;
...
-- ex 2-4)๋ชจ๋ ๊ธ์ฌ์ ํ๊ท ๊ธ์ฌ๋ณด๋ค ์(city)๋ณ๋ก -- ์ง๊ธ๋ ํ๊ท ๊ธ์ฌ๊ฐ ์ ์ ์์ ๋ํ์ฌ -- ์(city), ์๋ณ ํ๊ท ๊ธ์ฌ๋ฅผ ์ถ๋ ฅํ์์ค.select l.city,
round(avg(e.salary))
from locations l
join departments d on l.location_id=d.location_id
join employees e on d.department_id = e.department_id
groupby l.city
havingavg(e.salary)<(selectavg(salary) from employees);
-- ex 2-5)๋ ๋ฒ์ด์์ job์ด๋ ฅ์ ๊ฐ์ง ๋ชจ๋ ์ฌ์์ ์ฌ๋ฒ, -- first_name, ํ์ฌ๋ถ์๋ฒํธ, ํ์ฌ๋ถ์๋ช , job์ด๋ ฅ๋ถ์๋ฒํธ, -- job์ด๋ ฅ๋ถ์๋ช ์ ์ถ๋ ฅํ์์ค.select e.employee_id, e.first_name,
e.department_id, d.department_name,
h.department_id, (select department_name from departments where
department_id = h.department_id)
from employees e
join departments d on e.department_id = d.department_id
join job_history h on h.employee_id = e.employee_id
where e.employee_id in
(select employee_id from job_history groupby employee_id havingcount(*)>=2);
-- ๋ค๋ฅธ ๋ฐฉ๋ฒselect e.employee_id, e.first_name,
e.department_id, d.department_name,
h.department_id, (select department_name from departments where
department_id = h.department_id)
from employees e
join departments d on e.department_id = d.department_id
join job_history h on h.employee_id = e.employee_id
where2<=(selectcount(*) from job_history where e.employee_id=employee_id);
-- ex 2-6) job์ด๋ ฅ์ด ์๋ ๋ชจ๋ ์ฌ์์ -- ์ฌ๋ฒ, first_name, ํ์ฌ job๋ฒํธ, ํ์ฌ job๋ช ์ ์ถ๋ ฅํ์์ค.-- (exists ์ด์ฉ)select e.employee_id, e.first_name, e.job_id, j.job_title
from employees e join jobs j on e.job_id = j.job_id
whereexists (select*from job_history where e.employee_id=employee_id);
-- ex 2-7)CTAS๊ธฐ๋ฒ์ผ๋ก employees๋ฅผ testemp2 ํ ์ด๋ธ๋ก ๋ณต์ฌํ๊ณ -- testemp2์ job_id๊ฐ โST_MANโ์ธ ์ฌ์์ salary๋ฅผ -- ๋ถ์์ฅ์ฌ๋ฒ์ด โ100โ์ธ ๋ถ์์ ํ๊ท ์๊ธ์ผ๋ก updateํ๋ผ.createtable testemp3 asselect*from employees;
update testemp3 t
set salary = (selectavg(salary) from employees e
join departments d on d.department_id = e.department_id
where d.manager_id='100')
where job_id='ST_MAN';
-- ex 2-8)๋ถ์๋ช ์ด โMarketingโ์ธ ์ฌ์ ์ค์์ -- ๊ทธ ๋ถ์์ ํ๊ท ๊ธ์ฌ๋ณด๋ค -- ๋ ๋์ ๊ธ์ฌ๋ฅผ ๋ฐ๋ ์ฌ์์ first_name๊ณผ ๊ธ์ฌ๋ฅผ ์ถ๋ ฅํ๋ผ.select d.department_id,avg(e.salary) from employees e join departments d
on d.department_id = e.department_id groupby d.department_id;
select first_name, salary from employees e
where
(select department_name from departments d
where e.department_id = d.department_id)='Marketing'and
salary >
(selectavg(salary) from employees e1 join departments d
on e1.department_id = d.department_id);