Full answer
sql
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);
INSERT INTO order_items VALUES (102, 'Pen', 30, 1);The query as written:
sql
SELECT o.customer_id, SUM(DISTINCT oi.qty * oi.unit_price) AS total FROM orders o JOIN order_items oi ON oi.order_id = o.id WHERE o.customer_id = 1 GROUP BY o.customer_id;| customer_id | total |
|---|---|
| 1 | 43 |
Ana's lines are worth 8, 5, 30 and, after the added line of 30 pens, another 30. DISTINCT drops the second 30, so the total is 43 instead of 73.
A corrected version:
sql
SELECT o.customer_id, SUM(t.order_total) AS total FROM orders o
JOIN (SELECT order_id, SUM(qty * unit_price) AS order_total FROM order_items GROUP BY order_id) t ON t.order_id = o.id
WHERE o.customer_id = 1 GROUP BY o.customer_id;| customer_id | total |
|---|---|
| 1 | 73 |
Summing lines per order first and then per customer counts every line once, giving 73. When the original double counting came from joining a second one-to-many table (such as payments), pre-aggregate each child table separately and join the summaries. SUM(DISTINCT) is almost never correct for money.
sql
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);
INSERT INTO order_items VALUES (102, 'Pen', 30, 1);
SELECT o.customer_id, SUM(DISTINCT oi.qty * oi.unit_price) AS total FROM orders o JOIN order_items oi ON oi.order_id = o.id WHERE o.customer_id = 1 GROUP BY o.customer_id;
SELECT o.customer_id, SUM(t.order_total) AS total FROM orders o
JOIN (SELECT order_id, SUM(qty * unit_price) AS order_total FROM order_items GROUP BY order_id) t ON t.order_id = o.id
WHERE o.customer_id = 1 GROUP BY o.customer_id;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- SUM(DISTINCT) removes equal values
- Equal amounts are often legitimate
- Pre-aggregate to avoid fan-out
- Join summaries, not raw child rows
Common mistakes
- Using DISTINCT to hide join duplication
- Testing only with data where all values differ
Follow-ups
- How would you sum order items and payments per customer in one query?
- When is SUM(DISTINCT) correct?
Tips
- Separate duplicate rows from equal values