AdvancedJoins

Why does revenue double after joining orders to items and payments, and how do you fix it?

Scenario

A finance report joins orders to order_items (to count items) and to payments (to check what was paid). Total revenue comes out higher than the orders table says.

Interview answerTry answering first, then reveal

Join fan-out. Joining one order to 3 items and 2 payments produces 3 × 2 = 6 rows for that order, so every order-level amount is summed 6 times. Fix it by aggregating each child table to the order grain (one row per order) before joining, and always state and test the grain of a query.

Explanation

A join returns every matching pair. Joining a parent to two independent child tables multiplies their row counts per parent. This is sometimes called a chasm trap.

COUNT(DISTINCT ...) can rescue counts, but there is no equivalent fix for sums. SUM(DISTINCT amount) is wrong whenever two different rows happen to have the same amount.

The fix is to bring each child to the parent's grain first, in CTEs, then join one-to-one.

Worked example

Run on DuckDB when this page was built; the results shown are the real output.

Sample data
SQL
CREATE TABLE orders (order_id INT, order_total INT);
CREATE TABLE order_items (order_id INT, sku VARCHAR);
CREATE TABLE payments (order_id INT, paid INT);
INSERT INTO orders VALUES (1, 300), (2, 100);
INSERT INTO order_items VALUES (1, 'A'), (1, 'B'), (1, 'C'), (2, 'D');
INSERT INTO payments VALUES (1, 200), (1, 100), (2, 100);

Wrong: joining both children at once

SQL
SELECT SUM(o.order_total) AS revenue,
       COUNT(i.sku)       AS items,
       SUM(p.paid)        AS paid
FROM orders o
JOIN order_items i ON i.order_id = o.order_id
JOIN payments    p ON p.order_id = o.order_id;
Result
revenueitemspaid
190071000

Right: aggregate each child to the order grain first

SQL
WITH items AS (
  SELECT order_id, COUNT(*) AS items FROM order_items GROUP BY order_id
),
paid AS (
  SELECT order_id, SUM(paid) AS paid FROM payments GROUP BY order_id
)
SELECT SUM(o.order_total) AS revenue, SUM(i.items) AS items, SUM(p.paid) AS paid
FROM orders o
LEFT JOIN items i ON i.order_id = o.order_id
LEFT JOIN paid  p ON p.order_id = o.order_id;
Result
revenueitemspaid
4004400

The true figures are 400 revenue, 4 items and 400 paid. The first query reports 1,900, 7 and 1,000.

In production

  • Add a uniqueness test on the declared grain of every model, for example a dbt unique test on order_id for an orders mart.
  • Compare row counts before and after each join in development. A join that should be one-to-one but multiplies rows is the bug.
  • In star schemas, join facts to dimensions (many-to-one), never fact to fact on a shared key. Aggregate facts to a common grain first, a technique called drill-across.

Common mistakes

  • Adding DISTINCT to the final select to "remove duplicates", which hides the fan-out.
  • Using SUM(DISTINCT ...), which also drops legitimate equal amounts.
  • Switching to LEFT JOIN, which fixes missing rows but not multiplication.

More on this topic in the SQL Interview Questions guide, or browse all SQL questions.