Concept · Combining tables

INNER JOIN

INNER JOIN returns the pairs of rows that satisfy the ON condition. Rows with no match are left out.

Before continuing: This entry is part of the JOIN guide. If you are new to the topic, start with JOIN.

INNER JOIN example

Orders 101 and 104 have a row in payments. Order 103 does not appear because it has no match.

Result verified in SQLite
order_idstatusmethod
101shippedcard
104shippedcard
Selected orders that have a payment
SELECT p.id AS order_id,
       p.status,
       pa.method
FROM orders AS p
INNER JOIN payments AS pa
  ON pa.order_id = p.id
WHERE p.id IN (101, 103, 104)
ORDER BY p.id;

Check the result with small data

Liam has no orders; Amy and Eva do.

Self-contained query · SQLite
WITH customers(id, name) AS (
  VALUES (1,'Amy'), (2,'Liam'), (3,'Eva')
), orders(id, customer_id) AS (VALUES (101,1), (102,3))
SELECT c.name, p.id AS order_record
FROM customers c INNER JOIN orders p ON p.customer_id=c.id
ORDER BY p.id;
Query result
nameorder_record
Amy101
Eva102

INNER JOIN keeps only the matches between the two sources. Liam does not appear because he has no related row in orders.

Check your understanding: Which type of JOIN would keep Liam?

Show answer

A LEFT JOIN with customers on the left: FROM customers LEFT JOIN orders. Liam would appear with a NULL order.

How INNER JOIN is used

The word INNER is usually optional: writing JOIN with no qualifier normally means INNER JOIN. The condition must relate columns that represent the same data, not just columns with similar names.

Common mix-ups with INNER JOIN

Joining columns that do not hold the same key

ON orders.id = customers.id compares identifiers of different entities. It can return accidental matches and a result that looks valid.

Check the schema's keys and relationships. Here the correct relationship is orders.customer_id = customers.id.

Repeated rows can be correct

When you join customers to orders, one row per order is usually the right result. Amy, with two orders, appears twice, and Liam, with none, does not appear. If you need a list of customers who have bought something, the unit of the result changes: you can test for existence or summarise the orders before joining. Adding DISTINCT without understanding why rows repeat can hide a key error.

To check a join, temporarily show the keys from both sides and see which pair produced each row. A relationship between identifiers must refer to the same entity: matching an order id with a customer id can produce accidental matches just because both are numbers. The query will run fine, and the error will show up in what its rows mean.

A useful test case includes a row with no match, one with a single match and one with two matches. Also check null and repeated keys where the schema allows them. An equality condition never matches two nulls. If you need to keep the unmatched row, change the question and the join type explicitly; do not expect INNER JOIN to fill that row with empty values.

Technical questions: PostgreSQL: Table Expressions and Joins (opens in a new tab).