What is the difference between a Left Outer Join and a Right Outer Join in SQL?
The?outer join?returns all rows from one table and only those rows from a secondary table where the joined fields are equal (join condition is met).
The following query displays EVERY record from the Customer table and those records in the Orders table that have a corresponding Customer ID in the Customer table.
SELECT Customer.CustomerID, Orders.PlanID
FROM?Customer?LEFT OUTER JOIN?Orders
ON Customer.CustomerID = Orders.CustomerID;
The?LEFT OUTER JOIN?keywords tell the DBMS to include every row in the table (Customer)?to the left of the LEFT OUTER JOIN?keywords. The?ON?keyword is used to specify the condition (Customer.CustomerID = Orders.CustomerID).
In the results from the query, every Customer ID from the Customer table is retrieved. Even those Customers that have not ordered any items yet.
Keep in mind, that a right outer join and a left outer join is basically the same thing. It just depends on how you set up the query.
For example, both of the following queries are equivalent:
SELECT Customer.CustomerID, Orders.PlanID
FROM?Customer?LEFT OUTER JOIN?Orders
ON Customer.CustomerID = Orders.CustomerID;
SELECT Customer.CustomerID, Orders.PlanID
FROM Orders?RIGHT OUTER JOIN?Customer
ON Customer.CustomerID = Orders.CustomerID;