SQL join - swkim0128/PARA GitHub Wiki


type: Database archive: false

join


๋‘˜ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ํ…Œ์ด๋ธ” ์กฐ์ธ์ด ํ•„์š”.

์ผ๋ฐ˜์ ์œผ๋กœ ์กฐ์ธ ์กฐ๊ฑด์„ ํฌํ•จํ•˜๋Š” where ์ ˆ์„ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

์กฐ์ธ ์กฐ๊ฑด์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐํ…Œ์ด๋ธ”์˜ PK ๋ฐ FK๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.

join์˜ ์ข…๋ฅ˜

  • inner join
  • outer join
    • left outer join
    • right outer join

join ์กฐ๊ฑด์˜ ๋ช…์‹œ์— ๋”ฐ๋ฅธ ๊ตฌ๋ถ„.

  • natural join
  • cross join(full join, cartesian join)

join์‹œ ์ฃผ์˜

  • ์กฐ์ธ์˜ ์ฒ˜๋ฆฌ๋Š” ์–ด๋А ํ…Œ์ด๋ธ”์„ ๋จผ์ € ์ฝ์„์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”(์ฒ˜๋ฆฌํ•  ์ž‘์—…๋Ÿ‰์ด ์ƒ๋‹นํžˆ ๋‹ฌ๋ผ์ง„๋‹ค.)
  • inner join : ์–ด๋А ํ…Œ์ด๋ธ”์„ ๋จผ์ € ์ฝ์–ด๋„ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์ง€์ง€ ์•Š์•„ mysql ์˜ตํ‹ฐ๋งˆ์ด์ €๊ฐ€ ์กฐ์ธ์˜ ์ˆœ์„œ๋ฅผ ์กฐ์ ˆํ•ด์„œ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์ตœ์ ํ™”๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • outer join : ๋ฐ˜๋“œ์‹œ outer๊ฐ€ ๋˜๋Š” ํ…Œ์ด๋ธ”์„ ๋จผ์ € ์ฝ์–ด์•ผ ํ•˜๋ฏ€๋กœ ์˜ตํ‹ฐ๋งˆ์ด์ €๊ฐ€ ์กฐ์ธ ์ˆœ์„œ๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์—†๋‹ค.

inner join


๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ join์˜ ์ข…๋ฅ˜์ด๋ฉฐ ๊ต์ง‘ํ•ฉ์ด๋‹ค.

๋™๋“ฑ ์กฐ์ธ(uqui-join)์ด๋ผ๊ณ ๋„ ํ•˜๋ฉฐ, N๊ฐœ์˜ ํ…Œ์ด๋ธ” ์กฐ์ธ ์‹œ N - 1๊ฐœ์˜ ์กฐ์ธ ์กฐ๊ฑด์ด ํ•„์š”ํ•จ.

> select col1, col2, ..., colN
	from table1 inner join table2
	on table1.column = table2.column;

> select alias1.col1, alias1.col2, ..., alias2.colN
	from table1 as asias1 inner join table2 as alias2
	on alias1.column = alias2.column;
  • on์„ ์ด์šฉํ•œ join ์กฐ๊ฑด ์ง€์ •.

    !Untitled 40.png

  • using์„ ์ด์šฉํ•œ join ์กฐ๊ฑด ์ง€์ •.

    > select col1, col2, ..., colN
    	from table1 join table2
    	using (๊ณตํ†ต column);

    !Untitled 1 18.png

natural join


> select col1, col2, ..., colN
	from table1 natural join table2;

outer join


left outer join, right outer join, full outer join์œผ๋กœ ๊ตฌ๋ถ„ ๋จ.

์–ด๋А ํ•œ์ชฝ ํ…Œ์ด๋ธ”์—๋Š” ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ ๋‹ค๋ฅธ ์ชฝ ํ…Œ์ด๋ธ”์—๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ๊ทธ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฒ€์ƒ‰๋˜์ง€ ์•Š๋Š” ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.

!Untitled 2 17.png

left outer join

  • ์™ผ์ชฝ ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€์œผ๋กœ join ์กฐ๊ฑด์— ์ผ์น˜ ํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๊นŒ์ง€ ์ถœ๋ ฅ

    > select col1, col2, ..., colN
    	from table1 left outer join table2
    	on or using;

right outrer join

  • ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€์œผ๋กœ join์กฐ๊ฑด์— ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๊นŒ์ง€ ์ถœ๋ ฅ.
> select col1, col2, ..., colN
	from table1 right outer join table2
	on or using;

full outer join

  • ์–‘์ชฝ ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€์œผ๋กœ join ์กฐ๊ฑด์— ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๊นŒ์ง€ ์ถœ๋ ฅ.
> select col1, col2, ... colN
	from table1 full outer join table2
	on or using

self join

  • ๊ฐ™์€ ํ…Œ์ด๋ธ”๋ผ๋ฆฌ join
  • ๋ชจ๋“  ์‚ฌ์›์˜ ์‚ฌ๋ฒˆ, ์ด๋ฆ„, ๋งค๋‹ˆ์ €์‚ฌ๋ฒˆ, ๋งค๋‹ˆ์ € ์ด๋ฆ„
select e.employee_id, e.first_name, m.employee_id, m.first_name
from employees e inner join employees m
on e.manager_id = m.employee_id;

None-Equi join

  • table์˜ pk, fk๊ฐ€ ์•„๋‹Œ ์ผ๋ฐ˜ column์„ join ์กฐ๊ฑด์œผ๋กœ ์ง€์ •.
  • ๋ชจ๋“  ์‚ฌ์›์˜ ์‚ฌ๋ฒˆ, ์ด๋ฆ„, ๊ธ‰์—ฌ, ๊ธ‰์—ฌ ๋“ฑ๊ธ‰.

!Untitled 3 16.png

โš ๏ธ **GitHub.com Fallback** โš ๏ธ