Report each user's activity count and engagement level for login, purchase and page_view
Company: Palo Alto Networks
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
For every user and each of the three activity types (`login`, `purchase`, `page_view`), return how many activities of that type the user has and an engagement level based on that count. Every user gets all three rows. A type the user never performed has a count of 0.
The engagement level depends on the count for that user and type:
- `High` for 10 or more
- `Medium` for 5 to 9
- `Low` for 1 to 4
- `Inactive` for 0
```hint Rows that must exist
Some of the rows you need have no matching log at all. Think about what the query should start from so that every user and type pair is present before any counting happens.
```
### Input Table(s)
`users`
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | Primary key |
| username | VARCHAR(100) | Display name, never NULL |
| signup_date | DATE | Sign-up date, never NULL |
`activity_logs`
| Column | Type | Meaning |
|---|---|---|
| activity_id | INT | Primary key |
| user_id | INT | Foreign key to `users.user_id` |
| activity_type | VARCHAR(50) | One of `'login'`, `'purchase'`, `'page_view'`, never NULL |
| activity_date | DATETIME (TIMESTAMP in PostgreSQL) | When the activity happened, never NULL |
### Output Contract
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | The user |
| username | VARCHAR | The user's name from `users` |
| activity_type | TEXT | `'login'`, `'purchase'` or `'page_view'` |
| activity_count | INTEGER | Number of `activity_logs` rows for this user and type |
| engagement_level | TEXT | `'High'`, `'Medium'`, `'Low'` or `'Inactive'` |
- Exactly three rows per user, so the result has 3 times as many rows as `users`.
- Order by `user_id` ascending, then by activity type in the fixed order `login`, `purchase`, `page_view` (not alphabetical). The pair (`user_id`, `activity_type`) is unique, so this order is unique.
- Counts are whole numbers; no rounding is involved.
### Example
`users`
| user_id | username | signup_date |
|---|---|---|
| 1 | alice | 2023-01-10 |
| 2 | bob | 2023-02-14 |
| 3 | carol | 2023-03-01 |
`activity_logs`
| activity_id | user_id | activity_type | activity_date |
|---|---|---|---|
| 101 | 1 | login | 2023-04-01 08:00:00 |
| 102 | 1 | login | 2023-04-02 09:15:00 |
| 103 | 1 | purchase | 2023-04-02 09:20:00 |
| 104 | 2 | page_view | 2023-04-03 10:00:00 |
| 105 | 2 | page_view | 2023-04-03 10:01:00 |
| 106 | 2 | page_view | 2023-04-03 10:02:00 |
| 107 | 2 | page_view | 2023-04-03 10:03:00 |
| 108 | 2 | page_view | 2023-04-03 10:04:00 |
| 109 | 2 | login | 2023-04-03 09:59:00 |
Expected result:
| user_id | username | activity_type | activity_count | engagement_level |
|---|---|---|---|---|
| 1 | alice | login | 2 | Low |
| 1 | alice | purchase | 1 | Low |
| 1 | alice | page_view | 0 | Inactive |
| 2 | bob | login | 1 | Low |
| 2 | bob | purchase | 0 | Inactive |
| 2 | bob | page_view | 5 | Medium |
| 3 | carol | login | 0 | Inactive |
| 3 | carol | purchase | 0 | Inactive |
| 3 | carol | page_view | 0 | Inactive |
Bob's five page views sit exactly on the lower bound of `Medium`. Carol has no logs at all and still gets three rows, each with count 0 and level `Inactive`.
### Constraints and Clarifications
- `users` has at most 100 rows and `activity_logs` at most 10,000 rows.
- `activity_type` is always one of `'login'`, `'purchase'`, `'page_view'`.
- `activity_id` is unique, so every log row counts once. All dates are valid and in the past, and `activity_date` does not filter anything: count the user's full history.
- The engagement level applies to each type's count on its own, not to the user's total across types.
- A log row with a NULL `user_id` belongs to no user and is not counted.
- Two users may share a `username`; `user_id` tells them apart.
Overview: Write a SQL report that gives every user one row per activity type (login, purchase, page_view) with an activity count and an engagement level from High to Inactive. It tests keeping zero-count rows through outer joins, mapping counts to labels with CASE, and sorting by a custom category order.