DAY-24. Oracle Join

4 minute read

๐Ÿ” 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);

1.png

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);

2.png

๋‹ค์ค‘ 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;

3.png

Categories:

Updated:

Leave a comment