Full answer
This bug is often called fan-out, and it produces numbers that look plausible.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
CREATE TABLE payments (order_id INTEGER, amount INTEGER);
INSERT INTO payments VALUES (101, 8), (101, 5), (102, 30);Order 101 has two items and was paid in two instalments. Joining both child tables in one go:
SELECT o.id,
SUM(i.qty * i.unit_price) AS items_total,
SUM(p.amount) AS paid
FROM orders o
JOIN order_items i ON i.order_id = o.id
JOIN payments p ON p.order_id = o.id
GROUP BY o.id
ORDER BY o.id;| id | items_total | paid |
|---|---|---|
| 101 | 26 | 26 |
| 102 | 30 | 30 |
The true item total for order 101 is 2 x 4 + 5 x 1 = 13, and the true amount paid is 13. The query reports 26 for both. Each of the 2 item rows matched each of the 2 payment rows, giving 4 rows, and SUM added every value twice. Order 102 has one item and one payment, so it is correct, which is why the bug can hide in small test data.
Count the joined rows to see it:
SELECT o.id, COUNT(*) AS joined_rows
FROM orders o
JOIN order_items i ON i.order_id = o.id
JOIN payments p ON p.order_id = o.id
GROUP BY o.id
ORDER BY o.id;| id | joined_rows |
|---|---|
| 101 | 4 |
| 102 | 1 |
The fix is to reduce each child table to one row per order before joining:
WITH items AS (
SELECT order_id, SUM(qty * unit_price) AS items_total
FROM order_items GROUP BY order_id
), paid AS (
SELECT order_id, SUM(amount) AS paid
FROM payments GROUP BY order_id
)
SELECT o.id, i.items_total, p.paid
FROM orders o
JOIN items i ON i.order_id = o.id
JOIN paid p ON p.order_id = o.id
ORDER BY o.id;| id | items_total | paid |
|---|---|---|
| 101 | 13 | 13 |
| 102 | 30 | 30 |
Rules of thumb: when you join a table to two or more one-to-many children and then aggregate, pre-aggregate the children. SUM(DISTINCT ...) is not a fix, because two different items can have the same value and would be merged.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
CREATE TABLE payments (order_id INTEGER, amount INTEGER);
INSERT INTO payments VALUES (101, 8), (101, 5), (102, 30);
SELECT o.id,
SUM(i.qty * i.unit_price) AS items_total,
SUM(p.amount) AS paid
FROM orders o
JOIN order_items i ON i.order_id = o.id
JOIN payments p ON p.order_id = o.id
GROUP BY o.id
ORDER BY o.id;
SELECT o.id, COUNT(*) AS joined_rows
FROM orders o
JOIN order_items i ON i.order_id = o.id
JOIN payments p ON p.order_id = o.id
GROUP BY o.id
ORDER BY o.id;
WITH items AS (
SELECT order_id, SUM(qty * unit_price) AS items_total
FROM order_items GROUP BY order_id
), paid AS (
SELECT order_id, SUM(amount) AS paid
FROM payments GROUP BY order_id
)
SELECT o.id, i.items_total, p.paid
FROM orders o
JOIN items i ON i.order_id = o.id
JOIN paid p ON p.order_id = o.id
ORDER BY o.id;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Joining two independent child tables multiplies rows
- Sums and counts are inflated by the multiplication
- Pre-aggregate each child to one row per parent, then join
- SUM(DISTINCT) is not a safe fix
Common mistakes
- Testing with orders that have one item and one payment, where the bug cannot appear
- Reaching for DISTINCT to hide the duplication
Follow-ups
- How would you detect fan-out in an existing report?
- Does the same problem happen with COUNT(*)?
Tips
- Show the row count per order; it makes the multiplication visible