INNER JOIN — Match Records from Both Tables
An INNER JOIN returns only the rows that have matching values in both tables.
If a record in one table does not have a match in the other table, it will not appear in the result.
Note: In SQL, JOIN and INNER JOIN are essentially the same. The JOIN keyword defaults to INNER JOIN if no other join type is specified
Syntax
SELECT table1.column, table2.column
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
Example Scenario
We’ll use the same two tables:
customers
+-------------+--------+-------------+-----------+
| customer_id | name | city | country |
+-------------+--------+-------------+-----------+
| 1 | Alice | New York | USA |
| 2 | Bob | Los Angeles | USA |
| 3 | Emma | London | UK |
| 4 | Paul | Berlin | Germany |
+-------------+--------+-------------+-----------+
orders
+----------+-------------+--------------+--------------+
| order_id | customer_id | product_name | order_status |
+----------+-------------+--------------+--------------+
| 101 | 1 | Laptop | Delivered |
| 102 | 1 | Phone | Shipped |
| 103 | 2 | Charger | Cancelled |
| 104 | 3 | Watch | Delivered |
| 105 | 4 | Headphones | Shipped |
| 106 | 4 | Speakers | Pending |
+----------+-------------+--------------+--------------+
Example Query
SELECT customers.name, orders.product_name, orders.order_status
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id;
This will return only the customers who have placed orders, showing their name, product, and order status.
Output
+--------+--------------+--------------+
| name | product_name | order_status |
+--------+--------------+--------------+
| Alice | Laptop | Delivered |
| Alice | Phone | Shipped |
| Bob | Charger | Cancelled |
| Emma | Watch | Delivered |
| Paul | Headphones | Shipped |
| Paul | Speakers | Pending |
+--------+--------------+--------------+
When to Use INNER JOIN?
- When you want only matching records from both tables.
- Great for most real-world use cases where data is related by keys.
In the next topics, we'll explore how to include non-matching data too — with LEFT JOIN, RIGHT JOIN, and more!