First Time Learners Per Day

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L4
Asked in
JPMorgan Chase

Problem

Every row in user_sessions is one session stamped with the moment it began, and most users come back for many sessions spread over many days. Count how many new users each calendar day brought in, where a user is new only on the day of their very first session.

Brand new users, day by day.

Tables

user_sessions

Columns: session_id INTEGER, user_id INTEGER, session_start TEXT, session_duration_sec INTEGER, device_id 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 58 rows
first_session_datenew_user_count
2023-01-012
2023-01-051
2023-01-091
2023-01-251
2023-05-011

Worked solution and explanation

Why this problem exists in real interviews

Counting distinct first events per day is a core growth metric: DAU new-user acquisition, cohort sizing, retention windows. Interviewers use it to test whether you reach for MIN() inside a CTE versus a self-join or window function, and whether you understand why a simple GROUP BY session_start gets it wrong.


Break down the requirements

Step 1: Find each user's earliest session date

MIN(DATE(session_start)) AS first_session_date grouped by user_id. DATE() strips the time component so sessions on the same day collapse.

Step 2: Count first-time users per date

Outer GROUP BY first_session_date with COUNT(*). Each user appears exactly once in the CTE, so COUNT(*) is the new-user count for that day.

Step 3: Order chronologically

ORDER BY first_session_date makes the output a readable growth timeline.


The solution

MIN() inside a CTE then aggregate
WITH first_sessions AS (
  SELECT user_id, MIN(DATE(session_start)) AS first_session_date
  FROM user_sessions
  GROUP BY user_id
)
SELECT first_session_date, COUNT(*) AS new_user_count
FROM first_sessions
GROUP BY first_session_date
ORDER BY first_session_date

Common follow-up questions