SQL LEFT JOIN

LEFT JOIN (also called LEFT OUTER JOIN) returns all rows from the left table and the matching rows from the right table. If there is no match, the result will still include the left table row, but the right table columns will contain NULL.

Syntax

SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;

table1 → Left table (all rows kept)
table2 → Right table (only matching rows included)

Example

SELECT Customers.name, Orders.order_id
FROM Customers
LEFT JOIN Orders
ON Customers.customer_id = Orders.customer_id;

Visual Concept

LEFT TABLE        RIGHT TABLE
Customers         Orders

LEFT JOIN RESULT:
All Customers + matching Orders

Example: Find rows without matches

SELECT Customers.name
FROM Customers
LEFT JOIN Orders
ON Customers.customer_id = Orders.customer_id
WHERE Orders.order_id IS NULL;

This returns customers who never placed an order.


When to Use LEFT JOIN

Use it when you want:

All records from the main table
Related data if it exists
Missing matches shown as NULL

Example use cases:
All employees with their department (even if some have none)
All customers with their orders
All students with their grades


Topics