Total Usage Hours per Service per Month
Company: Twilio
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
The `twilio_usage` table logs usage of three services: Voice, SMS and Video. Each row is one usage record with its date and the number of hours used. Compute the total usage hours for each service in each calendar month, and return the month as a `YYYY-MM` label.
```hint Month labels as text
Make sure your month label includes the year, and check that sorting the label as text puts months in calendar order.
```
### Input Table(s)
`twilio_usage`
| Column | Type | Meaning |
|---|---|---|
| usage_id | INT | Primary key, one row per usage record |
| service_type | VARCHAR(20) | Service used: `'Voice'`, `'SMS'` or `'Video'` (not null) |
| usage_date | DATE | Date of the usage (not null) |
| usage_hours | DECIMAL(10,2) | Hours used in this record (not null, at least 0) |
Primary key: `usage_id`.
### Output Contract
One row per (month, service) pair that has at least one usage record, with these columns in this order:
| Column | Type | Meaning |
|---|---|---|
| month | TEXT | Year and month of `usage_date`, formatted `YYYY-MM` (for example `2024-03`) |
| service_type | VARCHAR | The service |
| total_usage_hours | DECIMAL | Sum of `usage_hours` for that service in that month, not rounded |
Order by `month` ascending, then `service_type` ascending. Each (month, service_type) pair appears once, so this ordering is unique.
### Example
Input `twilio_usage`:
| usage_id | service_type | usage_date | usage_hours |
|---|---|---|---|
| 1 | Voice | 2024-03-01 | 4.00 |
| 2 | Voice | 2024-03-15 | 6.00 |
| 3 | Voice | 2024-03-28 | 2.50 |
| 4 | SMS | 2024-03-10 | 3.00 |
| 5 | SMS | 2024-03-22 | 5.50 |
Expected output:
| month | service_type | total_usage_hours |
|---|---|---|
| 2024-03 | SMS | 8.50 |
| 2024-03 | Voice | 12.50 |
All rows fall in March 2024. SMS totals 3.0 + 5.5 = 8.5 hours and Voice totals 4.0 + 6.0 + 2.5 = 12.5 hours. Both rows share the same month, so `service_type` decides the order, and `SMS` sorts before `Voice`. Video has no rows, so it does not appear.
### Constraints and Clarifications
- The table has 1 to 100,000 rows.
- `service_type` is always one of `'Voice'`, `'SMS'` or `'Video'`, spelled exactly that way. Ascending order for these values is `SMS`, `Video`, `Voice`.
- No column is NULL, and `usage_hours` is never negative. A record with 0 hours still counts, so a service whose only records in a month have 0 hours appears with a total of 0.00.
- `usage_id` is unique. Several records for the same service on the same day are separate records, and all of them are summed.
- A month is a calendar month of a specific year: March 2024 and March 2025 are different rows.
- Pairs with no usage records are omitted. Do not add zero rows for services that had no usage in a month.
- `total_usage_hours` is not rounded. Because `usage_hours` has two decimal places, the sum is already exact to two decimals.
Overview: Write a SQL query that totals usage hours for each service type in each calendar month and labels the month as YYYY-MM text. It tests grouping on two keys, formatting dates so the labels sort in calendar order, and returning rows in a fully specified order.