Working with Outer Joins
An outer join is used to return all the rows from the specified outer table (specified with LEFT OUTER
, RIGHT OUTER
, or FULL OUTER
), even if the other table has no match. Rows returned from the outer table that have no corresponding match in the inner table display the value NULL
for any columns retrieved from the inner table. For example, you might want to display the IDs of all salespersons along with the their sales territory, if assigned:
SELECT sp.BusinessEntityID as SalesPersonID, st.Name AS TerritoryFROM Sales.SalesPerson spLEFT OUTER JOIN Sales.SalesTerritory stON st.TerritoryID = sp.TerritoryIDgo/* outputSalesPersonID Territory------------- ---------------- 274 NULL ...
Get Microsoft® SQL Server 2012 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.