OneBug This week's bugs
Spot the bug SQL
1
-- Total revenue per customer (one row per customer)
2
SELECT c.customer_id,
3
       c.name,
4
       SUM(oi.line_total) AS revenue
5
FROM customers c
6
JOIN orders o       ON o.customer_id = c.customer_id
7
JOIN order_items oi ON oi.order_id   = o.order_id
8
JOIN payments p     ON p.order_id    = o.order_id
9
GROUP BY c.customer_id, c.name
10
ORDER BY revenue DESC;

Read the full write-up for this bug (spoilers)