INNER JOIN logic
- SQL course
- Intermediate
- Lesson 1/115
- 4 min
Learn that INNER JOIN returns only matching rows.
INNER JOIN logic
INNER JOIN combines two tables through a shared column. Only rows matching in both tables appear.
SELECT * FROM orders
INNER JOIN customers
ON orders.customer_id = customers.id;This query matches orders with customers.
All columns are selected from orders. customers ON is joined, keeping only matching rows.
INNER JOIN logic — details
INNER JOIN is the most common join. Unmatched rows are ignored.
For example, a customer with no orders or an order whose customer was deleted does not appear; only records with a match in both tables are listed.
Practice INNER JOIN logic
Join orders with customers using INNER JOIN.
Starter code
-- join
SELECT *
FROM ordersSample solution
SELECT *
FROM orders
INNER JOIN customers
ON orders.customer_id = customers.id;Expected output
order_id customer 1 Ayşe 2 Mehmet
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which rows does INNER JOIN keep?
- None
- All rows of the left table
- All rows of the right table
- Only the matching rows Correct answer
Explanation: INNER JOIN keeps only rows matching in both tables.
2 Add the join
SELECT * FROM orders
____ customers ON orders.customer_id = customers.id;
SELECT * FROM orders
INNER JOIN customers ON orders.customer_id = customers.id;
Explanation: INNER JOIN is the right answer.
3 INNER JOIN logic: match the terms
| Term | Meaning |
|---|---|
INNER JOIN |
intersection |
ON |
condition |
LEFT JOIN |
all left |
CROSS JOIN |
cartesian |