본문 바로가기
정보관리(데이터베이스, DB)/오라클

[SQL] INNER 조인과 OUTER조인이 무엇인가요?

by 3604 2024. 3. 8.
728x90

출처: https://stanleykou.tistory.com/m/entry/SQL-INNER-%EC%A1%B0%EC%9D%B8%EA%B3%BC-OUTER%EC%A1%B0%EC%9D%B8%EC%9D%B4-%EB%AC%B4%EC%97%87%EC%9D%B8%EA%B0%80%EC%9A%94

http://stackoverflow.com/questions/38549/difference-between-inner-and-outer-joins/38578#38578

 

Q: INNER JOIN OUTER JOIN의 차이가 무엇인가요? 그리고 LEFT JOIN, RIGHT JOIN, FULL JOIN은 또 무엇인가요?

(질문자: cdv)

 

A: 중복이 없는 서로 다른 두 컬럼을 JOIN한다고 가정할 때, 일반적으로는:

- inner join 을 A 와 B에 대해 수행하는 것은, A와 B의 교집합을 말합니다. 벤다이어그램으로 그렸을 때 교차되는 부분입니다.

- outer join을 A와 B에 대해 수행하는 것은, A와 B의 합집합을 말합니다. 벤다이어 그램으로 그렸을 때, 합집합 부분입니다.

 

예제:

두 개의 테이블이 있다고 가정합시다. 컬럼은 1개이고, 데이터는 아래와 같습니다.

A    B
-    -
1    3
2    4
3    5
4    6

(1, 2)는 A에만 있고, (3, 4)는 A와 B 모두에 있으며, (5, 6)은 B에만 있습니다.

 

Inner join

이너 조인, Inner join을 수행하면 두 집합에 모두 있는 열만 남게 됩니다.

select * from a INNER JOIN b on a.a = b.b;
select a.*,b.*  from a,b where a.a = b.b;

a | b
--+--
3 | 3
4 | 4

 

Left outer join

레프트 아우터 조인, Left outer join을 하면, A의 모든 열 더하기 B에 있는 공통부분을 얻게 됩니다.

select * from a LEFT OUTER JOIN b on a.a = b.b;
select a.*,b.*  from a,b where a.a = b.b(+);

a |  b
--+-----
1 | null
2 | null
3 |    3
4 |    4

 

Right outer join

라이트 아우터 조인, Right outer join을 하면 B의 모든 열 더하기 A에 있는 공통부분을 얻게 됩니다.

select * from a RIGHT OUTER JOIN b on a.a = b.b;
select a.*,b.*  from a,b where a.a(+) = b.b;

a    |  b
-----+----
3    |  3
4    |  4
null |  5
null |  6

 

Full outer join

풀 아우터 조인, Full outer join을 하면 A와 B의 합집합을 얻게 됩니다. 

B에는 있는데 A에 없는 (5, 6)은 A에서는 해당 부분이 null 이 되고,

A에는 있는데 B에 없는 (1, 2)는 B에서는 해당 부분이 null 이 됩니다.

select * from a FULL OUTER JOIN b on a.a = b.b;

 a   |  b
-----+-----
   1 | null
   2 | null
   3 |    3
   4 |    4
null |    6
null |    5

 

(답변자: Mark Harrison)

 

A: 여러가지 join이 있고, 아래와 같은 스키마를 이용하실 수 있습니다.

Source: Visual-Representation-of-SQL-Joins explained in detail by C.L. Moffatt

(답변자: Teoman shipahi)

_

What is the difference between INNER JOIN and OUTER JOIN?
How do LEFT JOIN, RIGHT JOIN, and FULL JOIN fit in?

_

3607down voteaccepted
Assuming you're joining on columns with no duplicates, which is a very common case:
  • An inner join of A and B gives the result of A intersect B, i.e. the inner part of a Venn diagramintersection.
  • An outer join of A and B gives the results of A union B, i.e. the outer parts of a Venn diagram union.
Examples
Suppose you have two tables, with a single column each, and data as follows:
Note that (1,2) are unique to A, (3,4) are common, and (5,6) are unique to B.
Inner join
An inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in common.
Left outer join
A left outer join will give all rows in A, plus any common rows in B.
Right outer join
A right outer join will give all rows in B, plus any common rows in A.
Full outer join
A full outer join will give you the union of A and B, i.e. all the rows in A and all the rows in B. If something in A doesn't have a corresponding datum in B, then the B portion is null, and vice versa.

_

Also you can consider the following schema for different join types;
Source: Visual-Representation-of-SQL-Joins explained in detail by C.L. Moffatt
728x90