Platform Speed

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

Problem

The product team is investigating whether device type affects how long users stay engaged. For each device type with at least 35 logged sessions, show the typical session length, the longest single session on record, and how many sessions were logged.

Which devices keep users longest?

Tables

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

devices

Columns: device_id INTEGER, device_type TEXT, os_name TEXT, os_version TEXT, browser TEXT

devices: 10 of 200 rows
device_iddevice_typeos_nameos_versionbrowser
201desktopAndroid15.1Chrome
230tabletWindows16.2Firefox
259smart_tvmacOS17.0Edge
288wearableLinux11Opera
317mobiletvOS12Samsung Internet
346desktopwatchOS13NULL
375tabletiOS10.0.19041Safari
404smart_tvAndroid14.1Chrome
433wearableWindowsNULLFirefox
462mobilemacOS14.0Edge

Expected output

First 4 of 4 rows
device_typeavg_durationmax_durationsession_count
desktop1465.735294117647720036
mobile1334.03125291235
smart_tv1429.1764705882354540037
tablet1369.939393939394360036

Worked solution and explanation

What this really tests

Under the session-analytics costume this is a post-aggregation filter: you keep only the device types whose session count clears a floor, and that count does not exist until the rows are joined, grouped and counted. Anyone can join user_sessions to devices and average the durations. The tell is where the '35 sessions' rule lands. A candidate who reaches for WHERE is filtering individual sessions, not groups, and either errors on the aggregate or silently keeps every device type. Get it wrong and a thinly-sampled platform like wearable, with only 33 sessions, rides into the ranking as if its average were as trustworthy as desktop's.


Break down the requirements

Step 1: Join sessions to devices

JOIN devices ON user_sessions.device_id = devices.device_id attaches device_type to each session so the aggregates can be grouped by platform.

Step 2: Group by device type and aggregate

GROUP BY device_type with AVG(session_duration_sec), MAX(session_duration_sec), and COUNT(*) produces the three per-platform metrics in one pass.

Step 3: Filter with HAVING

HAVING COUNT(*) >= 35 runs after the grouping and drops device types below the sample-size floor. This cannot be a WHERE clause: the count it tests does not exist until the group is formed.

Step 4: Order by average duration descending

ORDER BY avg_duration DESC surfaces the most-engaged platforms first, matching the requested top-to-bottom ranking.


The solution

Having filter for platform speed
SELECT
    d.device_type,
    AVG(s.session_duration_sec) AS avg_duration,
    MAX(s.session_duration_sec) AS max_duration,
    COUNT(*) AS session_count
FROM user_sessions s
JOIN devices d ON s.device_id = d.device_id
GROUP BY d.device_type
HAVING COUNT(*) >= 35

Common follow-up questions