Session Logins Dec 13 to 19

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
Sears

Problem

Which unique users started a session from June 13 through December 19, 2025, the stretch the security team has flagged for suspicious logins in user_sessions?

Logins during one specific window.

Tables

user_sessions

Columns: session_id INTEGER, user_id INTEGER, device_id INTEGER, session_start TEXT, 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 10 rows
user_id
6114
6502
7278
7666
8442

Worked solution and explanation

What this really is

This is a set-membership question dressed up as a security review: who appears at least once inside a box of whole days. Everyone writes the filter and the dedupe. What separates candidates is the last day. session_start is a timestamp, not a date, so BETWEEN '...-06-13' AND '...-12-19' stops at midnight on the 19th and quietly drops almost every session that started that day. The investigation then misses exactly the users whose only activity was on the final day of the stretch.

Step 1: Bound the window to whole days

The lower bound '...-06-13' is midnight, which is exactly where the 13th begins, so it is right as written. The upper bound has to be the end of the 19th, so it becomes '...-12-19 23:59:59'. BETWEEN is inclusive on both sides, which is what makes this pair cover every full day from June 13 to December 19.

Step 2: Collapse sessions to users

user_sessions is one row per session and user_id is heavily skewed, so a busy account appears many times across the stretch. SELECT DISTINCT user_id turns the session log into the list of accounts the team actually reviews.

Accounts active in the flagged stretch
SELECT DISTINCT user_id
FROM user_sessions
WHERE session_start BETWEEN '2025-06-13' AND '2025-12-19 23:59:59'
Bare date upper bound
BETWEEN '...-06-13' AND '...-12-19' reads as inclusive but means up to 00:00:00 on the 19th. The final day is cut to a single second.
End-of-day upper bound
BETWEEN '...-06-13' AND '...-12-19 23:59:59' covers the full 19th, matching what the team asked for.

Common follow-up questions