record logo record

Join

SQL에서 Join의 유형

db-sql-join_1

Demo DataBase

다음과 같은 테이블이 있다고 해보자.

CREATE TABLE animal_A ( 
id VARCHAR(30) NOT NULL PRIMARY KEY, 
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
reg_date TIMESTAMP 
);

CREATE TABLE animal_B ( 
id VARCHAR(30) NOT NULL PRIMARY KEY,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
reg_date TIMESTAMP
); 
-- animal_A
INSERT INTO `animal_A` (`id`, `name`, `type`, `reg_date`) VALUES ('A352713', 'Jewel', 'cat', '2017-08-13 13:50:00');
INSERT INTO `animal_A` (`id`, `name`, `type`, `reg_date`) VALUES ('A352714', 'Cherokee', 'dog', '2017-07-08 09:41:00');
INSERT INTO `animal_A` (`id`, `name`, `type`, `reg_date`) VALUES ('A352715', 'Roll', 'dog', '2015-09-08 06:11:00');
INSERT INTO `animal_A` (`id`, `name`, `type`, `reg_date`) VALUES ('A352716', 'Back', 'cat', '2013-01-28 10:31:00');
-- animal_B
INSERT INTO `animal_B` (`id`, `name`, `type`, `reg_date`) VALUES ('A352413', 'Kevin', 'dog', '2019-01-13 17:50:00');
INSERT INTO `animal_B` (`id`, `name`, `type`, `reg_date`) VALUES ('A352714', 'Cherokee', 'dog', '2017-12-08 10:41:00');
INSERT INTO `animal_B` (`id`, `name`, `type`, `reg_date`) VALUES ('A352715', 'Roll', 'dog', '2015-10-08 08:18:00');
INSERT INTO `animal_B` (`id`, `name`, `type`, `reg_date`) VALUES ('A358711', 'Fall', 'cat', '2010-01-28 12:31:00');

db-sql-join_2

INNER 조인

--Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
-- Inner Join
select 
  a.id, 
  a.name, 
  a.reg_date as a_date, 
  b.reg_date as b_date
from 
  animal_A as a join animal_B as b
on a.id = b.id;

db-sql-join_innerjoin

LEFT OUTER 조인

--Syntax
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
-- Left Join
select 
  a.id, 
  a.name, 
  a.reg_date as a_date, 
  b.reg_date as b_date
from 
  animal_A as a left join animal_B as b
on a.id = b.id;

db-sql-join_leftjoin

RIGHT OUTER 조인

--Syntax
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
-- Right Join
select 
  a.id, 
  a.name, 
  a.reg_date as a_date, 
  b.reg_date as b_date
from 
  animal_A as a right join animal_B as b
on a.id = b.id;

db-sql-join_rightjoin

FULL OUTER JOIN

--Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

카티전 조인 (cross 조인)

--Syntax
SELECT column_name(s)
FROM table1
CROSS JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

셀프 조인

--Syntax
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;

참고

JOIN ON과 WHERE의 차이점

아래와 같은 테이블이 있다고 하자.

CREATE TABLE test1 ( 
id VARCHAR(30) NOT NULL PRIMARY KEY,
name VARCHAR(30) NOT NULL
); 

CREATE TABLE test2 ( 
id VARCHAR(30) NOT NULL PRIMARY KEY,
name VARCHAR(30) NOT NULL
); 

INSERT INTO `test1` (`id`, `name`) VALUES ('1', 'a');
INSERT INTO `test1` (`id`, `name`) VALUES ('2', 'b');
INSERT INTO `test1` (`id`, `name`) VALUES ('3', 'c');

INSERT INTO `test2` (`id`, `name`) VALUES ('1', 'd');
INSERT INTO `test2` (`id`, `name`) VALUES ('2', 'e');

db-sql-join_on_where_1

-- TEST A
SELECT * 
FROM test1 as a left join test2 b
on a.id = b.id and b.name = 'd';

db-sql-join_on_where_test_a

-- TEST B
SELECT * 
FROM test1 as a left join test2 b
on a.id = b.id
where b.name = 'd';

db-sql-join_on_where_test_b


LEFT JOIN으로 차집합을 구현하기

select *
from test1 a left join test2 b
on a.id = b.id
where b.id is null;

db-sql-join_on_where_test_c

조인 활용 예시

References