A RIGHT JOIN (also referred to as a RIGHT OUTER JOIN) returns all rows from the right table with corresponding rows from the left table. If there's no matching row, NULLs are returned as values from the left table.
The syntax for a RIGHT JOIN is as follows:
SELECT table1.column, table2.column
FROM table1
RIGHT [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.
Consider a join of the SalesOrderHeader table and the Customer table on their CustomerID columns. The results show only the Companies who have orders pending, their Freight, CustomerID and TotalDue.
SELECT oh.CustomerID, c.CompanyName, oh.Freight, oh.TotalDue
FROM SalesLT.SalesOrderHeader AS oh
JOIN SalesLT.Customer AS c
ON c.CustomerID = oh.CustomerID;
To return all records from the SalesOrderHeader table and only matching data from the Customer table (in other words, all orders including those for which there was no matching customer record), we will use a RIGHT [OUTER] join. Here is the modified query of the Transact-SQL right outer join:
SELECT oh.CustomerID, c.CompanyName, oh.Freight, oh.TotalDue
FROM SalesLT.SalesOrderHeader AS oh
RIGHT OUTER JOIN SalesLT.Customer AS c
ON c.CustomerID = oh.CustomerID;
Below is another right outer join example: This query retrieves a list of all customers and any orders they have placed, including customers who have registered but never placed an order.
///////////////////New Copy Code //////////////////////////.
GO
Keep in Mind:
Note