[DB] ORACLE - 5์ผ


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์˜ ์—ญํ•