Average interval in days between consecutive user logins

Quick Overview

Write a SQL query that finds, for each user with at least two logins, the average time in days between consecutive logins. It tests window functions such as LAG, turning timestamp differences into fractional days, excluding single-login users, and rounding only at the end.

Average interval in days between consecutive user logins

Company: Splunk

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

For each user with at least two logins, compute the average interval in days between consecutive logins, rounded to 2 decimal places. Return `user_id` and `avg_interval_days`, ordered by `user_id` ascending. ```hint Units Subtracting two timestamps in PostgreSQL gives an interval, not a number. Decide how you will turn it into fractional days. ``` ### Input Table(s) `user_login` | Column | Type | Meaning | |---|---|---| | id | INT | Login record identifier (primary key, auto-increment) | | user_id | INT | User who logged in, NOT NULL | | login_time | TIMESTAMP | Date and time of the login, NOT NULL | Keys: `id` is the primary key. A user has one row per login. ### Output Contract | Column | Type | Meaning | |---|---|---| | user_id | INT | User with at least two logins | | avg_interval_days | NUMERIC | Mean gap between consecutive logins in fractional days, rounded to 2 decimals | One row per qualifying user, ordered by `user_id` ascending. ### Example `user_login` | id | user_id | login_time | |---|---|---| | 1 | 1 | 2024-01-01 08:00:00 | | 2 | 1 | 2024-01-03 10:00:00 | | 3 | 1 | 2024-01-06 10:00:00 | | 4 | 2 | 2024-01-01 09:00:00 | | 5 | 2 | 2024-01-04 09:00:00 | | 6 | 3 | 2024-01-01 12:00:00 | Expected result: | user_id | avg_interval_days | |---|---| | 1 | 2.54 | | 2 | 3.00 | User 1 has intervals of 2.083 days (Jan 1 08:00 to Jan 3 10:00) and 3.000 days (Jan 3 to Jan 6), which average to 2.54 days. User 2 has one interval of 3 days. User 3 has only one login, so it is excluded. ### Constraints and Clarifications - `user_login` has 1 to 10,000 rows, and `user_id` is a positive integer. - Consecutive means adjacent in `login_time` order for the same user. The order of `id` values is not meaningful. - Intervals are measured in fractional days (elapsed seconds divided by 86,400), not as differences between calendar dates. - `login_time` is not necessarily unique per user. Two logins at the same time are still two logins, and the interval between them is 0. - Round only the final average, not each interval.

Overview: Write a SQL query that finds, for each user with at least two logins, the average time in days between consecutive logins. It tests window functions such as LAG, turning timestamp differences into fractional days, excluding single-login users, and rounding only at the end.

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

For each user with at least two logins, compute the average interval in days between consecutive logins, rounded to 2 decimal places. Return user_id and avg_interval_days, ordered by user_id ascending.

Input Table(s)

user_login

ColumnTypeMeaning
idINTLogin record identifier (primary key, auto-increment)
user_idINTUser who logged in, NOT NULL
login_timeTIMESTAMPDate and time of the login, NOT NULL

Keys: id is the primary key. A user has one row per login.

Output Contract

ColumnTypeMeaning
user_idINTUser with at least two logins
avg_interval_daysNUMERICMean gap between consecutive logins in fractional days, rounded to 2 decimals

One row per qualifying user, ordered by user_id ascending.

Example

user_login

iduser_idlogin_time
112024-01-01 08:00:00
212024-01-03 10:00:00
312024-01-06 10:00:00
422024-01-01 09:00:00
522024-01-04 09:00:00
632024-01-01 12:00:00

Expected result:

user_idavg_interval_days
12.54
23.00

User 1 has intervals of 2.083 days (Jan 1 08:00 to Jan 3 10:00) and 3.000 days (Jan 3 to Jan 6), which average to 2.54 days. User 2 has one interval of 3 days. User 3 has only one login, so it is excluded.

Constraints and Clarifications

  • user_login has 1 to 10,000 rows, and user_id is a positive integer.
  • Consecutive means adjacent in login_time order for the same user. The order of id values is not meaningful.
  • Intervals are measured in fractional days (elapsed seconds divided by 86,400), not as differences between calendar dates.
  • login_time is not necessarily unique per user. Two logins at the same time are still two logins, and the interval between them is 0.
  • Round only the final average, not each interval.