๐Ÿคณ [ORACLE]_๋ณต์Šต

โ€์ „ํฌ์ฃผยท2023๋…„ 2์›” 27์ผ
0

๐Ÿคณ 8์žฅ ์—ฐ์Šต๋ฌธ์ œ ์˜ค๋‹ต ๋…ธํŠธ

์—ฐ์Šต๋ฌธ์ œ, ์‹ค์Šต๋ฌธ์ œ ๊ณต๋ถ€ ๋ฐฉ๋ฒ•
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);

๐Ÿคณ ์‹ค์Šต ์—ฐ์Šต๋ฌธ์ œ ์˜ค๋‹ต ๋…ธํŠธ

๐Ÿคณ ref.

profile
heejoojeon@daou.co.kr

0๊ฐœ์˜ ๋Œ“๊ธ€