Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Users by Session Duration

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

Your question is Top Users by Session Duration. Start with the requirements and the two tables 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

Write a query to identify the top three users by session duration in this schema.

Use the users and sessions tables. Consider only completed sessions, where ended_at is not NULL. Session duration should be measured in seconds.

Output

  1. One row per user, with columns user_id, username, and total_session_duration_seconds.
  2. Include only the three users with the greatest total completed session duration.
  3. Order by total duration descending, using user_id ascending to break ties.

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
usernameVARCHAR(100)User display name
emailVARCHAR(255)User email address
sessions
ColumnTypeDescription
session_idPKINTUnique session identifier
user_idINTUser associated with the session
started_atTIMESTAMPSession start timestamp
ended_atTIMESTAMPSession end timestamp, NULL for ongoing sessions
device_typeVARCHAR(30)Device used during the session
Tablesuserssessions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results