A LEFT JOIN
returns all the records from the left table and the matching records from the right table. If there are no matches in the right table, the result will contain NULL values for columns from the right table.
Inner Join
Left Join
Right Outer Join
Full Outer Join
Cross Join
Self Join
Anti Join
returns all the records from the left table and the matching records from the right table. If there are no matches in the right table, the result will contain NULL values for columns from the right table.
Retrieve a list of all customers along with any orders they have placed. If a customer hasn’t placed any orders, still display their name with NULL values for the order details. Additionally, include payments for the orders wherever applicable. This will allow us to see: All customers, even those without orders. All orders placed by customers, with payments (if any) listed. NULL values for orders or payments where no matching records exist.
| CustomerID | CustomerName |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie |
| 4 | David |
| OrderID | CustomerID | Amount |
|---|---|---|
| 101 | 1 | 150 |
| 102 | 2 | 200 |
| 103 | 1 | 350 |
| 104 | 3 | 400 |
| PaymentID | CustomerID | Amount |
|---|---|---|
| 201 | 101 | 150 |
| 202 | 102 | 200 |
| 203 | 103 | 350 |
In this example, we have three related tables: Customers, Orders, and Payments. The relationships between these tables illustrate how data is connected and what happens when some records don't have matching entries. Customers Table (Left Table):
Orders Table (Right Table for First Join):
Payments Table (Right Table for Second Join):
Payments Table (Right Table for Second Join):
SELECT
Customers.CustomerName,
Orders.OrderID,
Orders.Amount AS OrderAmount,
Payments.Amount AS PaymentAmount
FROM
Customers
LEFT JOIN
Orders ON Customers.CustomerID = Orders.CustomerID;
LEFT JOIN
Payments ON Orders.OrderID = Payments.OrderID;
| CustomerName | OrderID | OrderAmount | PaymentAmount |
|---|---|---|---|
| Alice | 101 | 150 | 150 |
| Alice | 103 | 350 | 350 |
| Bob | 102 | 200 | 200 |
| Charlie | 104 | 400 | NULL |
| David | NULL | NULL | NULL |
The query output demonstrates the behavior of a LEFT OUTER JOIN by listing all customers, even those without orders, along with their corresponding order and payment details, if available. For example, Alice appears twice because she placed two orders, each with a matching payment, while Bob and Charlie show up once with their respective orders — although Charlie’s payment is NULL since no payment was made. David, who has not placed any orders, still appears in the result with NULL values for both the order and payment columns. This output highlights how a LEFT JOIN retains all rows from the Customers table (left table), filling in NULL values where there are no matches in the Orders or Payments tables.
A LEFT JOIN is ideal when you want to ensure that all rows from the left table are included in the result, even if there are no matching rows in the right table. This join is especially useful when one table may have incomplete or missing data and you still need to retain all the primary information from the left table. It’s commonly used in scenarios where you want to list all entities (e.g., customers or products) , even if they don’t have related records (such as orders or transactions), ensuring that no data from the primary table is excluded.
A LEFT JOIN ensures that all rows from the left table are included, even if there are no matching records in the right table, with unmatched values appearing as NULL. This makes it especially useful for maintaining complete data from a primary table while enriching it with related information from other tables, where some relationships may be missing.