Messaging Activity Analysis
Beginner Mode
Objective
Write a SQL query to calculate the percentage of time users spend sending messages versus opening messages, grouped by age bucket.
Table Schema:
activities
| Column | Type | Description |
|---|---|---|
| activity_id | INTEGER | Unique identifier for each activity |
| user_id | INTEGER | ID of the user performing the activity |
| activity_type | VARCHAR | Type of activity ('send', 'open') |
| time_spent | DECIMAL | Time spent on the activity in minutes |
| activity_date | DATE | Date when the activity occurred |
age_breakdown
| Column | Type | Description |
|---|---|---|
| user_id | INTEGER | ID of the user |
| age_bucket | VARCHAR | Age group of the user (e.g., '18-25', '26-35', '36-45') |
Task Requirements:
- Join the activities and age_breakdown tables
- Calculate total time spent on 'send' and 'open' activities per age group
- Calculate the percentage of time spent on each activity type
- Round percentages to 2 decimal places
- Display age_bucket, send_perc, and open_perc
Output columns: age_bucket, send_perc, open_perc
Examples
Example 1:
Output:
Input:
| activities | ||||
|---|---|---|---|---|
| activity_date | activity_id | activity_type | time_spent | user_id |
| 2024-01-15 | 1 | send | 10.5 | 101 |
| 2024-01-16 | 2 | open | 5.2 | 101 |
| 2024-01-17 | 3 | send | 8.3 | 102 |
| 2024-01-18 | 4 | open | 12.7 | 102 |
| age_breakdown | |
|---|---|
| age_bucket | user_id |
| 18-25 | 101 |
| 18-25 | 102 |
| age_bucket | open_perc | send_perc |
|---|---|---|
| 18-25 | 48.77 | 51.23 |
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Meta