-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathouter_joins.sql
More file actions
78 lines (29 loc) · 1.3 KB
/
Copy pathouter_joins.sql
File metadata and controls
78 lines (29 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
-- Get all customers, with sales orders for those who have 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;
-- Return only customers who havent purchased anything
SELECT c.firstname, c.lastname, oh.salesordernumber
FROM saleslt.customer AS c
LEFT JOIN saleslt.salesorderheader AS oh
ON c.customerid = oh.customerid
WHERE oh.salesordernumber IS NULL
ORDER BY c.customerid;
-- outer join more than two 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 to the right must also use LEFT JOINS
ON od.salesorderid = oh.salesorderid
SELECT p.name AS productname, c.name AS category, oh.salesordernumber
FROM saleslt.product AS p
LEFT JOIN saleslt.salesorderdetail AS od
ON p.productid = od.productid
LEFT JOIN saleslt.salesorderheader AS oh
ON od.salesorderid = oh.salesorderid
INNER JOIN saleslt.productcategory AS c -- Added to the left (to first/leftmost table 'saleslt.product'), so can use inner join
ON p.productcategoryid = c.productcategoryid
ORDER BY p.productid;