Sessionization in SQL: how to define and identify user sessions from inactivity gaps

Quick Overview

Asks how to split a user's event stream into sessions using an inactivity timeout and which SQL window functions do the work. Tests gaps-and-islands reasoning, running totals over ordered partitions, and handling ties and session boundaries.

Sessionization in SQL: how to define and identify user sessions from inactivity gaps

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Describe the sessionization pattern. How would you define and identify user sessions based on inactivity, and what advanced SQL techniques are involved? Assume an `events` table with one row per user action (`user_id`, `event_id`, `event_ts`). ```hint Mark where sessions start Before assigning any ids, think about how to mark the single row where each session begins, using only that row and the one just before it for the same user. ``` ```hint From markers to labels Once every row carries a 0 or 1 start marker, consider what a running aggregate over those markers would give each row. ``` ### What a Strong Answer Covers - A precise session definition: per user, ordered by time, with a new session whenever the gap to the previous event exceeds a timeout. - How to flag session starts with a window function, including each user's first event. - How the flags become a session id, and why that id is unique only together with the user. - Aggregating sessions into start, end, duration and event count. - Practical details such as timestamp ties, choosing the timeout, capping long sessions, and sessions that span processing runs. ### Follow-up Questions - How would you choose the inactivity threshold from the data instead of defaulting to 30 minutes? - Product also wants every session to end after 4 hours regardless of activity. How does the query change? - The table is processed once a day. How do you handle a session that starts before midnight and continues after it? - How would you write the gap check in a dialect without interval arithmetic?

Overview: Asks how to split a user's event stream into sessions using an inactivity timeout and which SQL window functions do the work. Tests gaps-and-islands reasoning, running totals over ordered partitions, and handling ties and session boundaries.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Describe the sessionization pattern. How would you define and identify user sessions based on inactivity, and what advanced SQL techniques are involved?

Assume an events table with one row per user action (user_id, event_id, event_ts).

What a Strong Answer Covers Guidance

  • A precise session definition: per user, ordered by time, with a new session whenever the gap to the previous event exceeds a timeout.
  • How to flag session starts with a window function, including each user's first event.
  • How the flags become a session id, and why that id is unique only together with the user.
  • Aggregating sessions into start, end, duration and event count.
  • Practical details such as timestamp ties, choosing the timeout, capping long sessions, and sessions that span processing runs.

Follow-up Questions Guidance

  • How would you choose the inactivity threshold from the data instead of defaulting to 30 minutes?
  • Product also wants every session to end after 4 hours regardless of activity. How does the query change?
  • The table is processed once a day. How do you handle a session that starts before midnight and continues after it?
  • How would you write the gap check in a dialect without interval arithmetic?