The Screens They Carry

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
L4
Asked in
Airbnb

Problem

Each user_sessions row records which device a user was on, with the user's age group kept in users and the device's type in devices. Count the unique devices the 25-34 age group has used, by device type, from most to fewest, with equal counts listed alphabetically by type.

One age cohort, and every kind of screen it reaches for.

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

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

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 5 rows
device_typedevice_count
smart_tv9
mobile5
desktop4
tablet4
wearable2

Worked solution and explanation

What this really is

Beneath the device-diversity framing, this is a fan-in with a dedup at the far end. A user has many sessions, each session points at exactly one device, and the same device reappears across many sessions. The skill being probed: can you walk the bridge from users through user_sessions to devices, then collapse the fan-out so each physical device is counted once? Anyone can write the three joins. The trap is the aggregate: COUNT(*) counts session rows, not devices, and since one popular device generates many sessions it silently inflates every device_type. COUNT(DISTINCT device_id) is the whole game.


Walking the chain

Step 1: Scope the cohort

WHERE u.age_bucket = '25-34' scopes the population to the cohort of interest before any counting. Applying it on the users table keeps the filter cheap and lets the optimizer prune early.

Step 2: Join through the bridge

There is no direct users-to-devices link. Go users -> user_sessions on user_id, then user_sessions -> devices on device_id. The session table is the bridge that ties a person to the hardware they actually used; skipping it has no join key to stand on.

Step 3: Dedup as you count

GROUP BY d.device_type with COUNT(DISTINCT d.device_id) buckets the surviving rows by kind of device and dedups within each bucket, so a device used in fifty sessions still counts as one. ORDER BY device_count DESC, device_type puts the most-used kinds on top and lists equal counts alphabetically.


The solution

Filtered three-table join with a deduped count
SELECT d.device_type, COUNT(DISTINCT d.device_id) AS device_count
FROM users u
JOIN user_sessions us ON u.user_id = us.user_id
JOIN devices d ON us.device_id = d.device_id
WHERE u.age_bucket = '25-34'
GROUP BY d.device_type
ORDER BY device_count DESC, device_type

Common follow-up questions