What is the difference between a Left Outer Join and a Right Outer Join in SQL?
Learn SQL Today!

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;



#onlinecourses,?#statistics,?#research,?#dataanalytics,?#dataanalysis,?#career,?#sql, #sqlqueries, #sqlserver, #sqltraining, #sqlprogramming

要查看或添加评论,请登录

Dr. Cecelia Allison的更多文章

社区洞察

其他会员也浏览了