M42/docs: view doc:89/v3

SQL join에 관한 정리

v1Ossia
v2Ossia
v3Ossia
요약

없음

다음의 두 개의 테이블을 이용하자. SQL 쿼리는 PostgreSQL 기준이다.

테이블 a

id

small

1

a

1

aa

2

b

3

c

테이블 b

id

big

1

A

2

B

2

BB

4

D

a, b에 공통으로 1, 2는 있으나 3은 a에만 있고 4는 b에만 있음에 주목하라. 또 a는 1이 두 개, b는 2가 두 개 있다.

-- drop table if exists a;
-- drop table if exists b;

create table a(
	id int,
	small text
);

insert into a(id, small) values
	(1, 'a'), (1, 'aa'), (2, 'b'), (3, 'c');

create table b(
	id int,
	big text
);

insert into b(id, big) values
	(1, 'A'), (2, 'B'), (2, 'BB'), (4, 'D');

inner join

select * from a inner join b on a.id = b.id;

select * from b inner join a on a.id = b.id;

select * from a, b where a.id = b.id;

id

small

big

1

a

A

1

aa

A

2

b

B

2

b

BB

left outer join

select * from a left join b on a.id = b.id;

id

small

big

1

a

A

1

aa

A

2

b

B

2

b

BB

3

c

null

right outer join

select * from a right join b on a.id = b.id;

id

small

big

1

a

A

1

aa

A

2

b

B

2

b

BB

4

null

D

full outer join

select * from a full join b on a.id = b.id;

id

small

big

1

a

A

1

aa

A

2

b

B

2

b

BB

3

c

null

4

null

D