Revenue reported by this query is exactly double what finance expects, and it started the day the tags table was loaded. Find it.
Given these rows
| id | amount |
|---|---|
| 1 | 100 |
| 2 | 50 |
| order_id | tag |
|---|---|
| 1 | gift |
| 1 | priority |
| 2 | gift |
| 2 | priority |
The query as found
SELECT SUM(o.amount) AS revenue, COUNT(*) AS rows_out FROM orders o JOIN order_tags t ON t.order_id = o.id;
It returns
| revenue | rows_out |
|---|---|
| 300 | 4 |
Why they ask this
Fan-out is the most common cause of an overstated metric in the industry, and the tell — a total that jumped when an unrelated table was joined — is the diagnostic worth recognising on sight.
Say this
The tags table has two rows per order, so the join duplicates every order and the SUM counts each amount twice. Aggregate the tags to one row per order before joining, or move the amount out of the fanned-out join entirely.
The reasoning
The join is correct — it is doing what a join means. What is wrong is that the query then sums a column at *order* grain over rows at *order-tag* grain. One order worth 100 that carries two tags contributes 200.
The fastest confirmation is to compare `COUNT(*)` against `COUNT(DISTINCT order_id)`. If they disagree, the result is not at the grain you think it is, and every additive measure in the query is inflated by exactly that ratio.
The fix below pre-aggregates the tags to one row per order, so the join cannot change the row count. The alternative — `SUM(DISTINCT amount)` — happens to give the right answer here and is a trap: two different orders with the same amount would collapse into one.
The fix verified against SQLite
| revenue | rows_out |
|---|---|
| 150 | 2 |
The answer most people give
"Add SELECT DISTINCT." That deduplicates rows, not contributions, and it silently merges two genuinely different orders that happen to share an amount. You would trade a visible error for an invisible one.
They’ll ask next
You also need the tag list on each row. How do you report revenue and tags in one query without reintroducing the double count?
