An INNER JOIN returns only the rows where there is a matching value in both tables. Rows from either table that have no corresponding match on the other side are completely excluded from the result. It is the most commonly used join type in MySQL.
SELECT columns
FROM table_a AS a
INNER JOIN table_b AS b ON a.id = b.foreign_key;
customers
| id | name | city |
|---|---|---|
| 1 | Rahim Uddin | Dhaka |
| 2 | Sara Begum | Chittagong |
| 3 | Karim Ali | Sylhet |
| 4 | Nila Akter | Dhaka |
orders
| id | customer_id | amount |
|---|---|---|
| 1 | 1 | 2500.00 |
| 2 | 1 | 1200.00 |
| 3 | 3 | 4800.00 |
| 4 | 6 | 900.00 |
Get the customer name alongside each of their orders:
SELECT c.name, c.city, o.id AS order_id, o.amount
FROM customers AS c
INNER JOIN orders AS o ON c.id = o.customer_id;
| name | city | order_id | amount |
|---|---|---|---|
| Rahim Uddin | Dhaka | 1 | 2500.00 |
| Rahim Uddin | Dhaka | 2 | 1200.00 |
| Karim Ali | Sylhet | 3 | 4800.00 |
Notice what happened:
id=4 (customer_id=6) is not included — no customer with id=6 exists.Add a WHERE clause to filter after the join:
-- Only orders from Dhaka customers
SELECT c.name, o.amount
FROM customers AS c
INNER JOIN orders AS o ON c.id = o.customer_id
WHERE c.city = 'Dhaka';
Combine with aggregate functions for useful reporting. Get the total amount spent by each customer:
SELECT c.name, COUNT(o.id) AS total_orders, SUM(o.amount) AS total_spent
FROM customers AS c
INNER JOIN orders AS o ON c.id = o.customer_id
GROUP BY c.id, c.name
ORDER BY total_spent DESC;
| name | total_orders | total_spent |
|---|---|---|
| Karim Ali | 1 | 4800.00 |
| Rahim Uddin | 2 | 3700.00 |
You can chain multiple INNER JOINs to bring together three or more tables. Simply add one INNER JOIN ... ON ...block for each additional table:
SELECT c.name, o.amount, p.name AS product
FROM customers AS c
INNER JOIN orders AS o ON c.id = o.customer_id
INNER JOIN order_items AS oi ON o.id = oi.order_id
INNER JOIN products AS p ON oi.product_id = p.id;
ON conditions in that chain order to keep the query readable.
INNER JOIN returns only rows that have a match in both tables.INNER JOINs to connect three or more tables.GROUP BY and aggregate functions to produce summary reports from joined data.