Table Schema
Inspect
Table
| Column | Type |
|---|---|
| booking_id | integer |
| host_id | integer |
| listing_id | integer |
| total_price | decimal |
Find the top 3 hosts based on total revenue.
Table Schema
| Column | Type |
|---|---|
| booking_id | integer |
| host_id | integer |
| listing_id | integer |
| total_price | decimal |
Sample Data
| booking_id | host_id | listing_id | total_price |
|---|---|---|---|
| 1 | 201 | 101 | 200 |
| 2 | 202 | 102 | 300 |
| 3 | 201 | 103 | 150 |
| 4 | 203 | 104 | 400 |
| 5 | 202 | 105 | 250 |
| 6 | 201 | 101 | 350 |
| 7 | 204 | 106 | 100 |
| host_id | total_revenue |
|---|---|
| 201 | 700 |
| 202 | 550 |
| 203 | 400 |
SQL Editor
| host_id | total_revenue |
|---|---|
| 201 | 700 |
| 202 | 550 |
| 203 | 400 |
Hints
Solution
Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.
SELECT host_id, SUM(total_price) AS total_revenue FROM bookings GROUP BY host_id ORDER BY total_revenue DESC LIMIT 3;
Explanation
Read the expected output columns to determine the final grain.
Aggregate or rank the input rows to calculate the requested metric.
Filter, sort, and alias the final columns to match the output.
Related Questions