JOIN 기능을 이용할 때, ON 절과 comparison operators를 이용하여 어떠한 조건문이라도 추가할 수 있다.
참고자료에서 나온 예시를 활용하자면,
SELECT ~~
FROM companies
LEFT JOIN investments
ON companies. key1 = investments. key2
AND investments.funded_year >= companies.founded_year + 5
위 쿼리는 companies 라는 테이블과 investments 라는 테이블을 연결짓는데
investments에서 funded된 년도가 companies에서 설립된 연도보다 5년 이상 지난 년도인만 행만 골라 left join 하겠다는 의미이다.
이는 그냥 WHERE 절로 조건을 주는 것과와는 다르다.
왜냐하면 그건 LEFT JOIN이 완료된 후에 완성된 테이블에서 필터링 과정을 거치는 것이기 때문이다.
SELECT ~~
FROM companies
LEFT JOIN investments
ON companies. key1 = investments. key2
WHERE investments.funded_year >= companies.founded_year + 5
reference : https://mode.com/sql-tutorial/sql-join-comparison-operators/
SQL Joins with Comparison Operators | Intermediate SQL - Mode
Starting here? This lesson is part of a full-length tutorial in using SQL for Data Analysis. Check out the beginning. In this lesson we'll cover: This lesson uses the same data from previous lessons, which was pulled from Crunchbase on Feb. 5, 2014. Learn
mode.com
'Study > SQL' 카테고리의 다른 글
SQL : Update (0) | 2023.10.05 |
---|---|
SQL : Insert (0) | 2023.10.05 |
SQL : Union & Union ALL (0) | 2023.07.20 |
SQL : Join - LEFT/RIGHT/OUTER JOIN (0) | 2023.07.20 |
SQL : JOIN - INNER JOIN (0) | 2023.07.19 |