[DB] ORACLE - 5์ผ
in DB
ORACLE
์ค๋ผํด 5์ผ
[ํ์์ง์(SubQuery]
ํ๋์ ์ฟผ๋ฆฌ์ ๋ค๋ฅธ ์ฟผ๋ฆฌ๊ฐ ํฌํจ๋๋ ๊ตฌ์กฐ, ()๋ก์ฒ๋ฆฌ
1) ๋จ์ผํ ์๋ธ์ฟผ๋ฆฌ(๋จ์ผํ๋ฐํ) : > , < , >=, <= , <>
Main Query
Sub Query -----> 1 ๊ฐ๊ฒฐ๊ณผ
2) ๋ค์คํ ์๋ธ์ฟผ๋ฆฌ(์ฌ๋ฌํ๋ฐํ) : in, any, all
Main Query
Sub Query -----> ์ฌ๋ฌ๊ฐ์ ๊ฒฐ๊ณผ
< any : ๋น๊ต๋์์ค ์ต๋๊ฐ๋ณด๋ค ์์
> any : ๋น๊ต๋์์ค ์ต์๊ฐ๋ณด๋ค ํผ
(ex. ๊ณผ์ฅ์ง๊ธ์ ๊ธ์ฌ๋ฅผ ๋ฐ๋ ์ฌ์์กฐํ)
= any : in์ฐ์ฐ์์ ๋์ผ
< all : ๋น๊ต๋์์ค ์ต์๊ฐ๋ณด๋ค ์์
> all : ๋น๊ต๋์์ค ์ต๋๊ฐ๋ณด๋ค ํผ
(ex. ๋ชจ๋ ๊ณผ์ฅ๋ค์ ์ง๊ธ๋ณด๋ค ๊ธ์ฌ๊ฐ ๋ง์ ์ฌ์์กฐํ)
3) ์๊ด์ฟผ๋ฆฌ(correlated subquery)
๋ฉ์ธ์ฟผ๋ฆฌ์์ ๊ณ ๋ ค๋ ๊ฐ ํ๋ณด ํ๋ค์ ๋ํด ์๋ธ์ฟผ๋ฆฌ๊ฐ ๋ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํด์ผํ๋๊ฒฝ์ฐ
(๋ฉ์ธ์ฟผ๋ฆฌ์์ ์ฒ๋ฆฌ๋๋ ๊ฐ ํ๋ค์ ๊ฐ์ ๋ฐ๋ผ ์๋ต์ด ๋ฌ๋ผ์ ธ์ผํ๋ ๊ฒฝ์ฐ)์ ์ ์ฉํ๋ค
exists, not exists : ์กด์ฌ ์ฌ๋ถ์ ๋ฐ๋ผ true,false์ ๋ฐํ
ex 1) Neena์ฌ์์ ๋ถ์๋ช ์ ์์๋ด์์ค
select department_id from employees where first_name='Neena';
select department_name from departments where department_id=90;
select department_name from departments
where department_id = (select department_id
from employees
where first_name='Neena');
ex 2) Neena์ฌ์์ ๋ถ์์์ Neena์ฌ์๋ณด๋ค ๊ธ์ฌ๋ฅผ ๋ง์ด ๋ฐ๋ ์ฌ์๋ค์ ๊ตฌํ์์ค
select last_name, department_id, salary
from employees
where department_id = (select department_id
from employees
where first_name='Neena')
and
salary > (select salary from employees where first_name='Neena');
[๋ฌธ์ 1] ์ต์ ๊ธ์ฌ๋ฅผ ๋ฐ๋ ์ฌ์๋ค์ ์ด๋ฆ๊ณผ ๊ธ์ฌ๋ฅผ ๊ตฌํ์์ค
[๋ฌธ์ 2] ๋ถ์๋ณ ๊ธ์ฌํฉ๊ณ์ค ์ต๋๊ธ์ฌ๋ฅผ ๋ฐ๋ ๋ถ์์ ๋ถ์๋ช ๊ณผ ๊ธ์ฌํฉ๊ณ๋ฅผ ๊ตฌํ์์ค(group by)
DEPARTMENT_NAME SUM(SALARY)
-----------------------------
Sales 304500
ex3 ) Austin๊ณผ ๊ฐ์ ๋ถ์์ด๋ฉด์ ๊ฐ์ ๊ธ์ฌ๋ฅผ ๋ฐ๋ ์ฌ์๋ค์ ์ด๋ฆ, ๋ถ์๋ช , ๊ธ์ฌ๋ฅผ ๊ตฌํ์์ค(60๋ถ์, 4800๋ฌ๋ฌ)
select last_name, department_name, salary
from employees
left join departments using(department_id)
where department_id = (select department_id
from employees
where last_name='Austin')
and
salary = (select salary from employees where last_name='Austin');
ex4 ) โST_MANโ ์ง๊ธ๋ณด๋ค ๊ธ์ฌ๊ฐ ๋ง์ โIT_PROGโ ์ง๊ธ ์ง์๋ค์ ์กฐํํ์์ค
select last_name, job_id, salary
from employees
where job_id = 'IT_PROG'
and
salary >any(select salary from employees where job_id='ST_MAN');
[๋ฌธ์ 3] โIT_PROGโ ์ง๊ธ ์ค ๊ฐ์ฅ ๋ง์ด ๋ฐ๋ ์ฌ์์ ๊ธ์ฌ๋ณด๋ค ๋ ๋ง์ ๊ธ์ฌ๋ฅผ ๋ฐ๋ โFI_ACCOUNTโ ๋๋ โSA_REPโ ์ง๊ธ ์ง์๋ค์ ์กฐํํ์์ค
์กฐ๊ฑด1) ๊ธ์ฌ ์์ผ๋ก ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌํ์์ค
์กฐ๊ฑด2) ๊ธ์ฌ๋ ์ธ์๋ฆฌ ๋ง๋ค ์ฝค๋ง(,) ์ฐ๊ณ ํํ๋จ์ โ์โ์ ๋ถ์ด์์ค
์กฐ๊ฑด3) ํ์ดํ์ ์ฌ์๋ช , ์ ๋ฌดID, ๊ธ์ฌ๋ก ํ์ํ์์ค
ex5 ) โIT_PROGโ์ ๊ฐ์ ๊ธ์ฌ๋ฅผ ๋ฐ๋ ์ฌ์๋ค์ ์ด๋ฆ,์ ๋ฌดID,๊ธ์ฌ๋ฅผ ์ ๋ถ ๊ตฌํ์์ค
select last_name, job_id, salary
from employees
where salary in(select salary from employees where job_id='IT_PROG');
ex6 ) ์ ์ฒด์ง์์ ๋ํ ๊ด๋ฆฌ์์ ์ง์์ ๊ตฌ๋ถํ๋ ํ์๋ฅผ ํ์์ค(in, not in์ด์ฉ)
์ฌ์๋ฒํธ ์ด๋ฆ ๊ตฌ๋ถ
---------------------
100 King ๊ด๋ฆฌ์
- ๋ฐฉ๋ฒ 1 (in ์ฐ์ฐ์)
select employee_id as ์ฌ์๋ฒํธ, last_name as ์ด๋ฆ,
case
when employee_id in(select manager_id from employees)
then '๊ด๋ฆฌ์'
else '์ง์'
end as ๊ตฌ๋ถ
from employees
order by 3,1;
- ๋ฐฉ๋ฒ 2 (union, in, not in ์ฐ์ฐ์)
select employee_id as ์ฌ์๋ฒํธ, last_name as ์ด๋ฆ, '๊ด๋ฆฌ์' as ๊ตฌ๋ถ
from employees
where employee_id in(select manager_id from employees)
union
select employee_id ์ฌ์๋ฒํธ, last_name ์ด๋ฆ, '์ง์' as ๊ตฌ๋ถ
from employees
where employee_id not in(select manager_id from employees where manager_id is not null) manger_id != 100 or manager_id != 102 or ,,,
order by 3,1;
- ๋ฐฉ๋ฒ 3 (์๊ด์ฟผ๋ฆฌ์ด์ฉ)
- ๋ฉ์ธ์ฟผ๋ฆฌ ํํ์ ์ฝ๊ณ ํด๋น๊ฐ์ ์๋ธ์ฟผ๋ฆฌ์์ ์ฐธ์กฐํ์ฌ ์๋ธ์ฟผ๋ฆฌ๊ฒฐ๊ณผ์ ์กด์ฌํ๋ฉด true๋ฅผ ๋ฐํ
select employee_id as ์ฌ์๋ฒํธ, last_name as ์ด๋ฆ, '๊ด๋ฆฌ์' as ๊ตฌ๋ถ
from employees e
where exists(select null from employees where e.employee_id=manager_id)
union
select employee_id as ์ฌ์๋ฒํธ, last_name as ์ด๋ฆ, '์ง์' as ๊ตฌ๋ถ
from employees e
where not exists(select null from employees where e.employee_id=manager_id)
order by 3,1;
[๋ฌธ์ 4] ์๊ธฐ ์ ๋ฌดid(job_id)์ ํ๊ท ๊ธ์ฌ๋ฅผ ๋ฐ๋ ์ง์๋ค์ ์กฐํํ์์ค
์กฐ๊ฑด1) ํ๊ท ๊ธ์ฌ๋ 100๋จ์ ์ดํ ์ ์ญ
๊ธ์ฌ๋ ์ธ์๋ฆฌ๋ง๋ค ์ฝค๋ง, $ํ์
์กฐ๊ฑด2) ์ฌ์์ด๋ฆ(last_name), ์ ๋ฌดid(job_id), ์ง๋ฌด(job_title), ๊ธ์ฌ(salary) ๋ก ํ์ํ์์ค
์กฐ๊ฑด3) ๊ธ์ฌ์์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ์์ค
ex7 ) group by rollup : a,b๋ณ ์ง๊ณ(Subtotal ๊ตฌํ๊ธฐ)
๋ถ์๋ณ, ์ง๋ฌดID๋ณ ๊ธ์ฌํ๊ท ๊ตฌํ๊ธฐ(๋์ผ๋ถ์์ ๋ํ ์ง๋ฌด๋ณ ํ๊ท ๊ธ์ฌ)
์กฐ๊ฑด1) ๋ฐ์ฌ๋ฆผํด์ ์์ 2์งธ์๋ฆฌ๊น์ง ๊ตฌํ์์ค
์กฐ๊ฑด2) ์ ๋ชฉ์ Job_title, Department_name, Avg_sal๋ก ํ์ํ์์ค
select department_name, job_title, round(avg(salary), 2) as "Avg_sal"
from employees
join departments using(department_id)
join jobs using(job_id)
group by rollup(department_name, job_title);
ex8 ) group by cube : a๋ณ ์ง๊ณ ๋๋ b๋ณ ์ง๊ณ
๋ถ์๋ณ, ์ง๋ฌดID๋ณ ๊ธ์ฌํ๊ท ๊ตฌํ๊ธฐ(๋ถ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ํ๋ด๋ ํ๊ท ๊ธ์ฌ)
select department_name, job_title, round(avg(salary), 2) as "Avg_sal"
from employees
join departments using(department_id)
join jobs using(job_id)
group by cube(department_name, job_title);
ex9 ) group by grouping sets
์ง๋ฌด๋ณ ํ๊ท ๊ธ์ฌ์ ์ ์ฒด์ฌ์์ ํ๊ท ๊ธ์ฌ๋ฅผ ํจ๊ป ๊ตฌํ์์ค
select job_title, round(avg(salary), 2) as "Avg_sal"
from employees
join departments using(department_id)
join jobs using(job_id)
group by grouping sets((job_title),()); โ () All Rows์ ์ญํ
