Code Quiz

Inflated Revenue From Join Fan-Out

A GROUP BY revenue query joins an extra table and accidentally multiplies the summed totals.

Codesql
SELECT c.name, SUM(o.total) AS revenue
FROM customers c
JOIN orders o     ON o.customer_id = c.id
JOIN order_items oi ON oi.order_id = o.id
GROUP BY c.name;

The revenue per customer comes out far too high. What is the bug in this query?