pbPassingBI
/
Combining data beginner 5 min

INNER JOIN

Keeping only matching rows, and what silently disappears.

What you'll be able to do
  • Write an INNER JOIN
  • Predict which rows are excluded
  • Recognise when an inner join is losing data

Matching rows only

SELECT o.order_id, c.name
FROM orders o
INNER JOIN customers c ON c.customer_id = o.customer_id;

INNER is the default, so plain JOIN means the same thing. Only rows with a match on both sides appear.

What disappears

Two categories vanish silently:

  • Customers with no orders
  • Orders whose customer_id matches no customer (orphans, often from bad data)

Neither produces a warning. A report that quietly omits customers who have not ordered is a classic inner-join defect.

Checking for loss

Compare counts:

SELECT COUNT(*) FROM orders;                        -- 1000
SELECT COUNT(*) FROM orders o
JOIN customers c ON c.customer_id = o.customer_id;  -- 987

Thirteen orders reference a customer that does not exist. Worth knowing before you publish a revenue figure.

Multiple tables

FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
JOIN regions   r ON r.region_id   = c.region_id

Joins evaluate left to right, each operating on the accumulated result.

Key points
  • INNER JOIN keeps only rows matching on both sides
  • Unmatched rows disappear with no warning
  • Compare row counts to detect silent data loss
Check yourself