Posting Activity Gap
Beginner Mode
Objective
Write a SQL query to calculate the number of days between each user's first and last post in 2021 for users who posted at least twice during that year.
Table Schema:
- posts
| Column | Type | Description |
|---|---|---|
| post_id | INTEGER | Unique identifier for each post |
| user_id | INTEGER | ID of the user who created the post |
| post_date | DATETIME | Date and time when the post was created |
Task Requirements:
- Filter only posts from the year 2021
- Include only users who posted at least twice in 2021
- Calculate the number of days between each user's first and last post
- Return the user ID and the days between their posts
Output columns: user_id, days_between
Examples
Example 1:
Output:
Input:
| posts | ||
|---|---|---|
| post_date | post_id | user_id |
| 2021-01-15 10:00:00 | 1 | 101 |
| 2021-01-20 11:00:00 | 2 | 101 |
| 2021-02-01 12:00:00 | 3 | 102 |
| 2021-03-01 13:00:00 | 4 | 102 |
| 2021-04-15 14:00:00 | 5 | 103 |
| days_between | user_id |
|---|---|
| 5 | 101 |
| 28 | 102 |
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