P05SQLIntermediateInterpret12 min
Top 5 Customers by Revenue
Aggregate completed orders, then rank customers by revenue without counting cancelled orders.
#group by#aggregation#order by
Scenario
Sales leadership needs the top five customers by completed-order revenue for the current year.
Problem statement
Write a query that returns customer name and total completed revenue, sorted highest first, limited to five rows.
Your Task
- Filter to completed orders before aggregating.
- Group at the customer level.
- Use an alias that makes the result self-explanatory.
Tables
customers(customer_id, customer_name)
orders(order_id, customer_id, order_date, amount, status)
Related Concepts
SQL GROUP BYRevenue metricsData grain
Ready to count this problem?
Mark it complete when you have reasoned through the solution in your own words.