Table Schema
Inspect
Table
| Column | Type |
|---|---|
| guest_id | integer |
| booking_date | date |
Find guests who made bookings in both January and February.
Table Schema
| Column | Type |
|---|---|
| guest_id | integer |
| booking_date | date |
Sample Data
| guest_id | booking_date |
|---|---|
| 101 | 2024-01-01 |
| 101 | 2024-02-01 |
| 102 | 2024-01-05 |
| 103 | 2024-02-10 |
| 104 | 2024-01-15 |
| 104 | 2024-02-20 |
| guest_id |
|---|
| 101 |
| 104 |
SQL Editor
| guest_id |
|---|
| 101 |
| 104 |
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 guest_id
FROM bookings
WHERE booking_date >= DATE '2024-01-01'
AND booking_date < DATE '2024-03-01'
GROUP BY guest_id
HAVING COUNT(DISTINCT DATE_TRUNC('month', booking_date)) = 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