--ex 2-1) ๋ถ์๋ณ๋ก ์ฌ์์ ์๊ฐ 5๋ช ์ด์์ธ ๋ถ์์
-- ๋ถ์๋ฒํธ์ ๋ถ์๋ช
-- , ๋ถ์์ฅ์ฌ๋ฒ, first_name์ ์ธ์์์์ผ๋ก ์ถ๋ ฅํ๋ผ.
select d.department_id, d.department_name, d.manager_id,
(select first_name from employees
where d.manager_id = employee_id
), count(*)
from departments d join employees e
on d.department_id = e.department_id
group by d.department_id, d.department_name, d.manager_id
having count(*) >=5
order by 5 desc;
-- ex 2-2) ๋ถ์๋ณ๋ก ์ผ๋ง๋ ๋ง์ ๊ธ์ฌ๊ฐ ์ง๊ธ๋๋์ง ์๊ณ ์ถ๋ค.
-- ์ ์ฒด ์ด๊ธ์ฌ์์ ๋ถ์๊ฐ ์ฐจ์งํ๋ ๋น์จ์ ๋ณผ ์ ์๋๋ก ์์ฑํ๋ผ.
-- ๋ถ์๋ฒํธ,๋ถ์๋ณ ๊ธ์ฌํฉ๊ณ, ๋น์จ(%๊ฐ ๋งจ๋ค์ ์ค๋๋ก)์ ๋น์จ์์ผ๋ก ์ถ๋ ฅํ๋ผ.
select department_id, sum(salary),
lpad(round((sum(salary)/(select sum(salary) from employees))*100) || '%',10)
from employees
group by department_id
order by 3;
-- 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
order by 1;
...
-- 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
group by l.city
having avg(e.salary)<(select avg(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 group by employee_id having count(*)>=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
where 2<=(select count(*) 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
where exists (select *
from job_history where e.employee_id=employee_id);
-- ex 2-7)CTAS๊ธฐ๋ฒ์ผ๋ก employees๋ฅผ testemp2 ํ ์ด๋ธ๋ก ๋ณต์ฌํ๊ณ
-- testemp2์ job_id๊ฐ ‘ST_MAN’์ธ ์ฌ์์ salary๋ฅผ
-- ๋ถ์์ฅ์ฌ๋ฒ์ด ‘100’์ธ ๋ถ์์ ํ๊ท ์๊ธ์ผ๋ก updateํ๋ผ.
create table testemp3 as select * from employees;
update testemp3 t
set salary = (select avg(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 group by 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 >
(select avg(salary) from employees e1 join departments d
on e1.department_id = d.department_id);