Top Inviter Spend
Start Timer
0:00:00
We’re given a customers table, where a customer may have been invited by another customer, and an orders table with the amount of each purchase. Among invited customers who have placed at least one order, write a query that finds the five highest-spending customers and returns each one’s inviter along with that customer’s total spend.
Note: A customer’s inviter is the customer whose id matches their inviter_id.
Rules
- If fewer than five such customers exist, return them all.
- Break ties in total spend by customer id ascending.
- Order the results by total_spend descending, then by customer id ascending.
Example:
Input:
customers table
| Column | Type |
|---|---|
id |
INTEGER |
name |
VARCHAR |
inviter_id |
INTEGER |
orders table
| Column | Type |
|---|---|
id |
INTEGER |
customer_id |
INTEGER |
amount |
FLOAT |
created_at |
DATETIME |
Output:
| Column | Type |
|---|---|
customer_name |
VARCHAR |
inviter_name |
VARCHAR |
total_spend |
FLOAT |
.
.
.
.
.
.
.
.
.
Comments