SQL interview questionsQuestion 1 of 14
SQL interview question · Question 1 of 14
Explain INNER JOIN vs LEFT JOIN with a practical example.
Short answer
An INNER JOIN returns only rows that have a match in both tables. A LEFT JOIN returns every row from the left table, and fills the right table's columns with NULL where there is no match. So a customer with no orders disappears from an inner join but appears with NULLs in a left join, which changes counts and totals.
On this page
Detailed explanation
Choose the join by asking: which rows must I not lose? If every customer must appear in a report, the customer table is the left side of a LEFT JOIN.
Example
Customers Asha (2 orders), Ben (1 order) and Chen (no orders):
SELECT c.name,
COUNT(o.order_id) AS orders,
COALESCE(SUM(o.amount), 0) AS revenue
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
GROUP BY c.name;
| name | orders | revenue |
|---|---|---|
| Asha | 2 | 120 |
| Ben | 1 | 20 |
| Chen | 0 | 0 |
With INNER JOIN, Chen would be missing entirely.
Common mistakes
COUNT(*)instead ofCOUNT(o.order_id).COUNT(*)counts Chen’s unmatched row as 1 order.- Filtering the right table in
WHERE.WHERE o.amount > 0removes rows whereo.amountisNULL, which drops Chen and silently turns the query into an inner join. Move the condition intoON. - Assuming a left join cannot add rows. If the right side has several matches, the left row is repeated once per match.
Progress is saved in this browser only. No account needed.