April and May Active Users
An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- easy
- Seniority
- L3
- Asked in
- ActiveCampaign
Problem
Return every user with at least one session in April or May together with each spring month they were active in as a month number, each user and month listed once, lowest user ID first and April before May. Read them from user_sessions, where a spring month counts in every year the log covers, not only the current one.
Spring cleaning for the user base. Who was actually around?
Tables
user_sessions
Columns: session_id INTEGER, user_id INTEGER, device_id INTEGER, session_start TIMESTAMP, session_duration_sec INTEGER, pages_viewed INTEGER
| session_id | user_id | device_id | session_start | session_duration_sec | pages_viewed |
|---|---|---|---|---|---|
| 7061 | 197 | 230 | 2026-02-02 01:00:00 | 53 | 3 |
| 7122 | 294 | 259 | 2026-03-03 02:00:00 | 76 | 6 |
| 7183 | 391 | 288 | 2026-04-04 03:00:00 | 99 | 9 |
| 7244 | 488 | 317 | 2026-05-05 04:00:00 | 122 | 12 |
| 7305 | 585 | 346 | 2026-06-06 05:00:00 | 145 | 15 |
| 7366 | 682 | 375 | 2026-07-07 06:00:00 | 168 | 18 |
| 7427 | 779 | 404 | 2026-08-08 07:00:00 | 191 | 21 |
| 7488 | 876 | NULL | 2026-09-09 08:00:00 | 214 | 24 |
| 7549 | 973 | 462 | 2026-01-10 09:00:00 | 237 | 27 |
| 7610 | 1070 | 491 | 2026-02-11 10:00:00 | 260 | 30 |
Expected output
| user_id | spring_month |
|---|---|
| 100 | 5 |
| 197 | 4 |
| 294 | 5 |
| 391 | 4 |
| 488 | 5 |
Worked solution and explanation
This is a dedupe to the user-and-month grain wearing a growth-team costume. user_sessions holds one row per session, and the same person can come back in the same spring month more than once: one user logs 3 April sessions across 2024 and 2026. Anyone writes the month filter. What separates candidates is seeing that the filter still returns sessions, so each user and month pair repeats once per visit until something collapses it, and every per-month headcount built on the list counts repeat visitors 2 or 3 times.
Read the grain before you filter
Step 1: Pull the month out of session_start
strftime('%m', session_start) turns a value like '2026-04-04 03:00:00' into the 2-character text '04'. Filter on that text directly with IN ('04', '05'), so the comparison is text against text.
Step 2: Match the month in every year
Spring here means April and May in every year the log holds, and user_sessions runs from 2023 into 2026. A BETWEEN on one year's dates keeps only that year and loses the users whose spring visits were all earlier.
Step 3: Collapse sessions to one row per user and month
Put DISTINCT over user_id and the month, and keep the year out of it. A user with April sessions in 2024 and 2026 becomes one April row. Selecting session_id or session_start beside them would make every row unique again and undo the collapse.
Step 4: Turn the month into a number
strftime gives back the text '04'. CAST(strftime('%m', session_start) AS INTEGER) turns it into 4, the month number the report asks for, and the alias spring_month names it once for the sort.
Step 5: Sort by user, then month
ORDER BY user_id, spring_month puts the lowest ID first and April ahead of May within a user. Together the 2 keys identify every row, so there are no ties to settle.
The solution
SELECT DISTINCT
user_id,
CAST(strftime('%m', session_start) AS INTEGER) AS spring_month
FROM user_sessions
WHERE strftime('%m', session_start) IN ('04', '05')
ORDER BY user_id, spring_month- One row per session
SELECT user_idand the month after the filter with noDISTINCT: the user with 3 April sessions shows up 3 times as the same April row.- One row per user and month
SELECT DISTINCT user_id, spring_month: each pair once, however many visits sit behind it.GROUP BY user_id, spring_monthreturns the same set, andDISTINCTsays it with less.
Common follow-up questions
- How many unique users were active each spring, one row per year? (Moves the year into the output grain, so the dedupe runs once per year.)
- Return only the users active in both April and May of the same year. (Turns a membership filter into a per-user condition across 2 months.)
- How would you write the filter so a date-partitioned `user_sessions` can prune partitions? (Tests whether the candidate knows a function on the partition column defeats pruning.)
- What changes if `session_start` is stored in UTC and spring should follow each user's local time? (Probes converting time zones before extracting the month.)