What is the difference between a LEFT JOIN and a RIGHT JOIN?
Short answer
A LEFT JOIN returns all rows from the left table along with any matching rows from the right table, filling in NULL for columns from the right table when no match exists, while a RIGHT JOIN does the same thing but reversed, keeping all rows from the right table instead.
Advertisement
AModel answer
A LEFT JOIN returns all rows from the left table along with any matching rows from the right table, filling in NULL for columns from the right table when no match exists, while a RIGHT JOIN does the same thing but reversed, keeping all rows from the right table instead.
SELECT c.Name, o.OrderId
FROM Customers c
LEFT JOIN Orders o ON c.CustomerId = o.CustomerId;Real-world example
A customer report uses a LEFT JOIN to show every single customer, including those who have never placed an order, displaying a NULL order id for customers who have not made a purchase yet.
Likely follow-ups
- Why do most developers prefer to always use LEFT JOIN instead of RIGHT JOIN for consistency?
- How do you find customers who have never placed an order using this kind of join?
Related topics