A bookstore runs an invite program, and customers.invited_by holds the customer_id of whoever invited that customer (NULL if nobody did). For each inviter, compute the average payment per book across all of their invitees combined: the total amount the invitees paid divided by the total number of books they bought. An inviter's own purchases do not count, and inviters whose invitees never bought anything are left out. Write an SQL query that returns the top 3 inviters as inviter_id, inviter_name and avg_payment_per_book rounded to 2 decimal places. Order by avg_payment_per_book descending, then inviter_id ascending.
Loading…
Try solving the question with the schema explorer and hints first. When ready, reveal the verified canonical query and step-by-step logic breakdown.
Ask questions, share alternate queries, and explore solutions
Your recent query executions and answer checks
This will insert the verified MySQL solution into your editor so you can study how each clause transforms the table.