Table Schema
Inspect
Table
| Column | Type |
|---|---|
| booking_id | integer |
| guest_id | integer |
| booking_date | date |
Find guests who made more than 2 bookings in a month.
Table Schema
| Column | Type |
|---|---|
| booking_id | integer |
| guest_id | integer |
| booking_date | date |
Sample Data
| booking_id | guest_id | booking_date |
|---|---|---|
| 1 | 101 | 2024-01-01 |
| 2 | 101 | 2024-01-05 |
| 3 | 101 | 2024-01-10 |
| 4 | 102 | 2024-01-03 |
| 5 | 102 | 2024-02-01 |
| 6 | 103 | 2024-01-15 |
| guest_id |
|---|
| 101 |
SQL Editor
| guest_id |
|---|
| 101 |
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 DISTINCT guest_id
FROM bookings
GROUP BY guest_id, DATE_TRUNC('month', booking_date)
HAVING COUNT(*) > 2;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