DAY-24. Oracle Join
๐ 2022-04-04 - 2
๋ฐ์ดํฐ๋ฒ ์ด์ค
โ ์์ ์ ๋ฃ๊ณ , ๊ฐ์ธ์ด ๊ณต๋ถํ ๋ด์ฉ์ ์ ์ ๊ฒ ์ด๊ธฐ์ ์ค๋ฅ๊ฐ ๋ง์ ์๋ ์์
2022-04-04 -2
1๏ธโฃ Join
- ๋ ๊ฐ ์ด์์ ํ ์ด๋ธ์ ๊ฒฐํฉ ํ๋ ๊ฒ -> ์กฐํฉํ๋ ค๊ณ ํ๋ ํ ์ด๋ธ์์ ๊ฐ ํ ์ด๋ธ์ ๊ณตํต๋ ์ปฌ๋ผ์ ๋ฐ์ดํฐ๋ก ํฉ์ณ์ ํํํ๋ ๊ฒ
- 2์ค for๋ฌธ์ด๋ผ๊ณ ์๊ฐ
๐์กฐ์ธ๋ฌธ์ ์ฌ์ฉํ ๋ ๊ฐ ํ ์ด๋ธ์ด ๊ฐ์ง๊ณ ์๋ ์ปฌ๋ผ๋ช ๊ฐ์ ๋/ ์ปฌ๋ผ๋ช ์ผ ๋ค๋ฅผ ๋
1) ์ปฌ๋ผ๋ช ์ด ๋ค๋ฅผ ๋
๐
//๋ถ์์ฝ๋๊ฐ ๊ฐ์ employee, department ํ
์ด๋ธ์ ์ฌ๋ฒ, ์ฌ์๋ช
, ๋ถ์์ฝ๋, ๋ถ์๋ช
์กฐํ
// oracle ๋ฌธ๋ฒ
// emp_id, emp_name, dept_code๋ employee๋ง ๊ฐ์ง๊ณ ์๊ณ , dept_title์ department๋ง ๊ฐ์ง๊ณ ์๋ค
select emp_id, emp_name, dept_code, dept_title
from employee, department
where dept_code = dept_id;
// ANSI ๋ฌธ๋ฒ
select emp_id, emp_name, dept_code, dept_title
from employee join department
on(dept_code = dept_id);
2) ์ปฌ๋ผ๋ช ์ด ๊ฐ์ ๋
- ๊ฐ ํ ์ด๋ธ์ ๋ณ์นญ์ ์ ํด์ค์ผํ๋ค
๐
// ์ฌ๋ฒ, ์ฌ์๋ช
, ์ง๊ธ์ฝ๋, ์ง๊ธ๋ช
// ๊ฐ ํ
์ด๋ธ์ ๋ณ์นญ์ ์ ํด์ค์ผํ๋ค
// oracle
select
emp_id, emp_name, e.job_code, job_name // e.job_code์ด๋ j.job_code ์ด๋ ์๊ด x -> ์ด์ฐจํผ ๊ฐ์์
from employee e, job j // ๋ณ์นญ ์ ํด์ฃผ๊ธฐ
where e.job_code = j.job_code; // ๋ณ์นญ.์ปฌ๋ผ๋ช
= ๋ณ์นญ.์ปฌ๋ผ๋ช
// ANSI : ๋ ํ
์ด๋ธ์ ๊ฐ์ ์ปฌ๋ผ๋ช
-> using(์ปฌ๋ผ๋ช
)
select
emp_id, emp_name, job_code,job_name
from employee join job
using(job_code);
๐ Join์ ์ข ๋ฅ
Cross Join (์ํธ ์กฐ์ธ)
- ์กฐ์ธ๋๋ ํ ์ด๋ธ์ ์กฐ๊ฑด์ ๊ฑธ์ด์ฃผ์ง ์์์ ๋ชจ๋ ๋ฐ์ดํฐ(๋ชจ๋ ๊ฒฝ์ฐ์ ์๊ฐ) ์กฐํฉ๋์ด ๋์ค๋ ๊ฒฝ์ฐ
- ํ ํ ์ด๋ธ์ ์ด ํ์ * ๋ค๋ฅธ ํ ์ด๋ธ์ ์ด ํ์
๐
select * from employee, department; // oracle
select * from employee cross join department; // ANSI
(Inner) Join (๋ด๋ถ ์กฐ์ธ/ ๊ต์งํฉ)
๊ฐ์ฅ ๋ง์ด ์ฉํ๋ Join ํํ : ํ ์ด๋ธ A์ ํ ์ด๋ธ B์์ ์กฐ๊ฑด์ด ๋ง๋ ๋ฐ์ดํฐ๋ง ๋ณํ
- select ์ปฌ๋ผ.. from ํ ์ด๋ธ A, ํ ์ด๋ธ B where ์กฐ๊ฑด;
- select ์ปฌ๋ผ.. from ํ ์ด๋ธ A inner join ํ ์ด๋ธ B on ์กฐ๊ฑด;
- select ์ปฌ๋ผ.. from ํ ์ด๋ธ A join ํ ์ด๋ธ B on ์กฐ๊ฑด;
๐
select * from employee, department where dept_code = dept_id; // oracle
select * from employee join department on (dept_code = dept_id); // ANSI
๐
// ์ด๋ฌด๋ถ, ํ๊ณ๊ด๋ฆฌ๋ถ์ธ ์ฌ์๋ค์ ์ฌ์๋ช
, ์ด๋ฉ์ผ, ๋ถ์๋ช
, ๋ถ์์ฝ๋๋ง ์กฐํ
// oracle
select
emp_name "์ฌ์๋ช
",
email "์ด๋ฉ์ผ",
dept_title "๋ถ์๋ช
",
dept_id "๋ถ์์ฝ๋"
from employee, department
where dept_code = dept_id and dept_title in ('์ด๋ฌด๋ถ','ํ๊ณ๊ด๋ฆฌ๋ถ');
// ANSI
select
emp_name "์ฌ์๋ช
",
email "์ด๋ฉ์ผ",
dept_title "๋ถ์๋ช
",
dept_code "๋ถ์์ฝ๋"
from employee inner join department
on(dept_code = dept_id)
where dept_title in ('์ด๋ฌด๋ถ','ํ๊ณ๊ด๋ฆฌ๋ถ');
๐
// ์ง์ฑ
์ด ๋๋ฆฌ์ธ ์ฌ๋๋ค์ ๊ธ์ฌ ๋ชฉ๋ก ํ์ธ
// ๋๋ฆฌ๊ธ ์ฌ์๋ค์ ์ฌ์๋ช
, ์ง๊ธ์ฝ๋, ์ง๊ธ๋ช
, ๋ถ์์ฝ๋ (null - ์ธํด), ์๊ธ ์กฐํ
// ์๊ธ -> ์ํ ๊ธฐํธ๋ก
// ์ด๋ฆ ์ค๋ฆ์ฐจ์, ์๊ธ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
// oracle
select * from employee;
select * from job;
select
emp_name "์ฌ์๋ช
",
j.job_code "์ง๊ธ์ฝ๋",
job_name "์ง๊ธ๋ช
",
nvl(dept_code,'์ธํด') "๋ถ์์ฝ๋",
to_char(salary,'L999,999,999') "์ฐ๋ด"
from employee e, job j
where e.job_code = j.job_code and job_name = '๋๋ฆฌ'
order by 1 asc, 4 desc;
// ANSI
select
emp_name "์ฌ์๋ช
",
job_code "์ง๊ธ์ฝ๋",
job_name "์ง๊ธ๋ช
",
nvl(dept_code,'์ธํด') "๋ถ์์ฝ๋",
to_char(salary,'L999,999,999') "์๊ธ"
from employee inner join job
using(job_code)
where job_name = '๋๋ฆฌ'
order by 1,5 desc;
Left Outer Join (์ธ๋ถ ์กฐ์ธ/ ํฉ์งํฉ โ ์ผ์ชฝ ์ธ๋ถ ์กฐ์ธ)
์กฐ์ธํ๋ ํ ์ด๋ธ A์ ํ ์ดํฐ๋ฅผ ๋ชจ๋ ๋ฐํ, ํ ์ด๋ธ B๋ ์กฐ๊ฑด ๊ตฌ๋ฌธ์ ์ผ์นํ๋ ๋ฐ์ดํฐ๋ง ๋ฐํ
โ inner join ์กฐ๊ฑด์ ๊ฑธ๊ฒ ๋๋ฉด ์กฐ๊ฑด์ ๋ง๋ ๋ฐ์ดํฐ๋ง ๋ฐํ
โ left outer join ์กฐ๊ฑด์ด ์ผ์นํ์ง ์์๋ ์ผ์ชฝ ํ ์ด๋ธ์ ๋ฐ์ดํฐ๋ ๋ชจ๋ ๋ฐํ
โ inner join์ ์ฌ์ฉํ ๊ฒฝ์ฐ ์กฐ๊ฑด์ ์ผ์นํ์ง ์๋ ๋ฐ์ดํฐ๊ฐ full๋ก ์กฐํ๋์ง ์์ ๊ฒฝ์ฐ ๋ฐ์ โ left outer join ์ฌ์ฉํ๋ฉด ์ผ์ชฝ์ผ๋ก ๋ ํ ์ด๋ธ์ ์ ์ฒด๊ฐ ๋์ค๊ณ ์ค๋ฅธ์ชฝ์ผ๋ก ๋ ํ ์ด๋ธ์ ์กฐ๊ฑด์ ๋ง๋ ๋ฐ์ดํฐ๋ง ์ถ๋ ฅ
๋ฐฉ๋ฒ)
oracle : select ์ปฌ๋ผ .. from ํ
์ด๋ธ A, ํ
์ด๋ธ B where ์ปฌ๋ผ = ์ปฌ๋ผ(+);
ANSI : select ์ปฌ๋ผ.. from ํ
์ด๋ธ A left outer join ํ
์ด๋ธ B on() / using()
๐
// oracle
select emp_name,dept_title
from employee, department
where dept_code = dept_id(+);
// ANSI
select emp_name, dept_title
from employee left outer join department
on(dept_code = dept_id);
๐
// ๊ธฐ์ ์ง์๋ถ๋ฅผ ์ ์ธํ๊ณ ๋ชจ๋ ๋ถ์ ์ง์์ ์ฌ๋ฒ, ์ฌ์๋ช
, ๋ถ์๋ช
, ์ฐ๋ด ์กฐํ
select emp_id "์ฌ๋ฒ",
emp_name "์ฌ์๋ช
",
nvl(dept_title,'๋ฏธ์ ')"๋ถ์๋ช
",
to_char(salary * 12,'L999,999,999') "์ฐ๋ด"
from employee left outer join department on (dept_code = dept_id)
where dept_title != '๊ธฐ์ ์ง์๋ถ' or dept_code is null
order by ๋ถ์๋ช
,์ฌ๋ฒ,์ฐ๋ด;
- != / = / like / not like ๊ฐ์ equal ์ฐ์ฐ์๋ฅผ ์ด์ฉํ๊ฒ ๋๋ฉด null ๊ฐ์ ๋ํ ์ ๋๋ก ๋ ๋น๊ต๊ฐ ๋์ง ์๊ณ ๋ชจ๋ ์ ์ธ
- null ๊ฐ๋ ํฌํจ์ํค๊ณ ์ถ๋ค๋ฉด ์ฐํ์ ์ผ๋ก ์ถ๊ฐ ์กฐ๊ฑด์ ๋ถ์ฌ์ค -> dept_code is null
Right Outer Join (ํฉ์งํฉ / ์ค๋ฅธ์ชฝ ์ธ๋ถ ์กฐ์ธ)
์กฐ์ธํ๊ณ ์ ํ๋ ํ ์ด๋ธ A์ ํ ์ด๋ธ B๊ฐ ์์ ๋, ํ ์ด๋ธ A๋ ์กฐ๊ฑด ๊ตฌ๋ฌธ์ ์ผ์นํ๋ ๋ฐ์ดํ ๋ ๋ฐํ, ํ ์ด๋ธ B๋ ์กฐ๊ฑด์ ์๊ด์์ด ๋ชจ๋ ๋ฐ์ดํฐ ๋ณํ
๋ฐฉ๋ฒ)
oracle : select ์ปฌ๋ผ .. from ํ
์ด๋ธ A, ํ
์ด๋ธ B where ์ปฌ๋ผ(+) = ์ปฌ๋ผ;
ANSI : select ์ปฌ๋ผ.. from ํ
์ด๋ธ A right outer join ํ
์ด๋ธ B on() / using()
- Left outer Join ๊ณผ ๋ค๋ฅผ๊ฒ ์๋ค
Full Outer Join(ํฉ์งํฉ)
ํ ์ด๋ธ A์ ํ ์ด๋ธ B๋ฅผ ์กฐ์ธํ ๋ ์กฐ๊ฑด์ ๋ง์ง ์๋๋ผ๋ ๋ชจ๋ ๋ฐ์ดํฐ ์ถ๋ ฅ
๐
select emp_name, dept_title
from employee full outer join department
on(dept_code = dept_id);
- null ๊ฐ๋ค๋ ๋ค ์ถ๋ ฅ
Non-Equi Join(๋น๋ฑ๊ฐ ์กฐ์ธ)
์ง์ ํ ์ปฌ๋ผ์ ๊ฐ์ด ์ผ์นํ๋ ๊ฒฝ์ฐ๊ฐ ์๋๋ผ, ๊ฐ์ ๋ฒ์์ ํฌํจ๋๋ ํ์ ์ฐ๊ฒฐ(between, <, >, โค ๋ฑ)
Self Join
๋ค๋ฅธ ํ ์ด๋ธ์ด ์๋ ๊ฐ์ ํ ์ด๋ธ์ ์กฐ์ธํ๋ ๊ฒ
- ๋๊ฐ์ ํ ์ด๋ธ์ join ํ๊ธฐ ๋๋ฌธ์ ๊ฐ ํ ์ด๋ธ์ ๋ณ์นญ์ ๋ฐ๋์ ์ง์
- ์ฌ์ฉํ๋ ค๋ ์ปฌ๋ผ๋ช ์ด ์ด๋ค ๋ณ์นญ์ ํด๋นํ๋ ์ปฌ๋ผ๋ช ์ ์ฌ์ฉํ๋ ค๋๊ฑด์ง ๋ช ํํ ํด์ค์ผํ๋ค
๐
//manager_id์ ํด๋นํ๋ ์ฌ์๋ช
(๋งค๋์ ๋ช
)๋ ์ถ๊ฐํ๊ณ ์ถ๋ค๋ฉด?
// ๋ด๋น ๋งค๋์ ์ ์ฌ๋ฒ์ ํด๋นํ๋ ์ด๋ฆ์ ์ถ๋ ฅ ํ๋ ๋ฌธ์
// ์ค์ํด ๋งค๋์ ์ฌ๋ฒ์ 207 -> ์ง์ ์ฌ๋ฒ -> 207์ ํ์ด์ (๋งค๋์ )
select
e1.emp_id "์ง์์ฌ๋ฒ",
e1.emp_name "์ง์๋ช
",
e1.manager_id "๋งค๋์ ์ฌ๋ฒ",
e2.emp_name "๋งค๋์ ๋ช
"
from employee e1 join employee e2
on(e1.manager_id = e2.emp_id);
๋ค์ค Join
์ฌ๋ฌ ๊ฐ์ ์กฐ์ธ๋ฌธ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ
- ์์์ ์ ์
๐
// employee departement ์กฐ์ธ -> ์ฌ๋ฒ, ์ฌ์๋ช
, ๋ถ์์ฝ๋, ๋ถ์๋ช
select emp_id "์ฌ๋ฒ",
emp_name "์ฌ์๋ช
",
dept_code "๋ถ์์ฝ๋",
dept_title "๋ถ์๋ช
"
from employee join department
on(dept_code = dept_id);
-- ์ฌ๋ฒ, ์ฌ์๋ช
, ๋ถ์์ฝ๋, ๋ถ์๋ช
+ ๋ถ์์ง์ญ๋ช
select * from location;
select * from department;
select
dept_id,
dept_title,
local_name
from department join location
on(location_id = local_code);
// ANSI
select
emp_id "์ฌ๋ฒ",
emp_name "์ฌ์๋ช
",
dept_code "๋ถ์์ฝ๋",
dept_title "๋ถ์๋ช
",
local_name "๋ถ์์ง์ญ๋ช
"
from employee join department on(dept_code = dept_id)
join location on(location_id = local_code);
// oracle
select
emp_id "์ฌ๋ฒ",
emp_name "์ฌ์๋ช
",
dept_code "๋ถ์์ฝ๋",
dept_title "๋ถ์๋ช
",
local_name "๋ถ์์ง์ญ๋ช
"
from employee, department, location
where dept_code = dept_id
and location_id = local_code;
Leave a comment