Table Schema
Inspect
Table
| Column | Type |
|---|---|
| booking_id | integer |
| guest_id | integer |
| check_in | date |
| check_out | date |
Find bookings where the stay duration is more than 5 days.
Table Schema
| Column | Type |
|---|---|
| booking_id | integer |
| guest_id | integer |
| check_in | date |
| check_out | date |
Sample Data
| booking_id | guest_id | check_in | check_out |
|---|---|---|---|
| 1 | 101 | 2024-01-01 | 2024-01-03 |
| 2 | 102 | 2024-01-01 | 2024-01-10 |
| 3 | 103 | 2024-01-05 | 2024-01-07 |
| 4 | 104 | 2024-01-02 | 2024-01-09 |
| 5 | 105 | 2024-01-03 | 2024-01-04 |
| booking_id |
|---|
| 2 |
| 4 |
SQL Editor
| booking_id |
|---|
| 2 |
| 4 |
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 booking_id FROM bookings WHERE check_out - check_in > 5;
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