Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Compute Session Durations and Gaps

MediumSQL · PostgreSQL00:00
I
Practice interviewer
In session
5 left
00:00

Your question is Compute Session Durations and Gaps. Start with the requirements and the one table on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Adobe Analytics stores user session logs with a start and end timestamp for each session. Write a SQL query to calculate the duration of each session and identify the gap from the previous session for the same user.

Requirements

  1. Return one row per session.
  2. Calculate session_duration_minutes as the difference between session_end and session_start.
  3. Calculate gap_from_previous_minutes as the time between the current session start and the previous session end for the same user.
  4. Add gap_flag with value TRUE when the gap is greater than 30 minutes, otherwise FALSE.
  5. Order the result by user_id, then session_start.

Schema

session_logs
ColumnTypeDescription
session_idPKINTPrimary key for the session record
user_idINTAdobe user identifier
session_startTIMESTAMPSession start time
session_endTIMESTAMPSession end time
device_typeVARCHAR(50)Device used for the session
Tablessession_logs
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results