DAY-25. Oracle, ์๋ธ์ฟผ๋ฆฌ
๐ 2022-04-05
๋ฐ์ดํฐ๋ฒ ์ด์ค
โ ์์ ์ ๋ฃ๊ณ , ๊ฐ์ธ์ด ๊ณต๋ถํ ๋ด์ฉ์ ์ ์ ๊ฒ ์ด๊ธฐ์ ์ค๋ฅ๊ฐ ๋ง์ ์๋ ์์
2022-04-05
๐ set Operator (์งํฉ ์ฐ์ฐ์)
- ๋ ๊ฐ ์ด์์ ํ ์ด๋ธ์ ์กฐ์ธ ์์ด ์ฐ๊ด๋ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ๋ฐฉ์
- ๊ฐ ํ ์ด๋ธ์์ ๋ฐํ๋ ๊ฒฐ๊ณผ๊ฐ์ ํ๋์ ํ ์ด๋ธ๋ก ๊ฒฐํฉํ๋ ๋ฐฉ์
- ์ฌ๊ฑฐ ๊ฐ์ sql๋ฌธ์ ์ฌ์ฉํด์ ํ๋์ ํ ์ด๋ธ๋ก ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํด์ผ ํ๋ ๊ฒฝ์ฐ
์กฐ๊ฑด
- select ์ ์์ ์กฐํํ๋ ค๋ โ์ปฌ๋ผ์ ์๊ฐ ๋์ผโ
- select ์ ์ ์ข ์ผ ์์น์ ์กด์ฌํ๋ ์ปฌ๋ผ์ โ๋ฐ์ดํฐ ํ์ ์ด ์ํธ ํธํ ๊ฐ๋ฅโ
UNION(ํฉ์งํฉ)
์ค๋ณต๋ ๋ฐ์ดํฐ๋ฅผ ์ ๊ฑฐํ๊ณ ์ฒซ๋ฒ์งธ ์ปฌ๋ผ์ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ํ์ฌ ๋ฐ์ดํฐ๋ฅผ ๋ณด์ฌ ์ค
- ๊ฐ ํ ์ด๋ธ์์ ์กฐํํ๋ ์ปฌ๋ผ์๊ฐ ๋ค๋ฅด๋ฉด ์๋ฌ ๋ฐ์
- ์กฐํํ๋ ค๋ ์ปฌ๋ผ์ด ์๋ก ์ํธํธํ ๋ถ๊ฐํ(๊ฐ์ ํ์ ์ด ์๋๋ฉด) ๋ฐ์ดํฐ์ด๋ฉด union ์ฌ์ฉ x
๐ก UNION์ ์ปฌ๋ผ์ ์์ ๋ฐ์ดํฐ๊ฐ ๊ฐ์์ผ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค
SELECT no_a from A // 1,5,3,4,2
SELECT no_b from B; // 7,6,3,8,5,9
SELECT no_a from A
UNION
SELECT no_b from B;
// 1,2,3,4,5,6,7,8,9 (์ค๋ณต๋ ๋ฐ์ดํฐ๋ ์ ๊ฑฐ ๋๊ณ ์ถ๋ ฅ)
UNION ALL (ํฉ์งํฉ)
์ข ๋ณต๋ ๋ฐ์ดํฐ์ ๊ฑฐ๋ฅผ ์ํ๊ณ , ์ ๋ ฌ ์์ด ๋ฐ์ดํฐ๋ฅผ ๊ทธ๋๋ก ๋ณด์ฌ์ค๋ค
โ ์กฐํ๋ ๊ฒฐ๊ณผ๊ฐ์ ์ฒซ๋ฒ์งธ ํ ์ด๋ธ ์๋๋ก ๋๋ฒ์งธ ํ ์ด๋ธ ๊ฒฐ๊ณผ๊ฐ์ ๊ทธ๋๋ก ์ด์ด๋ถ์ด๋ ์.
SELECT no_a from A // 1,5,3,4,2
SELECT no_b from B; // 7,6,3,8,5,9
SELECT no_a from A
UNION ALL
SELECT no_b from B; // 5 3 4 2 7 6 3 8 5 9
INTERSECT (๊ต์งํฉ)
๋ ํ ์ด๋ธ์ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ค ๊ณตํต๋ ๊ฒฐ๊ณผ ๊ฐ๋ง ๋ณด์ฌ ์ค.
SELECT no_a from A // 1,5,3,4,2
SELECT no_b from B; // 7,6,3,8,5,9
SELECT no_a from A
INTERSECT
SELECT no_b from B; // 3 5
MINUS (์ฐจ์งํฉ)
๋ ํ ์ด๋ธ์ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ค ๊ณตํต๋ ์์๋ฅผ ๋บ ํ ํ ์ด๋ธ์ ๊ฒฐ๊ณผ๊ฐ๋ง ๋ณด์ฌ์ค
SELECT no_a from A // 1,5,3,4,2
SELECT no_b from B; // 7,6,3,8,5,9
SELECT no_a from A
MINUS
SELECT no_b from B; // 1,2,4 (์ค๋ณต ์ ๊ฑฐ ํ A๋ถ๋ถ์ ๋ณด์ฌ์ค)
SELECT no_b from B
MINUS
SELECT no_a from A; // 6,7,8,9(์ค๋ณต ์ ๊ฑฐ ํ B๋ถ๋ถ์ ๋ณด์ฌ์ค)
๐ ์๋ธ์ฟผ๋ฆฌ (SubQuery)
ํ๋์ sql ๋ฌธ์์ ํฌํจ๋ ๋ค๋ฅธ sql๋ฌธ ()์์ sql ๋ฌธ ์ฝ์
โ ๋ฉ์ธ ์ฟผ๋ฆฌ๊ฐ ์๋ธ ์ฟผ๋ฆฌ๋ฅผ ํฌํจํ๋ ์ข ์์ ์ธ ๊ด๊ณ
โ ์๊ดํธ๋ก ๋ฌถ์ด์ค / ์ฐ์ฐ์์ ์ค๋ฅธ์ชฝ์ ์์น / order by ์ ์ subquery๋ด์์ ์ฌ์ฉ ๋ถ๊ฐ
select
emp_id, emp_name
from employee
where emp_id = (select manager_id from employee where emp_name ='์ ์ง์ฐ');
// select manager_id from employee where emp_name ='์ ์ง์ฐ'์ ๊ฐ์ด 214๊ฐ ๋์ถ๋์ด
// where emp_id = 214๊ฐ ๋๋ค.
๋จ์ผํ ์๋ธ์ฟผ๋ฆฌ
์๋ธ์ฟผ๋ฆฌ ์กฐํ ๊ฒฐ๊ณผ ๊ฐ์ด 1๊ฐ(ํ)์ผ ๊ฒฝ์ฐ
๐
// ์ ์ง์์ ๊ธ์ฌ ํ๊ท ๋ณด๋ค ๊ธ์ฌ๋ฅผ ๋ง์ด ๋ฐ๋ ์ฌ์์ ์ฌ๋ฒ, ์ฌ์๋ช
, ์ง๊ธ์ฝ๋, ์๊ธ ์กฐํ
select
emp_id , emp_name , job_code , salary
from employee
where salary > 3047662;
// ์ ๋๊ฐ์ ์ฟผ๋ฆฌ๋ฌธ์ ํ๋๋ก
select
emp_id , emp_name , job_code , salary
from employee
where salary > (select floor(avg(salary)) from employee);
๐
// ์ค์ํด ์ง์๊ณผ ๊ธ์ฌ๊ฐ ๊ฐ์ ์ฌ์๋ค์ ๊ฒ์ -> ์ฌ๋ฒ, ์ฌ์๋ช
, ๊ธ์ฌ ์ถ๋ ฅ
select
emp_id, emp_name, salary
from employee
where salary = (select salary from employee where emp_name = '์ค์ํด')
and emp_name != '์ค์ํด';
๋ค์ฃผํ ์๋ธ์ฟผ๋ฆฌ
์๋ธ์ฟผ๋ฆฌ ์กฐํ ๊ฒฐ๊ณผ๊ฐ ์ฌ๋ฌ๊ฐ(์ฌ๋ฌ ํ) ์ผ ๋
โ ๋ค์คํ ์๋ธ์ฟผ๋ฆฌ ์์๋ = , ! = ์ฐ์ฐ์ ์ฌ์ฉ ๋ถ๊ฐ
โ in, not in, any, all, exists ๋ฑ์ ์ด์ฉ
๐
//์ฐจํ์ฐ, ์ ์ง์ฐ ์ฌ์์ ๊ธ์ฌ๋ฑ๊ธ(sal_level)๊ณผ ๊ฐ์ ๋ฑ๊ธ์ ๊ฐ์ง ์ฌ์์ ์ง๊ธ๋ช
, ์ฌ์๋ช
, ๊ธ์ฌ๋ฑ๊ธ ์ถ๋ ฅ
select * from job;
select emp_name , sal_level from employee where emp_name in('์ฐจํ์ฐ','์ ์ง์ฐ');
select emp_name, sal_level from employee where sal_level in ('S5','S4');
select
job_name "์ง๊ธ๋ช
",
emp_name "์ฌ์๋ช
",
sal_level "๊ธ์ฌ๋ฑ๊ธ"
from employee join job
using(job_code)
where sal_level in (select sal_level from employee where emp_name in('์ฐจํ์ฐ','์ ์ง์ฐ'));
any
์๋ธ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ค์ ํ๋๋ผ๋ ์ฐธ์ด๋ผ๊ณ ํ๋ค๋ฉด ์ฐธ (or)
๊ฐ > any(1,2,3) : ์ผ์ชฝ์ ์๋ ๊ฐ์ด ์ค๋ฅธ์ชฝ์ ์ต์๊ฐ๋ณด๋ค ํฌ๋ฉด ๋๋ค.-> ๊ฐ์ด 1 ๋ณด๋ค ํฌ๋ฉด ์ฐธ
๊ฐ < any(1,2,3) : ์ผ์ชฝ์ ์๋ ๊ฐ์ด ์ค๋ฅธ์ชฝ์ ์ต๋๊ฐ๋ณด๋ค ์์ผ๋ฉด ๋๋ค -> ๊ฐ์ด 3๋ณด๋ค ์์ผ๋ฉด ์ฐธ
๊ฐ = any(1,2,3) : in๊ณผ ๊ฐ์ ์๋ฏธ
๊ฐ != any(1,2,3) : not in ๊ณผ ๊ฐ์ ์๋ฏธ
๐
// ๊ธ์ฌ๊ฐ 200๋ง์ ํน์ 300๋ง์๋ณด๋ค ํฐ ์ฌ๋์ ์ฌ์๋ช
, ๊ธ์ฌ ์ถ๋ ฅ
select
emp_name,salary
from employee
where salary > any(2000000,3000000); // salary๊ฐ 2000000๋ณด๋ค ํฌ๋ฉด ์ถ๋ ฅ
๐
// D1, D5์ ๋ถ์์ฝ๋๋ฅผ ๊ฐ์ง ์ฌ์๋ค์ ๊ธ์ฌ๋ณด๋ค ์ ๊ฒ ๋ฐ๋ ์ฌ์๋ค์ ์ฌ์๋ช
, ๊ธ์ฌ, ๋ถ์์ฝ๋ ์ถ๋ ฅ
// D1, D5 ๋ถ์ ์ง์์ ์ถ๋ ฅ X
// ์ ๋ ฌ์ ๋ถ์์ฝ๋ ๊ธฐ์ค ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
// 1 ๋ฒ์งธ ๋ฐฉ๋ฒ
select
emp_name, salary, nvl(dept_code,'์ธํด') "๋ถ์์ฝ๋"
from employee
where salary < any(select salary from employee where dept_code in('D1','D5'))
and dept_code not in ('D1','D5') or dept_code is null
// null ๊ฐ๋ ์ถ๋ ฅํด์ฃผ๋ ์ฝ๋ dept_code is null
order by 3;
// 2 ๋ฒ์งธ ๋ฐฉ๋ฒ
select
emp_name, salary, dept_code "๋ถ์์ฝ๋"
from employee
where salary < any(select salary from employee where dept_code in('D1','D5'))
and nvl(dept_code,'์์') not in ('D1','D5')
order by 3;
๐ก null ๊ฐ ๋น๊ต๋ is null ํน์ is not null๋ก๋ง ๋น๊ต ๊ฐ๋ฅ
all
์๋ธ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ค์ ๋ชจ๋ ๊ฒ์ด ์ฐธ์ด์ฌ์ผ๋ง ํจ
๊ฐ > all(1,2,3) : ์ผ์ชฝ์ ์๋ ๊ฐ์ด ์ค๋ฅธ์ชฝ์ ์๋ ์ต๋๊ฐ ๋ณด๋ค ์ปค์ผํ๋ค
๊ฐ < all(1,2,3) : ์ผ์ชฝ์ ์๋ ๊ฐ์ด ์ค๋ฅธ์ชฝ์ ์๋ ์ต์๊ฐ ๋ณด๋ค ์์์ผํ๋ค
๊ฐ = all(1,2,3) : ์ผ์ชฝ์ ์๋ ๊ฐ์ด ์ค๋ฅธ์ชฝ์ ์๋ ๊ฐ์ ๋ชจ๋์ ๋๊ฐ์์ผํ๋ค -> and ์ฐ์ฐ์
๋ค์ค์ด ์๋ธ์ฟผ๋ฆฌ
์๋ธ์ฟผ๋ฆฌ ์กฐํ ๊ฒฐ๊ณผ ์์ด ์ฌ๋ฌ๊ฐ(์ด) ์ผ ๋
๐
// ํด์ฌํ ์ฌ์ง์ -> ๊ฐ์ ๋ถ์, ๊ฐ์ ์ง๊ธ์ ํด๋นํ๋ ์ฌ์์ ์ฌ์๋ช
, ์ง๊ธ์ฝ๋, ๋ถ์์ฝ๋, ์
์ฌ์ผ ์กฐํ
select dept_code, job_code
from employee
where ent_yn ='Y'
and substr(emp_no,8,1) = 2;
select
emp_name, job_code, dept_code, hire_date
from employee
where dept_code ='D8' and job_code = 'J6';
select
emp_name, job_code, dept_code, hire_date
from employee
where (dept_code ,job_code) in (select dept_code, job_code from employee where ent_yn ='Y' and substr(emp_no,8,1) = 2);
- dept_code์ job_code โ ์ด์ด ๋๊ฐ๋ค โ ๋ค์ค์ด ์๋ธ์ฟผ๋ฆฌ ์ด์ฉ โ where ์ ์์ () in ()์ผ๋ก ํ์ฉ
๐
๊ธฐ์ ์ง์๋ถ์ด๋ฉด์ ๊ธ์ฌ๊ฐ 200๋ง์์ธ ์ง์์ ์ฌ์๋ช
, ๋ถ์์ฝ๋, ๊ธ์ฌ, ๋ถ์์ ์ง์ญ๋ช
์ถ๋ ฅ
select * from employee join department on(dept_code = dept_id) where dept_title ='๊ธฐ์ ์ง์๋ถ' and salary = 2000000;
// ๋ฐฉ๋ฒ 1
select
emp_name,dept_code,salary,local_name
from employee join department
on(dept_code = dept_id) join location
on(location_id = local_code)
where dept_title ='๊ธฐ์ ์ง์๋ถ' and salary = 2000000;
// ๋ฐฉ๋ฒ 2 -> department๋ฅผ ์ฐ๊ฒฐ ํค๋ก ํด์ ์ฌ์ฉ
select
emp_name,dept_code,salary,local_name,local_code
from employee, location
where (dept_code, local_code) in (select dept_id, location_id from department where dept_title = '๊ธฐ์ ์ง์๋ถ')
and salary = 2000000;
// employee์ location์ ์ง์ ์ฐ๊ฒฐํ ํค๊ฐ ์๊ธฐ์ department์ฌ์ฉ
join์์๋ group by ๊ฐ๋ฅ
//๋ถ์์ฝ๋, ๋ถ์๋ช
, ๋ถ์๋ณ ํ๊ท ๊ธ์ฌ, ๋ถ์๋ณ ์ธ์์
select
nvl(dept_code,'์์') "๋ถ์์ฝ๋",
nvl(dept_title,'์์') "๋ถ์๋ช
",
floor(avg(salary)) "๋ถ์๋ณ ํ๊ท ๊ธ์ฌ",
count(*) "๋ถ์๋ณ ์ธ์์"
from employee left outer join department
on(dept_code = dept_id)
group by dept_code, dept_title // dept_code๋ก๋ง ํ๋ฉด ์ค๋ฅ ๋ฐ์ dept_title(department์ ์์)๋ ํ์
order by 1;
Leave a comment