Im between cherry

INNER JOIN과 OUTER JOIN(LEFT JOIN, RIGHT JOIN)의 차이 본문

데이터분석/SQL

INNER JOIN과 OUTER JOIN(LEFT JOIN, RIGHT JOIN)의 차이

meal 2020. 8. 30. 13:59
더보기

 한 줄 요약

- INNER JOIN은 INNER JOIN할 원래 테이블에 찾는 테이블 값이 있으면, 찾는 테이블 값만 출력

- OUTER JOIN은 OUTER JOIN할 원래 테이블에 찾는 테이블 값이 있으면, 원래 테이블+찾는 테이블 값 출력

1. 정의

INNER JOIN - 서로 매칭되는 것만 엮어 조회한다.

OUTER JOIN - 매칭 뿐만 아니라 매칭되지 않은 데이터도 함께 조회한다.

OUTER JOIN에는 Left Outer Join, Right Outer Join, Full Outer Join이 있다.

Left Join, Right Join은 미매칭 데이터도 조회할 테이블 방향이다.

따라서 Left Outer Join의 경우 왼쪽에 기입한 테이블이 매칭여부와 상관없이 모두 조회된다.

 

2. 예시

2-1. INNER JOIN

SELECT 
     select_list
FROM 
     t1
INNER JOIN t2 ON join_condition;

샘플) `products` and `productlines` tables 

SELECT 
    productCode, 
    productName, 
    textDescription
FROM
    products t1
INNER JOIN productlines t2 
    ON t1.productline = t2.productline;

샘플 아웃풋)

https://www.mysqltutorial.org/mysql-inner-join.aspx

 

MySQL INNER JOIN By Practical Examples

In this tutorial, you will learn how to use MySQL INNER JOIN clause to select data from multiple tables based on join conditions.

www.mysqltutorial.org

 

 

2-2. LEFT JOIN

SELECT 
    select_list
FROM
    t1
LEFT JOIN t2 ON 
    join_condition;

샘플) tables `customers` and `orders`

SELECT
    c.customerNumber,
    customerName,
    orderNumber,
    status
FROM
    customers c
LEFT JOIN orders o 
    ON c.customerNumber = o.customerNumber;

샘플아웃풋)

https://www.mysqltutorial.org/mysql-left-join.aspx

 

Understanding MySQL LEFT JOIN Clause By Examples

This tutorial helps you understand MySQL LEFT JOIN concept and how to apply it to query data from two or more database tables.

www.mysqltutorial.org

 

Comments