Skip to content

Instantly share code, notes, and snippets.

@dnasca
Created January 27, 2016 09:33
Show Gist options
  • Save dnasca/79f9832d9d005a554d56 to your computer and use it in GitHub Desktop.
Save dnasca/79f9832d9d005a554d56 to your computer and use it in GitHub Desktop.
Joining more than two tables
--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