서로 다른 테이블을 연결하여, 큼직~하게 테이블 하나 만든다고 생각하면된다.
그럼 여기서 어떻게 테이블을 연결할까?
그 방법에 대하여 Inner , Outer, Self 등이 존재한다.

Different Types of SQL JOINs

Here are the different types of the JOINs in SQL:

  • (INNER) JOIN: Returns records that have matching values in both tables
    둘 다 있는 것만 조인됨
  • LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table
    왼쪽 테이블에 오른쪽 테이블을 붙여서 조인(왼쪽 테이블 행만큼)
  • RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table
    오른쪽테이블에 왼쪽 테이블을 붙여서 조인 (오른쪽 테이블 행만큼)
  • FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
    양쪽에 서로 다 붙임 → 없는거 Null 쭈르륵 생김

SQL INNER JOIN

The INNER JOIN keyword selects records that have matching values in both tables.

양쪽 테이블 모두 갖고있는 것들만 JOIN 한다

SQL LEFT JOIN Keyword

The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match.

왼쪽테이블의 모든 칸에 오른쪽테이블의 JOIN이 실행되며, 오른쪽에 결과값이 없는것은 NULL이 매핑된다
Note: In some databases LEFT JOIN is called LEFT OUTER JOIN.

SQL FULL OUTER JOIN Keyword

The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right (table2) table records.

양쪽의 모든 테이블이 JOIN되며, 없는것들은 모두 NULL

Note: FULL OUTER JOIN can potentially return very large result-sets!

Tip: FULL OUTER JOIN and FULL JOIN are the same.

SQL Self JOIN

A self JOIN is a regular join, but the table is joined with itself.

스스로 조인하는것으로, 활용하는 법에 대하여 조금 난이도있다고 생각된다.

가장 대표적인 것으로, 어떠한 인적정보 테이블에서 , 자신과 같은 도시에 사는 다른사람 쌍을 구해보는 쿼리를 짠다고 생각하면

select e1.FirstName ,e2.FirstName, e1.city    
from employees e1, employees e2  
where e1.EmployeeId <> e2.EmployeeId and e1.city=e2.City

where e1.EmployeeId <> e2.EmployeeId 의 부분은 자기 스스로와 매핑되지 않도록 하기위함이고,

and e1.city=e2.City 이 부분은 city가 같은 다른사람을 조인하기 위한 부분이다.

 

참조 : SQL Tutorial (w3schools.com)

 

'SQL' 카테고리의 다른 글

SQL기본 select, from, where / insert,update..  (0) 2021.04.01

+ Recent posts