How would you write a SQL query to find the top 5 customers by revenue in each region?
Assume revenue includes only orders with a completed status. Break ties by customer_id ascending, and include regions with fewer than five qualifying customers.
Output
- One row per qualifying customer, with
region_name, customer_id, customer_name, total_revenue, and revenue_rank.
- Include only customers with completed orders and a rank from 1 through 5 within their region.
- Order by
region_name ascending, revenue_rank ascending, then customer_id ascending.