INNER JOIN: Only Matching Rows
An INNER JOIN returns only the rows that have a match in both tables.
If a row in one table has no match in the other table, it is not in the result.
Note: JOIN and INNER JOIN mean the same thing. When you write just JOIN, PostgreSQL uses an inner join.
Syntax
SELECT table1.column, table2.column
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
Example Query
We use the same customers and orders tables:
SELECT customers.name, orders.product_name,
orders.order_status
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id
ORDER BY orders.order_id;
Output:
name | product_name | order_status
-------+--------------+--------------
Alice | Laptop | Delivered
Alice | Phone | Shipped
Bob | Charger | Cancelled
Emma | Watch | Delivered
Paul | Headphones | Shipped
Paul | Speakers | Pending
(6 rows)
This returns only the customers who have placed orders, with their product and order status.
Two rows are missing on purpose:
- Olivia has no orders, so she has no match in
orders. - Order 107 (Tablet) has no customer (
customer_idisNULL), so it has no match incustomers.
When to Use INNER JOIN?
- When you want only the rows that match in both tables.
- It is the most common join for related data.
In the next lessons, you will see how to keep the rows that don't match too, with LEFT JOIN, RIGHT JOIN and FULL JOIN.
In the exercise, use INNER JOIN to list the shipped orders.