Find Premium Users Whose Total Data Usage Exceeds Their Monthly Limit
Company: ServiceNow
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Find users on the `'Premium'` tier whose total usage (the sum of `data_used_mb` over all of their usage log rows) exceeds their `monthly_limit`. Return `user_id`, `username`, `monthly_limit` and `total_usage_mb`, ordered by `total_usage_mb` descending, then `user_id` ascending.
```hint Where the total exists
Usage is spread over many log rows per user, while the limit is one value per user. Work out at which step of the query a per-user total becomes available to compare.
```
### Input Table(s)
Table `Users`
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | Id of the user. Primary key. |
| username | VARCHAR(100) | The user's name. |
Table `Subscriptions`
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | User this subscription belongs to. Primary key, and foreign key to `Users.user_id`. |
| tier | VARCHAR(20) | One of `'Basic'`, `'Standard'`, `'Premium'`. |
| monthly_limit | INT | Usage allowance in MB. Always positive. |
Table `UsageLogs`
| Column | Type | Meaning |
|---|---|---|
| log_id | INT | Id of the log row. Primary key. |
| user_id | INT | User the usage belongs to. Foreign key to `Users.user_id`. |
| data_used_mb | DECIMAL(10,2) | Data used in this log entry, in MB. Never negative. |
### Output Contract
- Columns, in this order: `user_id`, `username`, `monthly_limit`, `total_usage_mb`.
- One row per user whose subscription tier is `'Premium'` and whose total usage is strictly greater than `monthly_limit`.
- `total_usage_mb` is the exact sum of that user's `data_used_mb` values, with two decimals and no further rounding.
- Order by `total_usage_mb` descending, then `user_id` ascending. `user_id` is unique, so the order is fully determined.
### Example
Input `Users`:
| user_id | username |
|---|---|
| 1 | alice |
| 2 | bob |
| 3 | carol |
| 4 | dave |
| 5 | erin |
| 6 | frank |
Input `Subscriptions`:
| user_id | tier | monthly_limit |
|---|---|---|
| 1 | Premium | 1000 |
| 2 | Premium | 500 |
| 3 | Standard | 200 |
| 4 | Premium | 800 |
| 5 | Premium | 300 |
| 6 | Premium | 400 |
Input `UsageLogs`:
| log_id | user_id | data_used_mb |
|---|---|---|
| 101 | 1 | 600.00 |
| 102 | 1 | 450.50 |
| 103 | 2 | 500.00 |
| 104 | 3 | 350.00 |
| 105 | 4 | 1000.00 |
| 106 | 4 | 50.50 |
| 107 | 5 | 320.25 |
Expected output:
| user_id | username | monthly_limit | total_usage_mb |
|---|---|---|---|
| 1 | alice | 1000 | 1050.50 |
| 4 | dave | 800 | 1050.50 |
| 5 | erin | 300 | 320.25 |
Alice and dave both used 1050.50 MB, above their limits, and the tie is broken by `user_id`. Bob used exactly his 500 MB limit, which does not exceed it. Carol is over her limit but on the Standard tier. Frank has no usage rows.
### Constraints and Clarifications
- Each user has exactly one `Subscriptions` row.
- The tier comparison is exact: only `'Premium'` counts.
- Usage rows carry no date. Sum every log row of the user and compare that total with `monthly_limit`.
- Usage equal to the limit does not exceed it.
- A Premium user with no usage rows has a total of 0, which can never exceed a positive limit, so that user does not appear.
- Every `user_id` in `UsageLogs` exists in `Users`.
- Each table has at most 10,000 rows.
Overview: Write a SQL query that joins users, subscriptions and usage logs to find Premium users whose total data usage is above their monthly limit. It tests multi-table joins, GROUP BY with HAVING to compare an aggregate against a per-user column, and ordering with a tiebreaker.