MySQL LEFT OUTER JOIN

MySQL LEFT OUTER JOIN/LEFT JOIN returns all the rows from the LEFT table and the corresponding matching rows from the right table. If right table doesn’t have the matching record then for such records right table column will have NULL value in the result.

To demonstrate MySQL LEFT OUTER JOIN with extensive list of examples and visual representation let us create Customers and Orders tables as shown in the following image by executing the following script

mysql-joins-demo-tables

EXAMPLE 1: As per the data in our demo tables, Customers with CustomerId 1 and 2 in Customers table have the orders in the Orders table. Where as the customer with CustomerId 3 doesn’t have any order in the Orders table. So, the LEFT OUTER JOIN on the CustomerId column between Customers and Orders table will return the Customer and Order details of the Customers with CustomerId 1 and 2 and for CustomerId 3 the Orders Table columns will have NULL value in the result.

RESULT:
mysql-left-outer-join

EXAMPLE 2: Below query demonstrates how to get the name of the Customer who don’t have Orders using LEFT OUTER JOIN.

RESULT:
mysql-left-outer-join-example-2-result

Leave a Reply

Your email address will not be published. Required fields are marked *