Table Schema
Inspect
Table
| Column | Type |
|---|---|
| driver_id | integer |
| trip_date | date |
Find drivers who completed trips in both January and February.
Table Schema
| Column | Type |
|---|---|
| driver_id | integer |
| trip_date | date |
Sample Data
| driver_id | trip_date |
|---|---|
| 201 | 2024-01-01 |
| 201 | 2024-02-01 |
| 202 | 2024-01-05 |
| 203 | 2024-02-10 |
| 204 | 2024-01-15 |
| 204 | 2024-02-20 |
| driver_id |
|---|
| 201 |
| 204 |
SQL Editor
| driver_id |
|---|
| 201 |
| 204 |
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 driver_id
FROM trips
WHERE trip_date >= DATE '2024-01-01'
AND trip_date < DATE '2024-03-01'
GROUP BY driver_id
HAVING COUNT(DISTINCT DATE_TRUNC('month', trip_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