SQL - JOIN 정리
예시 테이블
Table: employees
+--------+--------------+------------+---------+
| emp_id | emp_name | hire_date | dept_id |
+--------+--------------+------------+---------+
| 1 | Ethan Hunt | 2001-05-01 | 4 |
| 2 | Tony Montana | 2002-07-15 | 1 |
| 3 | Sarah Connor | 2005-10-18 | 5 |
| 4 | Rick Deckard | 2007-01-03 | 3 |
| 5 | Martin Blank | 2008-06-24 | NULL |
+--------+--------------+------------+---------+
Table: departments
+---------+------------------+
| dept_id | dept_name |
+---------+------------------+
| 1 | Administration |
| 2 | Customer Service |
| 3 | Finance |
| 4 | Human Resources |
| 5 | Sales |
+---------+------------------+
1. Inner Join
1-1) Cross Join (교차 조인)
- 특별한 조건없이 테이블 A의 각 행과 테이블 B의 각행을 다 조합한 결과(곱하기)
SELECT t1.emp_id, t1.emp_name, t1.hire_date, t2.dept_name
FROM employees AS t1
CROSS JOIN departments AS t2;
Cross Join Result
+--------+--------------+------------+------------------+
| emp_id | emp_name | hire_date | dept_name |
+--------+--------------+------------+------------------+
| 1 | Ethan Hunt | 2001-05-01 | Administration |
| 2 | Tony Montana | 2002-07-15 | Administration |
| 3 | Sarah Connor | 2005-10-18 | Administration |
| 4 | Rick Deckard | 2007-01-03 | Administration |
| 5 | Martin Blank | 2008-06-24 | Administration |
| 1 | Ethan Hunt | 2001-05-01 | Customer Service |
| 2 | Tony Montana | 2002-07-15 | Customer Service |
| 3 | Sarah Connor | 2005-10-18 | Customer Service |
| 4 | Rick Deckard | 2007-01-03 | Customer Service |
| 5 | Martin Blank | 2008-06-24 | Customer Service |
| 1 | Ethan Hunt | 2001-05-01 | Finance |
| 2 | Tony Montana | 2002-07-15 | Finance |
| 3 | Sarah Connor | 2005-10-18 | Finance |
| 4 | Rick Deckard | 2007-01-03 | Finance |
| 5 | Martin Blank | 2008-06-24 | Finance |
| 1 | Ethan Hunt | 2001-05-01 | Human Resources |
| 2 | Tony Montana | 2002-07-15 | Human Resources |
| 3 | Sarah Connor | 2005-10-18 | Human Resources |
| 4 | Rick Deckard | 2007-01-03 | Human Resources |
| 5 | Martin Blank | 2008-06-24 | Human Resources |
| 1 | Ethan Hunt | 2001-05-01 | Sales |
| 2 | Tony Montana | 2002-07-15 | Sales |
| 3 | Sarah Connor | 2005-10-18 | Sales |
| 4 | Rick Deckard | 2007-01-03 | Sales |
| 5 | Martin Blank | 2008-06-24 | Sales |
+--------+--------------+------------+------------------+
1-2) Inner Join
- 가장 많이 사용된다.
- 2개의 테이블의 컬럼을 합쳐 새로운 테이블을 생성한다.
SELECT t1.emp_id, t1.emp_name, t1.hire_date, t2.dept_name
FROM employees AS t1
INNER JOIN departments AS t2
ON t1.dept_id = t2.dept_id
ORDER BY emp_id;
Inner Join Result
+--------+--------------+------------+-----------------+
| emp_id | emp_name | hire_date | dept_name |
+--------+--------------+------------+-----------------+
| 1 | Ethan Hunt | 2001-05-01 | Human Resources |
| 2 | Tony Montana | 2002-07-15 | Administration |
| 3 | Sarah Connor | 2005-10-18 | Sales |
| 4 | Rick Deckard | 2007-01-03 | Finance |
+--------+--------------+------------+-----------------+
2. Outer Join
- 조건문에 만족하지 않는 행도 표시해주는 조인
- 조인했을 때, 한쪽의 테이블에 데이터가 없어도 조인 결과에는 포함된다.
2-1) Left Outer Join
- 테이블 A의 모든 데이터와 테이블 B와 매칭 되는 레코드를 포함하는 조인
SELECT t1.emp_id, t1.emp_name, t1.hire_date, t2.dept_name
FROM employees AS t1
LEFT JOIN departments AS t2
ON t1.dept_id = t2.dept_id
ORDER BY emp_id;
Left Outer Join Result
+--------+--------------+------------+-----------------+
| emp_id | emp_name | hire_date | dept_name |
+--------+--------------+------------+-----------------+
| 1 | Ethan Hunt | 2001-05-01 | Human Resources |
| 2 | Tony Montana | 2002-07-15 | Administration |
| 3 | Sarah Connor | 2005-10-18 | Sales |
| 4 | Rick Deckard | 2007-01-03 | Finance |
| 5 | Martin Blank | 2008-06-24 | NULL |
+--------+--------------+------------+-----------------+
2-2) Right Outer Join
- 오른쪽 외부 조인은 테이블 B의 모든 데이터와 테이블 A와 매칭이 되는 레코드를 포함하는 조인
SELECT t1.emp_id, t1.emp_name, t1.hire_date, t2.dept_name
FROM employees AS t1
RIGHT JOIN departments AS t2
ON t1.dept_id = t2.dept_id
ORDER BY dept_name;
Right Outer Join Result
+--------+--------------+------------+------------------+
| emp_id | emp_name | hire_date | dept_name |
+--------+--------------+------------+------------------+
| 2 | Tony Montana | 2002-07-15 | Administration |
| NULL | NULL | NULL | Customer Service |
| 4 | Rick Deckard | 2007-01-03 | Finance |
| 1 | Ethan Hunt | 2001-05-01 | Human Resources |
| 3 | Sarah Connor | 2005-10-18 | Sales |
+--------+--------------+------------+------------------+
2-3) Full Outer Join
- Oracle, MySQL은 지원하지 않음
- Left, Right Join, Union, Union All 등을 같이 써야함
## Union All
SELECT t1.emp_id, t1.emp_name, t1.hire_date, t2.dept_name
FROM employees AS t1
LEFT JOIN departments AS t2
ON t1.dept_id = t2.dept_id
UNION ALL
SELECT t1.emp_id, t1.emp_name, t1.hire_date, t2.dept_name
FROM employees AS t1
RIGHT JOIN departments AS t2
ON t1.dept_id = t2.dept_id ORDER BY emp_name;
+--------+--------------+------------+------------------+
| emp_id | emp_name | hire_date | dept_name |
+--------+--------------+------------+------------------+
| NULL | NULL | NULL | Customer Service |
| 1 | Ethan Hunt | 2001-05-01 | Human Resources |
| 1 | Ethan Hunt | 2001-05-01 | Human Resources |
| 5 | Martin Blank | 2008-06-24 | NULL |
| 4 | Rick Deckard | 2007-01-03 | Finance |
| 4 | Rick Deckard | 2007-01-03 | Finance |
| 3 | Sarah Connor | 2005-10-18 | Sales |
| 3 | Sarah Connor | 2005-10-18 | Sales |
| 2 | Tony Montana | 2002-07-15 | Administration |
| 2 | Tony Montana | 2002-07-15 | Administration |
+--------+--------------+------------+------------------+
참조 : https://velog.io/@kdh10806/DataBase-Join
https://www.tutorialrepublic.com/sql-tutorial/