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

user_sessions: 10 of 200 rows
session_iduser_iddevice_idsession_startsession_duration_secpages_viewed
70611972302026-02-02 01:00:00533
71222942592026-03-03 02:00:00766
71833912882026-04-04 03:00:00999
72444883172026-05-05 04:00:0012212
73055853462026-06-06 05:00:0014515
73666823752026-07-07 06:00:0016818
74277794042026-08-08 07:00:0019121
7488876NULL2026-09-09 08:00:0021424
75499734622026-01-10 09:00:0023727
761010704912026-02-11 10:00:0026030

Expected output

First 5 of 35 rows
user_idspring_month
1005
1974
2945
3914
4885

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

Users and the spring months they showed up
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_id and the month after the filter with no DISTINCT: 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_month returns the same set, and DISTINCT says it with less.

Common follow-up questions