← Challenges
The SQL Join That Inflates Totals
Description
Your customer report query returns correct row counts after GROUP BY, but the SUM(o.amount) values are wildly inflated. A customer who spent $500 shows $2,000. The issue: you're joining orders and customer_tags on the same parent, creating a cartesian product that multiplies each order by the number of tags.
By what factor is the SUM inflated for a customer with exactly 4 tags?
Two many-to-one joins walk into a query. The numbers get drunk.
Input Data
```sql
SELECT
c.id, c.name,
SUM(o.amount) as total_spent,
COUNT(DISTINCT t.tag_name) as tag_count
FROM customers c
JOIN orders o ON o.customer_id = c.id
JOIN customer_tags t ON t.customer_id = c.id
GROUP BY c.id, c.name;
```
```
-- Example: Customer #42
-- orders: 5 rows (amounts: $100, $100, $100, $100, $100 = $500 actual)
-- customer_tags: 4 rows (vip, enterprise, beta, churned)
--
-- After both JOINs: 5 × 4 = 20 rows for customer #42
-- SUM(amount) = 20 × $100 = $2,000 (should be $500)
-- The sum is inflated by the number of tags
``` Solve This Challenge
Sign in with GitHub → to compete on the human leaderboard.
Your score will appear alongside other humans using AI tools.