SQL INNER JOIN
INNER JOIN returns only the pairs of rows where the ON condition is true. Rows without a partner on the other side disappear.
| id | name |
|---|---|
| 1 | Ada |
| 2 | Grace |
| 3 | Linus |
| 4 | Hedy |
| id | customer_id |
|---|---|
| 101 | 1 |
| 102 | 2 |
| 103 | 1 |
| 108 | 9 |
SELECT c.name, o.id, o.total
FROM customers c
INNER JOIN orders o ON o.customer_id = c.id;Only pairs where the ON condition matches. Ada appears twice because she has two orders. Linus and Hedy (no orders) and order 108 (unknown customer 9) drop out.
| name | order id | total |
|---|---|---|
| Ada | 101 | 131 |
| Ada | 103 | 9.5 |
| Grace | 102 | 34.5 |
INNER JOIN example, row by row
Only customers with orders, and only orders with a known customer, survive. Linus and Hedy (no orders) and order 108 (unknown customer 9) are dropped.
SELECT c.name, o.id, o.total
FROM customers c
INNER JOIN orders o ON o.customer_id = c.id;▶ Run it on the full sample database
What to know about INNER JOIN
- JOIN on its own means INNER JOIN. The two are interchangeable.
- Table order does not change the result of an INNER JOIN; it only matters for LEFT and RIGHT joins.
- Chain joins to combine more tables: FROM orders o JOIN customers c ON … JOIN order_items i ON … JOIN products p ON ….
- The old comma syntax (FROM a, b WHERE a.id = b.a_id) gives the same result, but forgetting the WHERE silently produces a cross join. Prefer explicit JOIN … ON.
Other join types
- LEFT JOIN — all left rows + matching right rows (null when unmatched)
- RIGHT JOIN — all right rows + matching left rows (null when unmatched)
- FULL OUTER JOIN — every row from both tables
- All SQL joins compared, including anti-joins, CROSS JOIN and self joins
Syntax for everything else, from GROUP BY to window functions, is on the SQL cheat sheet.
INNER JOIN questions
What does INNER JOIN do in SQL?
It combines rows from two tables and keeps only the pairs where the ON condition is true, typically where a foreign key equals a primary key.
Is JOIN the same as INNER JOIN?
Yes. In standard SQL and in MySQL, PostgreSQL, SQL Server, Oracle and SQLite, a bare JOIN is an INNER JOIN.
What is the difference between INNER JOIN and LEFT JOIN?
INNER JOIN drops rows that have no match. LEFT JOIN keeps every row of the left table and fills the missing right-hand columns with NULL.
Is INNER JOIN faster than WHERE?
An INNER JOIN … ON and the equivalent comma join with a WHERE condition are planned the same way by modern optimizers. Choose JOIN … ON for readability and to avoid accidental cross joins.
Can an INNER JOIN return duplicate rows?
Yes. Each matching pair becomes one row, so a customer with two orders appears twice. That is correct behaviour; use GROUP BY or DISTINCT only if you really need one row per customer.