[Data Science] Transact-SQL : Outer Joins, Using Outer Joins
Online Learning 2020. 12. 12. 16:28Outer Joins
- Return all rows from one table and any matching rows from second table
- One table’s rows are “preserved”
- –Designated with LEFT, RIGHT, FULL keyword
- –All rows from preserved table output to result set
- Matches from other table retrieved
- Additional rows added to results for non-matched rows
- –NULLs added in places where attributes do not match
- Example: Return all employees and for those who have taken orders, return the order amount. Employees without matching orders will display NULL for order amount.
DEMO
Customer 테이블은 FirstName, LastName은 모든 행을 포함하고,
SalesOrderHeader 테이블의 SalesOrderNumber는
CustomerID로 매칭되는 경우 표기되고, 매칭되지않는 경우 NULL로 표기된다.
--Get all customers, with sales orders for those who've bought anything
SELECT c.FirstName, c.LastName, oh.SalesOrderNumber
FROM SalesLT.Customer AS c
LEFT OUTER JOIN SalesLT.SalesOrderHeader AS oh
ON c.CustomerID = oh.CustomerID
ORDER BY c.CustomerID;
847개의 결과가 나타남
위의 쿼리에서 WHERE절을 아래처럼 추가하면,
한번도 구매하지 않은 Customer를 표기할 수 있다.
--Return only customers who haven't purchased anything
SELECT c.FirstName, c.LastName, oh.SalesOrderNumber
FROM SalesLT.Customer AS c
LEFT OUTER JOIN SalesLT.SalesOrderHeader AS oh
ON c.CustomerID = oh.CustomerID
WHERE oh.SalesOrderNumber IS NULL
ORDER BY c.CustomerID;
815개의 결과가 나타남
Product 테이블에서 Name과 SalesOrderNumber를 표기한다.
SalesOrderNumber가 없는 경우 NULL로 표기
Product 테이블과 SalesOrderHeader는 매칭가능한 key가 없으므로
Product LEFT JOIN SalesOrderDetail LEFT JOIN SalesOrderHeader순으로 조합한다.
--More than 2 tables
SELECT p.Name As ProductName, oh.SalesOrderNumber
FROM SalesLT.Product AS p
LEFT JOIN SalesLT.SalesOrderDetail AS od
ON p.ProductID = od.ProductID
LEFT JOIN SalesLT.SalesOrderHeader AS oh --Additional tables added to the right must also use a left join
ON od.SalesOrderID = oh.SalesOrderID
ORDER BY p.ProductID;
위의 결과에서 Category를 추가하고 싶다면 Product 테이블과 ProductCategory를 INNER JOIN한다.
두 쿼리 모두 잘 동작하나 INNER JOIN을 앞에다 두는 편이 이해하기 편하다.
SELECT p.Name As ProductName, c.Name AS Category, oh.SalesOrderNumber
FROM SalesLT.Product AS p
LEFT OUTER JOIN SalesLT.SalesOrderDetail AS od
ON p.ProductID = od.ProductID
LEFT OUTER JOIN SalesLT.SalesOrderHeader AS oh
ON od.SalesOrderID = oh.SalesOrderID
INNER JOIN SalesLT.ProductCategory AS c --Added to the left, so can use inner join
ON p.ProductCategoryID = c.ProductCategoryID
ORDER BY p.ProductID;
SELECT p.Name As ProductName, c.Name AS Category, oh.SalesOrderNumber
FROM SalesLT.Product AS p
INNER JOIN SalesLT.ProductCategory AS c --Added to the left, so can use inner join
ON p.ProductCategoryID = c.ProductCategoryID
LEFT OUTER JOIN SalesLT.SalesOrderDetail AS od
ON p.ProductID = od.ProductID
LEFT OUTER JOIN SalesLT.SalesOrderHeader AS oh
ON od.SalesOrderID = oh.SalesOrderID
ORDER BY p.ProductID;
'Online Learning' 카테고리의 다른 글
[Data Science] Transact-SQL : Inner Joins, Using Inner Joins (0) | 2020.12.11 |
---|---|
[Data Science] Transact-SQL : Introduction to Joins (0) | 2020.12.11 |
[영리한 프로그래밍을 위한 알고리즘 강좌] - Recursion의 응용 미로찾기 1 (0) | 2020.12.10 |
[영리한 프로그래밍을 위한 알고리즘 강좌] - 순환(Recursion)의 개념과 기본 예제 3 (0) | 2020.12.10 |
[영리한 프로그래밍을 위한 알고리즘 강좌] - 순환(Recursion)의 개념과 기본 예제 2 (2) | 2020.12.10 |