HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

MySQL INNER JOIN

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.

customers
match
orders

Syntax

SELECT columns
FROM table_a AS a
INNER JOIN table_b AS b ON a.id = b.foreign_key;

Sample Tables

customers

idnamecity
1Rahim UddinDhaka
2Sara BegumChittagong
3Karim AliSylhet
4Nila AkterDhaka

orders

idcustomer_idamount
112500.00
211200.00
334800.00
46900.00

Basic INNER JOIN Example

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;
namecityorder_idamount
Rahim UddinDhaka12500.00
Rahim UddinDhaka21200.00
Karim AliSylhet34800.00

Notice what happened:

  • Sara Begum and Nila Akter are not included — they have no orders.
  • Order id=4 (customer_id=6) is not included — no customer with id=6 exists.
  • Rahim Uddin appears twice — once for each of his orders.

INNER JOIN with WHERE

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';

INNER JOIN with ORDER BY and Aggregates

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;
nametotal_orderstotal_spent
Karim Ali14800.00
Rahim Uddin23700.00

Joining Three Tables

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;
Tip: When joining three or more tables, always draw out the relationships first. Identify which table connects to which, and write your ON conditions in that chain order to keep the query readable.

Key Points to Remember

  • INNER JOIN returns only rows that have a match in both tables.
  • Rows with no matching partner on the other side are silently excluded.
  • If one customer has multiple orders, that customer's row appears multiple times in the result — once per order.
  • Use table aliases to keep join queries clean and readable.
  • Chain multiple INNER JOINs to connect three or more tables.
  • Combine with GROUP BY and aggregate functions to produce summary reports from joined data.
What's next? The next lesson covers LEFT JOIN — which returns all rows from the left table even when there is no matching row in the right table.