Free courses in AI, Data Science & Machine Learning Written lessons · Quizzes · Final exams
Future Data Academy
Log inGet started ↗
FUTURE DATA ACADEMY

Why SQL joins multiply rows: a worked example

By Future Data Academy · 3 min read · Updated

Learn why a SQL join can inflate a total, how to check table grain, and when pre-aggregation fixes the problem.

A join can be technically valid and still answer the wrong question. Before adding DISTINCT, check what one row represents in each input. This worked example uses synthetic data.

Start with the decision

You want total order value by customer. The orders table has one row per order: order 101 belongs to customer A and is worth 100; order 102 belongs to A and is worth 50. The correct total is 150.

A separate order_tags table stores two tags for order 101, “new” and “priority”, and one tag for order 102, “repeat”. Its grain is one row per order-tag combination, not one row per order.

Follow the rows through the join

Joining on order_id produces three rows: 101/new/100, 101/priority/100 and 102/repeat/50. Summing order_value now returns 250. The join did exactly what the relationship required: it matched each order to every matching tag. The mistake was treating the resulting rows as unique orders.

SELECT o.customer_id, SUM(o.order_value) AS total_value
FROM orders o
JOIN order_tags t ON t.order_id = o.order_id
GROUP BY o.customer_id;

That query does not calculate the intended total. If you do not need tags to answer the question, aggregate orders directly. If tags are only an eligibility condition, use EXISTS so each qualifying order contributes once:

SELECT o.customer_id, SUM(o.order_value) AS total_value
FROM orders o
WHERE EXISTS (
  SELECT 1 FROM order_tags t WHERE t.order_id = o.order_id
)
GROUP BY o.customer_id;

This returns 150 for customer A in our example. It excludes untagged orders intentionally; remove the condition if those orders belong in the population.

Why DISTINCT is not a universal repair

SUM(DISTINCT order_value) collapses equal values, not duplicate orders. If two legitimate orders are both worth 100, it counts only one of those values. SELECT DISTINCT across every joined column also retains rows with different tags. Define the entity you need before choosing a deduplication rule.

If you need measures from both tables, aggregate each side to the required shared grain first. For example, create one tag-count row per order, then join that result to the orders table. Check uniqueness of each join key rather than assuming it.

A small quality checklist

  • Record the grain and expected key for each input.
  • Compare row counts and distinct order counts before and after the join.
  • Reconcile the resulting total to an independently calculated baseline.
  • Include an untagged order, a multi-tag order and two equally priced orders in your test.
  • Decide explicitly whether unmatched rows should remain; an inner join removes them, while a left join preserves the left-hand rows.

Try it yourself

Add order 103 for customer B with value 100 and no tag. Compute totals with no tag condition, with EXISTS and with the original inner join. Explain both the duplicated value and the missing customer. A good answer states which population each query describes.

Continue with SQL Essentials, then record your query and reconciliation checks in your lesson notes.

Further reading: PostgreSQL's official join tutorial. The example above is original academy material; the reference explains join behavior, not an endorsement of this course.

CONTINUE EXPLORING

More practical reading

All guides →