Top Users by Session Time

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L3
Asked in
Lyft

Problem

Return the top 10 users by total session duration. Show each user's ID, username, and total duration, from highest to lowest.

They spent the most time here.

Tables

users

Columns: user_id INTEGER, username TEXT, email TEXT, signup_date TEXT, account_status TEXT, age_bucket TEXT

users: 10 of 200 rows
user_idusernameemailsignup_dateaccount_statusage_bucket
100alicealice@example.com2025-02-02inactive25-34
197aaron42aaron42@example.com2026-03-03suspended35-44
294 ameliaamelia@example.com2024-04-04pending_verification45-54
391arjun arjun@example.com2025-05-05active55-64
488ava99ava99@example.com2026-06-06inactive65+
585andrew_kandrew_k@example.com2024-07-07suspendedNULL
682anikaanika@example.com2025-08-08 16:34:00pending_verification18-24
779aidenaiden@example.com2026-09-09 14:53:00active25-34
876aria_baria_b@example.com2024-10-10inactive35-44
973brianbrian@example.com2025-11-11suspended45-54

user_sessions

Columns: session_id INTEGER, user_id INTEGER, device_id INTEGER, session_start TEXT, session_duration_sec INTEGER, pages_viewed INTEGER

user_sessions: 10 of 200 rows
session_iduser_iddevice_idsession_startsession_duration_secpages_viewed
70611972302026-02-02 01:00:00533
71222942592026-03-03 02:00:00766
71833912882026-04-04 03:00:00999
72444883172026-05-05 04:00:0012212
73055853462026-06-06 05:00:0014515
73666823752026-07-07 06:00:0016818
74277794042026-08-08 07:00:0019121
7488876NULL2026-09-09 08:00:0021424
75499734622026-01-10 09:00:0023727
761010704912026-02-11 10:00:0026030

Expected output

First 5 of 10 rows
user_idusernametotal_duration
100alice21840
1943cyrus6935
1846cameron6820
1749chloe6705
1555brooke76475

Worked solution and explanation

What this really is

Strip the engagement framing and this is a top ten over a per-user total, not over rows. user_sessions holds one row per session, so every user fans out across several rows, and the ranking only means something after those rows collapse into one SUM(session_duration_sec) per user. Anyone can write the join. What separates candidates is whether the sort and the cut run on the summed totals or on the raw joined rows. Get it wrong and you publish the ten longest single sessions: 'alice' fills three slots with her 7200, 5400 and 3600 second sessions, and 'cyrus', the real runner-up at 6935 seconds, never appears.

Build it in order

Step 1: Join users to user_sessions on user_id

The username lives on users and the time lives on user_sessions. Keep only matched users: someone with no sessions has no total to compete with, so padding them in as zeros adds rows that can never reach the top ten.

Step 2: Collapse to one row per user

Group on u.user_id and u.username and emit SUM(s.session_duration_sec) AS total_duration. The id is the identity; username rides along only so it can be shown. SUM skips sessions whose duration was never recorded, which is what a total of recorded time should do.

Step 3: Sort the totals, then cut

ORDER BY total_duration DESC sorts the summed values and LIMIT 10 cuts after that sort, so each user can appear at most once. On this data the tenth and eleventh totals differ (5900 against 5853), so the cut lands cleanly without a tie rule.

The solution

Join, total per user, then take the top ten
SELECT u.user_id, u.username, SUM(s.session_duration_sec) AS total_duration
FROM users u
JOIN user_sessions s ON u.user_id = s.user_id
GROUP BY u.user_id, u.username
ORDER BY total_duration DESC
LIMIT 10
The version that sorts sessions, not users
SELECT u.user_id, u.username, s.session_duration_sec AS total_duration
FROM users u
JOIN user_sessions s ON u.user_id = s.user_id
ORDER BY s.session_duration_sec DESC
LIMIT 10

Ranks single session_duration_sec values: 'alice' takes three of the ten slots and the true volume leaders vanish.


Common follow-up questions