SQLQuokka
Start practicing
QuestionsLearnSandboxFeaturesRoadmapFAQ

Top Inviters by Referred Spend

Unsolved

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.

SQL Query Editor (Ctrl+Enter to run)
0 characters · Valid
Canonical Solution Official

Official Solution is Hidden

Try solving the question with the schema explorer and hints first. When ready, reveal the verified canonical query and step-by-step logic breakdown.

Community Q&A & Discussion

Ask questions, share alternate queries, and explore solutions

0 comments
Asked By:
🏢 Meta

Submission History

Your recent query executions and answer checks

Execution Pipeline:
FROM customers
Full source dataset
12 rows total
Ready. Type a query to see live execution.
Clause: Auto-Sync · AlaSQL Engine