SQL
Lab
Airbnb Airbnb Interview Question 02

Top
Earning
Hosts

Find the top 3 hosts based on total revenue.

Table Schema

Inspect
Table

interactive
ColumnType
booking_idinteger
host_idinteger
listing_idinteger
total_pricedecimal

Sample Data

Input
Output

Sample Input: bookings
booking_idhost_idlisting_idtotal_price
1201101200
2202102300
3201103150
4203104400
5202105250
6201101350
7204106100
Expected Output
host_idtotal_revenue
201700
202550
203400

SQL Editor

Run
Query

postgresql
Waiting for query

host_idtotal_revenue
201700
202550
203400

Hints

Unlock
Clues

Hint 01: Identify the grouping level required by the output.
Hint 02: Aggregate with COUNT, SUM, AVG, or a window function as needed.
Hint 03: Filter after aggregation with HAVING or after ranking with an outer query.

Solution

Locked
Answer

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

Step By
Step

01

Read the expected output columns to determine the final grain.

02

Aggregate or rank the input rows to calculate the requested metric.

03

Filter, sort, and alias the final columns to match the output.

Related Questions

Keep
Solving