Platform Check

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

Domain
SQL
Difficulty
easy
Seniority
L3

Problem

Which device type and operating system did each session in user_sessions that lasted more than a thousand seconds run on, going by the devices table? Show each such session's user and its duration alongside them.

When a session drags on, the platform it ran on is the first suspect.

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 5 of 102 rows
user_iddevice_typeos_namesession_duration_sec
391wearableWindows1019
488mobilemacOS1042
585desktopLinux1065
682tablettvOS1088
779smart_tvwatchOS1111

Worked solution and explanation

What this really is

Strip off the profiling costume and this is a fact-to-dimension lookup. The filter and the reported numbers live on user_sessions, but the two attributes the question asks for, device_type and os_name, exist only on devices. No computation over the session table can conjure them, so the two tables have to meet on device_id. Everyone writes that join. What separates candidates is what they do about sessions with no device: 23 of the 200 sessions carry a NULL device_id, and a nervous candidate reaches for a LEFT JOIN so they don't 'lose rows'. That emits long sessions with a NULL device type and a NULL operating system, rows that cannot answer 'which device', and the result grows past the 102 rows the grade expects.


Break it down

Step 1: Keep only the long sessions

WHERE s.session_duration_sec > 1000 is strictly greater than, so a session of exactly 1000 seconds stays out. A NULL session_duration_sec fails the comparison too: a session of unknown length is not a long one.

Step 2: Look up each session's device

JOIN devices d ON s.device_id = d.device_id goes from the many side to the one side. device_id is unique in devices, so no session row multiplies. A NULL device_id equals nothing, so those sessions fall away, which is exactly right when the question is which device a session ran on.

Step 3: Return the four columns

Project user_id, device_type, os_name and session_duration_sec. There is no grouping and no aggregate: each qualifying session is its own row.


The solution

Join sessions to devices and filter to long sessions
SELECT s.user_id, d.device_type, d.os_name, s.session_duration_sec
FROM user_sessions s
JOIN devices d ON s.device_id = d.device_id
WHERE s.session_duration_sec > 1000
Keep every long session
LEFT JOIN devices keeps the long sessions whose device_id is NULL and returns them with NULL device_type and NULL os_name. The row count climbs past 102, and every extra row says nothing about a device.
Keep sessions with a known device
JOIN devices keeps only sessions that matched a device. Every row names a device type and an operating system, which is the only kind of row the question can use.

Common follow-up questions