INNER JOIN:
- Returns only the rows that have matching values in both tables.
- If there’s no match between the two tables, that row is excluded from the result.
LEFT JOIN (or LEFT OUTER JOIN):
- Returns all rows from the left table.
- If there’s a match in the right table, it returns the matching row.
- If there’s no match, it still returns the row from the left table, but with
NULLs for columns from the right table.
Example
Given two tables:
Customers
| customer_id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie |
Orders
| order_id | customer_id | product |
|---|---|---|
| 101 | 1 | Laptop |
| 102 | 2 | Smartphone |
INNER JOIN example:
SELECT Customers.name, Orders.product
FROM Customers
INNER JOIN Orders ON Customers.customer_id = Orders.customer_id;
Result:
| name | product |
|---|---|
| Alice | Laptop |
| Bob | Smartphone |
- Only Alice and Bob appear — because they have matching orders.
- Charlie doesn’t have an order, so he’s not shown.
LEFT JOIN example:
SELECT Customers.name, Orders.product
FROM Customers
LEFT JOIN Orders ON Customers.customer_id = Orders.customer_id;
Result:
| name | product |
|---|---|
| Alice | Laptop |
| Bob | Smartphone |
| Charlie | NULL |
- Alice and Bob have matching orders — so we see their orders.
- Charlie has no matching order — but he still shows up, and
productisNULL.
In Short:
| JOIN Type | Rows Returned |
|---|---|
| INNER JOIN | Only where there is a match in both tables. |
| LEFT JOIN | All from left table, matched or not (fill NULLs if no match). |
🎯 Analogy:
- INNER JOIN is like an intersection — show only what both have in common.
- LEFT JOIN is like a left sweep — show everything from the left, and match from the right if you can, otherwise leave
NULL.