A FULL JOIN (also called a FULL OUTER JOIN) combines the results of both left and right outer joins and returns all the records from each table even if there are no matches in the joined table. The joined table will contain all records from both tables, and fill in NULLs for missing matches on either side.
The syntax for a FULL JOIN is as follows:
SELECT table1.column, table2.column
FROM table1
FULL [OUTER] JOIN table2 ON (table1.column=table2.column)
WHERE conditions
Sample Database All the examples on this page use the:
AdventureWorks Lightweight (LT) Sample Database.
Pubs Sample Database or
Northwind Sample Database.
if you don't have an instance of these databases running on your pc, please follow the links to download and install them.
The following example returns the product name and any corresponding sales orders in the SalesOrderDetail table in the AdventureWorks2022 database. It also returns any sales orders that have no product listed in the Product table, and any products with a sales order other than the one listed in the Product table.
-- The OUTER keyword following the FULL keyword is optional.
SELECT p.Name,
sod.SalesOrderID
FROM SalesLT.Product AS p
FULL JOIN SalesLT.SalesOrderDetail AS sod
ON p.ProductID = sod.ProductID
ORDER BY p.Name;
GO
You can also use a FULL outer join to preserve unmatched rows from both sides of the join (all customers, including those who haven't placed an order; and all orders, including those with no matching customer), though in practice this is used less frequently.
///////////////////New Copy Code //////////////////////////** |
Here is the result set:
///////////////////////////Note