LEFT JOIN
- SQL course
- Intermediate
- Lesson 7/115
- 4 min
Keep all rows from the left table.
LEFT JOIN
LEFT JOIN keeps every row from the left table. Unmatched right columns arrive as NULL.
SELECT c.name, o.id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;This query customers without orders also appear.
c.name, o.id is selected from customers c. orders o is joined on o.customer_id = c.id, keeping every row of the left table.
LEFT JOIN — details
LEFT JOIN follows an 'all left' rule. With no right match, those columns become NULL.
For example, customers LEFT JOIN orders also lists customers who never ordered; their order columns come back as NULL.
Practice LEFT JOIN
Join customers with orders using LEFT JOIN.
Starter code
-- left join
SELECT c.name, o.id
FROM customers cSample solution
SELECT c.name, o.id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;Expected output
name id Ayşe 1 Fatma NULL
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does LEFT JOIN keep entirely?
- The intersection of both
- The left table Correct answer
- The right table
- None
Explanation: LEFT JOIN keeps all rows of the left table.
2 Add LEFT JOIN
SELECT c.name, o.id
FROM customers c
____ orders o ON o.customer_id = c.id;
SELECT c.name, o.id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;
Explanation: LEFT JOIN is the right answer.
3 LEFT JOIN: match the terms
| Term | Meaning |
|---|---|
LEFT JOIN |
all left |
NULL |
no match |
ON |
condition |
RIGHT JOIN |
all right |