SQL#sql#analytics
Top customers by revenue
Aggregate orders per customer and rank the top spenders.
1SELECT c.id,
2 c.name,
3 SUM(o.total) AS revenue,
4 COUNT(o.id) AS orders
5FROM customers c
6JOIN orders o ON o.customer_id = c.id
7WHERE o.status = 'paid'
8GROUP BY c.id, c.name
9ORDER BY revenue DESC
10LIMIT 10;
11
README.md
Top customers
Returns the ten highest-spending customers, counting only paid orders.
Add a date filter on o.created_at to scope it to a period.