Device Type Serving Most Users

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
Netflix

Problem

The device team's reach report crowns a single winner, but the sessions in user_sessions show other device types serving nearly as many unique users. Using each session's device type from devices, list every device type that reaches at least 90% of the leader's unique-user count, along with that count.

One device type serves more users than the rest.

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 3 of 3 rows
device_typeuser_count
desktop12
smart_tv13
tablet13

Worked solution and explanation

This is a relative-cutoff filter dressed up as a device leaderboard. Two things must be right at once: the reach number counts people, not sessions, and the bar is measured against a leader the query has to find first. Anyone writes the join and the COUNT(DISTINCT us.user_id). What separates candidates is refusing to treat 'the most' as one row. On this data 'smart_tv' and 'tablet' tie at 13 and 'desktop' sits at 12, so a sort-and-take-one answer returns a third of the front-runners, and a bar computed with integer math quietly lets the two 11s in.

Building it

Step 1: Count people per device type

Inner-join user_sessions to devices on device_id and take COUNT(DISTINCT us.user_id) per device_type. Users have several sessions each, and one user ran on 'desktop', 'tablet' and 'smart_tv' in the same morning, so COUNT(*) measures traffic, not reach. Sessions with a NULL device_id match no device and drop out, which is correct: they have no type to credit.

Step 2: Name the per-type counts once

Put the aggregate in a CTE, type_users. The final filter reads it twice, once row by row and once to find the leader, and the CTE makes both reads use the same numbers.

Step 3: Compare against the leader, not a constant

(SELECT MAX(user_count) FROM type_users) is one value, 13 here, and 0.9 * turns it into the bar, 11.7. Every type at or above it survives: the two 13s and the 12. However the top ties, and however many types fall inside the band, they all come back.

Every type within 90% of the leader
WITH type_users AS (
    SELECT
        d.device_type,
        COUNT(DISTINCT us.user_id) AS user_count
    FROM user_sessions us
    JOIN devices d ON us.device_id = d.device_id
    GROUP BY d.device_type
)
SELECT device_type, user_count
FROM type_users
WHERE user_count >= 0.9 * (SELECT MAX(user_count) FROM type_users)
Sort and take one
ORDER BY user_count DESC LIMIT 1 returns whichever 13 the engine emits first and never sees 'desktop'. Raising it to LIMIT 3 happens to match today, but it hardcodes a number of front-runners nobody asked for.
Band around the leader
WHERE user_count >= 0.9 * (SELECT MAX(user_count) FROM type_users) returns every type that clears the bar, however many there are and however the top ties.

Common follow-up questions