MySQL RIGHT OUTER JOIN

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

To demonstrate MySQL RIGHT 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, only for the order with OrderId 200 we don’t have it’s corresponding customer info with CustomerId 4 in the Customers table. And for the other two orders, the corresponding customer info is present in the Customers Table. So for the orders with CustomerId 1 and 2 will have customer details and for the order with CustomerId 4, the Customers table columns will have NULL value in the result.

RESULT:
mysql-right-outer-join

EXAMPLE 2: Below query demonstrates how we can get the Orders with a CustomerId, for which we don’t have a mapping any record in the Customers Table:

RESULT:
mysql-right-outer-join-example-2

Leave a Reply

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