Created
January 27, 2016 09:33
-
-
Save dnasca/79f9832d9d005a554d56 to your computer and use it in GitHub Desktop.
Joining more than two tables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--INNER JOIN multi-table join example | |
SELECT | |
p.Name AS ProductName, | |
pc.Name AS CategoryName, | |
ps.Name AS SubcategoryName | |
FROM | |
Production.Product AS p | |
INNER JOIN | |
Production.ProductSubcategory AS ps | |
ON | |
p.ProductSubcategoryId = ps.ProductSubcategoryId | |
INNER JOIN | |
Production.ProductCategory AS pc | |
ON | |
ps.ProductCategoryId = pc.ProductCategoryId | |
ORDER BY | |
ProductName, | |
CategoryName, | |
SubcategoryName | |
--OUTER JOIN multi-table join example | |
SELECT | |
p.Name, | |
sod.SalesOrderDetailId | |
FROM | |
Production.Product AS p | |
LEFT OUTER JOIN | |
Sales.SAlesOrderDetail AS sod | |
ON | |
p.ProductId = sod.ProductId | |
LEFT OUTER JOIN | |
Sales.SpecialOffer AS so | |
ON | |
sod.SpecialOfferId = so.SpecialOfferId | |
ORDER BY | |
p.Name, sod.SalesOrderDetailId; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment