๋ธ”๋กœ๊ทธ๋กœ ๋Œ์•„๊ฐ€๊ธฐ
2 min read

JOIN

๋ชฉ์ฐจ

Cross Join

select * from table1, table2
  • join ์กฐ๊ฑด ์ƒ๋žต. ๋ฐ์นด๋ฅดํŠธ ๊ณฑ.

Natural Join

select * from departments
natural join locations
  • ๋‘ ํ…Œ์ด๋ธ”์— ๊ณตํ†ต ์ปฌ๋Ÿผ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋ฌต์‹œ์ ์œผ๋กœ ์กฐ์ธ.
  • ์ปฌ๋Ÿผ๋ช… ๊ฒน์ณ์„œ ์—‰๋šฑํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ฃผ์˜.

Inner Join

select * from employees e
inner join departments d
on(e.department_id = d.department_id)
  • inner ํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ.
  • ANSI ํ‘œ์ค€ ๋ฌธ๋ฒ•.
  • on ์ ˆ์— ์กฐ์ธ ์กฐ๊ฑด ์ž‘์„ฑ.(where ์ ˆ์—๋„ ์ž‘์„ฑ ๊ฐ€๋Šฅ)
  • ์ปฌ๋Ÿผ๋ช…์ด ๊ฐ™์€ ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด using ์ ˆ์„ ์‚ฌ์šฉํ•ด๋„ ๋ฌด๋ฐฉํ•จ.
select * from employees
inner join departments
using(department_id)

Oracle ๋ฐฉ์‹

select t1.col1, t1.col2, t2.col1
from table1 t1, table2 t2
where t1.col3 = t2.col3
  • from ์ ˆ์— ํ•„์š”๋กœ ํ•˜๋Š” ํ…Œ์ด๋ธ”์„ ๋ชจ๋‘ ์ ๋Š”๋‹ค. (alias ์‚ฌ์šฉ ๊ฐ€๋Šฅ)
  • ์ ์ ˆํ•œ ์กฐ๊ฑด์„ where ์ ˆ์— ๋ถ€์—ฌํ•œ๋‹ค.

Outer Join

select *
from employees e
left outer join departments d
on(e.department_id = d.department_id)
  • left(right) outer join: ์™ผ์ชฝ(์˜ค๋ฅธ์ชฝ)์˜ ๋ชจ๋“  ํŠœํ”Œ์€ ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ”์— ๋‚˜ํƒ€๋‚จ.
  • full outer join: ์–‘์ชฝ ๋ชจ๋‘ ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ”์— ๋‚˜ํƒ€๋‚จ.
  • outer ํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ.

Self Join

select e.name as '์‚ฌ์› ์ด๋ฆ„', m.name as '์ƒ์‚ฌ ์ด๋ฆ„'
from employees e
join employees m
on(e.manager_id = m.employee_id)
  • ๊ฐ™์€ ํ…Œ์ด๋ธ”์— alias ๋‹ค๋ฅด๊ฒŒ ์ฃผ๊ณ  join