SQL INNER JOIN
INNER JOIN returns only the rows that have a match in both tables.
Example
SQL
SELECT c.name, o.total FROM customers c INNER JOIN orders o ON o.customer_id = c.id;
What gets dropped
| Row | Result |
|---|---|
| Customer with no orders | Not in the result. |
| Order whose customer was deleted | Not in the result. |
| Customer with 3 orders | Appears 3 times — one per order. |
Implicit comma join
You may see this older style — equivalent to INNER JOIN but harder to spot the join condition:
SQL
-- Old, avoid in new code SELECT c.name, o.total FROM customers c, orders o WHERE o.customer_id = c.id;
Modern style separates filtering (WHERE) from linking (ON) so reviewers can read each independently.
JOIN = INNER JOIN
The word INNER is optional. JOIN alone means INNER JOIN in every major database.
Tip: Use
INNER JOIN when "no match" should mean "drop the row". If you need customers with or without orders, switch to LEFT JOIN.Example
Example
SELECT c.name, o.total FROM customers c INNER JOIN orders o ON o.customer_id = c.id;Try it Yourself »
Exercise
Returns only matching rows from both tables.
JOIN orders ON ...
Five letters; matches-only join type.
Discussion
Loading…