Average Session Duration by Device

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
Amazon

Problem

Each row of user_sessions records a session's length in whole seconds and the device_id it ran on, while the type of every device is kept in devices. Report the average session length in minutes for each device type, listing the device types alphabetically.

Session length, device by device.

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 5 rows
device_typeavg_session_minutes
desktop24.42892156862745
mobile22.233854166666667
smart_tv23.819607843137256
tablet22.832323232323233
wearable22.894791666666666

Worked solution and explanation

This is a unit conversion dressed up as a device comparison. The lookup is the part everyone gets right: user_sessions only knows device_id, so the type has to come from devices. What separates candidates is the minutes. session_duration_sec is an INTEGER, and integer divided by integer drops the remainder on every row, so a 53-second session counts as zero minutes. Average those truncated values and every device type comes in about half a minute short, a gap nobody notices because the numbers still look plausible.

Building it

Step 1: Attach a type to every session

Join user_sessions to devices on device_id. A plain inner join is the right call here: 23 sessions have no device_id, and a session with no device cannot be credited to a type, so letting them fall out is the answer, not an accident.

Step 2: Convert before you divide

Turn the seconds into a real number first, CAST(us.session_duration_sec AS REAL), then divide by 60. The order matters: once the value is real, the division keeps the fraction, and a 90-second session is 1.5 minutes instead of 1.

Step 3: Average per device_type, alphabetical

GROUP BY d.device_type gives one row per type, and AVG skips the 17 sessions whose duration was never recorded, which is exactly what an average of measured lengths should do. ORDER BY d.device_type delivers the alphabetical listing.

Average session minutes per device type
SELECT d.device_type, AVG(CAST(us.session_duration_sec AS REAL) / 60) AS avg_session_minutes FROM user_sessions AS us JOIN devices AS d ON us.device_id = d.device_id GROUP BY d.device_type ORDER BY d.device_type

The CAST runs before the division, so no session loses its leftover seconds.

Divides integers
AVG(us.session_duration_sec / 60) truncates each session first: 53 seconds becomes 0, 119 becomes 1. The average is taken over whole minutes, and every device type reads low by roughly 0.5.
Divides a real
AVG(CAST(us.session_duration_sec AS REAL) / 60) keeps 53 seconds as 0.883 minutes. The average is the true mean length, and it matches the expected output to every decimal.

Common follow-up questions