Back to course

LEFT and RIGHT OUTER JOINS

Database Fundamentals: From Zero to Hero

Lesson 28: LEFT and RIGHT OUTER JOINS

Outer Joins are used when you want to see all data from one table, even if there are no matching records in the other table.

1. LEFT OUTER JOIN (or simply LEFT JOIN)

A LEFT JOIN returns all rows from the left table (the table listed first in the query) and the matching rows from the right table.

  • If a row from the left table has no match on the right, the columns from the right table will contain NULL.

Example: Finding Customers Without Orders

We want a list of ALL customers, including those who have never placed an order.

sql SELECT C.CustomerName, O.OrderID FROM Customers C -- Left Table (Keep all rows) LEFT JOIN Orders O -- Right Table (Match if possible) ON C.CustomerID = O.CustomerID WHERE O.OrderID IS NULL; -- Filter where the match failed (i.e., no orders)

2. RIGHT OUTER JOIN (or simply RIGHT JOIN)

A RIGHT JOIN is the mirror image: it returns all rows from the right table and the matching rows from the left table. If no match is found on the left, left table columns are NULL.

Note: Most developers prefer to use LEFT JOIN and swap the table order, as it makes the query easier to read and standardize.