Loading lessons...
INNER JOIN
INNER JOIN
INNER JOIN returns rows that have matching values in both tables. Unmatched rows are left out.
Example
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID = Customers.CustomerID;
Result logic
For each order, find the customer with the same CustomerID:
| OrderID | CustomerName |
|---|---|
| 10248 | Alfreds Futterkiste |
| 10249 | Antonio Moreno |
| 10250 | Alfreds Futterkiste |
Customers with no orders do not appear. Orders with no matching customer do not appear either.
Use table prefixes
Write Orders.OrderID instead of just OrderID to be clear which table a column comes from. This matters when both tables have a column with the same name (like CustomerID).
JOIN is shorthand for INNER JOIN
SELECT * FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
JOIN alone means INNER JOIN.
TL;DR
- INNER JOIN returns only matching rows.
- The ON clause defines the match.
- Use table prefixes to avoid ambiguity.
- JOIN alone means INNER JOIN.