Revenue Without Double Counting
The customer revenue report started overstating totals when an order has multiple line items. Count only payments with status = 'success', without multiplying them by product lines. Return id, name, and revenue; users with no successful payments get 0, sorted by id.
Buggy query
SELECT u.id, u.name, COALESCE(SUM(p.amount), 0) AS revenue FROM users u LEFT JOIN orders o ON o.user_id = u.id LEFT JOIN order_items oi ON oi.order_id = o.id LEFT JOIN payments p ON p.order_id = o.id AND p.status = 'success' GROUP BY u.id, u.name ORDER BY u.id;
Expected output sample
This is what a correct answer looks like — its row count is its own, it doesn't have to match the schema tables.
| id | name | revenue |
|---|---|---|
| 1 | Alice | 73 |
| 2 | Bob | 0 |
| 3 | Carol | 6 |
Sign in to see submission history
Sign inSign in to use AI Mentor
Sign in