SQL
Lab
Airbnb Airbnb Interview Question 01

Listing
Occupancy
Rate

Calculate occupancy rate = booked days / total available days for each listing.

Table Schema

Inspect
Table

interactive
ColumnType
booking_idinteger
listing_idinteger
check_indate
check_outdate
ColumnType
listing_idinteger
datedate
is_availableboolean

Sample Data

Input
Output

Sample Input: bookings
booking_idlisting_idcheck_incheck_out
11012024-01-012024-01-03
21012024-01-052024-01-07
31022024-01-022024-01-04
Sample Input: calendar
listing_iddateis_available
1012024-01-01false
1012024-01-02false
1012024-01-03true
1012024-01-04true
1012024-01-05false
1022024-01-02false
1022024-01-03false
Expected Output
listing_idoccupancy_rate
1010.60
1021.00

SQL Editor

Run
Query

postgresql
Waiting for query

listing_idoccupancy_rate
1010.60
1021.00

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 listing_id, ROUND(1.0 * SUM(CASE WHEN is_available = false THEN 1 ELSE 0 END) / COUNT(*), 2) AS occupancy_rate
FROM calendar
GROUP BY listing_id;

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