Monthly average star rating and rated ride count for each driver
Company: Grab
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
A ride-hailing service stores its drivers in `drivers` and every ride in `rides`. A rider may leave a star rating after a ride, and rides without one have a NULL `star_rating`.
For each driver and each month with at least one rated ride, return the average star rating and the number of rated rides.
### Input Table(s)
`drivers`
| Column | Type | Meaning |
|---|---|---|
| driver_id | INT | Driver id |
| driver_name | VARCHAR(100) | Driver's name, NOT NULL |
Primary key: `driver_id`.
`rides`
| Column | Type | Meaning |
|---|---|---|
| ride_id | INT | Ride id |
| driver_id | INT | Driver who gave the ride, NOT NULL |
| ride_date | DATE | Date of the ride, NOT NULL |
| star_rating | DECIMAL(2,1) | Rider's rating from 1.0 to 5.0, NULL when the ride was not rated |
Primary key: `ride_id`. Foreign key: `rides.driver_id` references `drivers.driver_id`.
### Output Contract
| Column | Type | Meaning |
|---|---|---|
| driver_id | INT | Driver id |
| driver_name | VARCHAR(100) | Driver's name |
| month | TEXT | Calendar month of the rides, formatted `YYYY-MM` |
| avg_rating | NUMERIC | Average `star_rating` of the driver's rated rides in that month, rounded to 2 decimals |
| ride_count | INT | Number of rated rides for the driver in that month |
Return one row per driver and month that has at least one rated ride. Order by `driver_id` ascending, then `month` ascending. The pair (`driver_id`, `month`) is unique in the output, so the order is fully determined.
### Example
`drivers`
| driver_id | driver_name |
|---|---|
| 1 | Ana |
| 2 | Ben |
| 3 | Chen |
`rides`
| ride_id | driver_id | ride_date | star_rating |
|---|---|---|---|
| 1 | 1 | 2024-01-03 | 5.0 |
| 2 | 1 | 2024-01-15 | 4.0 |
| 3 | 1 | 2024-01-20 | NULL |
| 4 | 1 | 2024-02-02 | 4.5 |
| 5 | 2 | 2024-01-10 | 3.0 |
| 6 | 2 | 2024-01-11 | 4.0 |
| 7 | 2 | 2024-01-25 | 4.0 |
| 8 | 2 | 2024-02-14 | NULL |
| 9 | 3 | 2024-03-01 | NULL |
| 10 | 1 | 2023-01-28 | 3.0 |
Expected result
| driver_id | driver_name | month | avg_rating | ride_count |
|---|---|---|---|---|
| 1 | Ana | 2023-01 | 3.00 | 1 |
| 1 | Ana | 2024-01 | 4.50 | 2 |
| 1 | Ana | 2024-02 | 4.50 | 1 |
| 2 | Ben | 2024-01 | 3.67 | 3 |
Ana's January 2024 has two rated rides (5.0 and 4.0, average 4.50). Her unrated ride 3 is left out of both the average and the count, and January 2023 is a separate month from January 2024. Ben's January ratings sum to 11.0 over 3 rides, which is 3.666..., rounded to 3.67. Ben's February and all of Chen's rides are unrated, so they produce no rows.
### Constraints and Clarifications
- 1 to 10,000 drivers and 0 to 100,000 rides.
- `star_rating` is between 1.0 and 5.0 inclusive, or NULL.
- Each `driver_id` in `rides` references a valid driver.
- Exclude rides where `star_rating` is NULL from both `avg_rating` and `ride_count`.
- Months with no rated rides for a driver do not appear, and a driver with no rated rides at all does not appear.
- `month` includes the year, so the same calendar month in different years gives different rows.
- Each rated ride counts once in the average. Round the exact average to 2 decimals with the standard `ROUND`.
```hint Watch the NULLs
Check which aggregate functions skip NULL values on their own and which ones count every row.
```
Overview: Write a SQL query that reports, for each driver and calendar month, the average star rating and the number of rated rides, leaving out rides with no rating. It tests joins, monthly grouping that keeps the year, NULL handling in aggregates, and rounding.