Sessionize user actions with a 30-minute inactivity gap

Quick Overview

Write a PostgreSQL query that splits each user's action log into sessions using a 30-minute inactivity gap and returns the session number, the previous action time and the gap in minutes. It tests window functions, timestamp arithmetic, strict boundary handling and deterministic tie-breaking on equal timestamps.

Sessionize user actions with a 30-minute inactivity gap

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `user_actions` table logs one row per action a user takes. Group each user's actions into sessions: a new session starts when the gap to the same user's previous action is strictly greater than 30 minutes, and each user's first action starts session 1. For each action, return the action itself, its session number, the time of the user's previous action and the gap in minutes since that action. ### Input Table(s) Table `user_actions` (primary key `action_id`): | Column | Type | Meaning | |---|---|---| | action_id | INT | Id of the action. Primary key. | | user_id | INT | User who took the action. Not null. | | action_time | TIMESTAMP | When the action happened. Not null. | | action_type | VARCHAR(50) | Kind of action, for example `login` or `view`. Not null. | A user can have many rows. ### Output Contract One row per row of `user_actions`, with these columns in this order: | Column | Meaning | |---|---| | user_id | From the input row | | action_id | From the input row | | action_time | From the input row | | action_type | From the input row | | session_id | Session number within the user, starting at 1 and going up by 1 at each new session | | prev_action_time | `action_time` of the same user's previous action, `NULL` for the user's first action | | gap_minutes | Minutes from `prev_action_time` to `action_time`, rounded to 2 decimals, `NULL` for the user's first action | A user's previous action is the row just before it when that user's actions are ordered by `action_time`, then `action_id`. Order the result by `user_id`, `action_time`, `action_id`, all ascending. `action_id` is unique, so the order has no ties. ### Example `user_actions`: | action_id | user_id | action_time | action_type | |---|---|---|---| | 1 | 10 | 2024-03-01 09:00:00 | login | | 2 | 10 | 2024-03-01 09:12:30 | view | | 3 | 10 | 2024-03-01 09:42:30 | click | | 4 | 10 | 2024-03-01 10:12:31 | view | | 5 | 20 | 2024-03-01 09:05:00 | view | | 6 | 20 | 2024-03-01 09:05:00 | click | | 7 | 20 | 2024-03-01 11:00:00 | logout | Expected result: | user_id | action_id | action_time | action_type | session_id | prev_action_time | gap_minutes | |---|---|---|---|---|---|---| | 10 | 1 | 2024-03-01 09:00:00 | login | 1 | NULL | NULL | | 10 | 2 | 2024-03-01 09:12:30 | view | 1 | 2024-03-01 09:00:00 | 12.50 | | 10 | 3 | 2024-03-01 09:42:30 | click | 1 | 2024-03-01 09:12:30 | 30.00 | | 10 | 4 | 2024-03-01 10:12:31 | view | 2 | 2024-03-01 09:42:30 | 30.02 | | 20 | 5 | 2024-03-01 09:05:00 | view | 1 | NULL | NULL | | 20 | 6 | 2024-03-01 09:05:00 | click | 1 | 2024-03-01 09:05:00 | 0.00 | | 20 | 7 | 2024-03-01 11:00:00 | logout | 2 | 2024-03-01 09:05:00 | 115.00 | Action 3 comes exactly 30 minutes after action 2, so it stays in session 1. Action 4 comes 30 minutes and 1 second after action 3, so it starts session 2. Actions 5 and 6 share a timestamp: `action_id` puts 5 first, and action 6 gets a gap of 0.00 in the same session. ### Constraints and Clarifications - `user_actions` has 1 to 10,000 rows. - A gap of exactly 30 minutes stays in the same session. Only a gap strictly greater than 30 minutes starts a new one, judged on the exact gap before rounding. - A user can have several actions with the same `action_time`. Order them by `action_id`; the later one has a gap of 0.00 minutes. - No column of `user_actions` is NULL. The only NULLs in the output are `prev_action_time` and `gap_minutes` on each user's first action. - Session numbers restart at 1 for every user. ```hint Split the work First decide, for each row, whether it opens a new session. Turning those yes/no decisions into session numbers is a separate step. ```

Overview: Write a PostgreSQL query that splits each user's action log into sessions using a 30-minute inactivity gap and returns the session number, the previous action time and the gap in minutes. It tests window functions, timestamp arithmetic, strict boundary handling and deterministic tie-breaking on equal timestamps.

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

The user_actions table logs one row per action a user takes. Group each user's actions into sessions: a new session starts when the gap to the same user's previous action is strictly greater than 30 minutes, and each user's first action starts session 1.

For each action, return the action itself, its session number, the time of the user's previous action and the gap in minutes since that action.

Input Table(s)

Table user_actions (primary key action_id):

ColumnTypeMeaning
action_idINTId of the action. Primary key.
user_idINTUser who took the action. Not null.
action_timeTIMESTAMPWhen the action happened. Not null.
action_typeVARCHAR(50)Kind of action, for example login or view. Not null.

A user can have many rows.

Output Contract

One row per row of user_actions, with these columns in this order:

ColumnMeaning
user_idFrom the input row
action_idFrom the input row
action_timeFrom the input row
action_typeFrom the input row
session_idSession number within the user, starting at 1 and going up by 1 at each new session
prev_action_timeaction_time of the same user's previous action, NULL for the user's first action
gap_minutesMinutes from prev_action_time to action_time, rounded to 2 decimals, NULL for the user's first action

A user's previous action is the row just before it when that user's actions are ordered by action_time, then action_id.

Order the result by user_id, action_time, action_id, all ascending. action_id is unique, so the order has no ties.

Example

user_actions:

action_iduser_idaction_timeaction_type
1102024-03-01 09:00:00login
2102024-03-01 09:12:30view
3102024-03-01 09:42:30click
4102024-03-01 10:12:31view
5202024-03-01 09:05:00view
6202024-03-01 09:05:00click
7202024-03-01 11:00:00logout

Expected result:

user_idaction_idaction_timeaction_typesession_idprev_action_timegap_minutes
1012024-03-01 09:00:00login1NULLNULL
1022024-03-01 09:12:30view12024-03-01 09:00:0012.50
1032024-03-01 09:42:30click12024-03-01 09:12:3030.00
1042024-03-01 10:12:31view22024-03-01 09:42:3030.02
2052024-03-01 09:05:00view1NULLNULL
2062024-03-01 09:05:00click12024-03-01 09:05:000.00
2072024-03-01 11:00:00logout22024-03-01 09:05:00115.00

Action 3 comes exactly 30 minutes after action 2, so it stays in session 1. Action 4 comes 30 minutes and 1 second after action 3, so it starts session 2. Actions 5 and 6 share a timestamp: action_id puts 5 first, and action 6 gets a gap of 0.00 in the same session.

Constraints and Clarifications

  • user_actions has 1 to 10,000 rows.
  • A gap of exactly 30 minutes stays in the same session. Only a gap strictly greater than 30 minutes starts a new one, judged on the exact gap before rounding.
  • A user can have several actions with the same action_time . Order them by action_id ; the later one has a gap of 0.00 minutes.
  • No column of user_actions is NULL. The only NULLs in the output are prev_action_time and gap_minutes on each user's first action.
  • Session numbers restart at 1 for every user.