SQL
Lab
Airbnb Airbnb Interview Question 03

Frequent
Guests

Find guests who made more than 2 bookings in a month.

Table Schema

Inspect
Table

interactive
ColumnType
booking_idinteger
guest_idinteger
booking_datedate

Sample Data

Input
Output

Sample Input: bookings
booking_idguest_idbooking_date
11012024-01-01
21012024-01-05
31012024-01-10
41022024-01-03
51022024-02-01
61032024-01-15
Expected Output
guest_id
101

SQL Editor

Run
Query

postgresql
Waiting for query

guest_id
101

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 DISTINCT guest_id
FROM bookings
GROUP BY guest_id, DATE_TRUNC('month', booking_date)
HAVING COUNT(*) > 2;

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