User Sessions on Specific Days

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
Meta

Problem

Pull every recorded field from user_sessions for the sessions that started on a Saturday or a Monday, the two days the growth team is setting side by side in its weekly engagement review.

One user. Specific days. What happened?

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 65 rows
session_iduser_iddevice_idsession_startsession_duration_secpages_viewed
70611972302026-02-02 01:00:00533
71833912882026-04-04 03:00:00999
73055853462026-06-06 05:00:0014515
74277794042026-08-08 07:00:0019121
75499734622026-01-10 09:00:0023727

Worked solution and explanation

Two days that never sit together

This is a calendar-recurrence filter in an engagement-review costume. Saturday and Monday come back every week of a multi-year table and never touch each other, so no date range or BETWEEN on session_start can describe them. You have to read the weekday off every row and match two separate codes. Everyone writes the extraction. What separates candidates is the numbering: STRFTIME('%w', ...) counts 0 for Sunday through 6 for Saturday, and the ISO habit of Monday=1 through Sunday=7 quietly hands you the wrong day. The query still runs and still returns rows, and every wrong row looks plausible.


Break down the requirements

Step 1: Read the weekday off each start time

session_start is a full timestamp, and the weekday is not stored anywhere. STRFTIME('%w', session_start) turns each one into a single-character code, '0' for Sunday up to '6' for Saturday. It returns text, so compare it to text.

Step 2: Match two codes, not a range

Monday is '1' and Saturday is '6'. They are not adjacent, so a BETWEEN '1' AND '6' would sweep in the whole working week. List the two codes with IN ('1', '6') and keep every column with SELECT *, since the review wants the full session record.


The solution

Saturday and Monday sessions
SELECT *
FROM user_sessions
WHERE STRFTIME('%w', session_start) IN ('1', '6')

Common follow-up questions