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
| session_id | user_id | device_id | session_start | session_duration_sec | pages_viewed |
|---|---|---|---|---|---|
| 7061 | 197 | 230 | 2026-02-02 01:00:00 | 53 | 3 |
| 7122 | 294 | 259 | 2026-03-03 02:00:00 | 76 | 6 |
| 7183 | 391 | 288 | 2026-04-04 03:00:00 | 99 | 9 |
| 7244 | 488 | 317 | 2026-05-05 04:00:00 | 122 | 12 |
| 7305 | 585 | 346 | 2026-06-06 05:00:00 | 145 | 15 |
| 7366 | 682 | 375 | 2026-07-07 06:00:00 | 168 | 18 |
| 7427 | 779 | 404 | 2026-08-08 07:00:00 | 191 | 21 |
| 7488 | 876 | NULL | 2026-09-09 08:00:00 | 214 | 24 |
| 7549 | 973 | 462 | 2026-01-10 09:00:00 | 237 | 27 |
| 7610 | 1070 | 491 | 2026-02-11 10:00:00 | 260 | 30 |
devices
Columns: device_id INTEGER, device_type TEXT, os_name TEXT, os_version TEXT, browser TEXT
| device_id | device_type | os_name | os_version | browser |
|---|---|---|---|---|
| 201 | desktop | Android | 15.1 | Chrome |
| 230 | tablet | Windows | 16.2 | Firefox |
| 259 | smart_tv | macOS | 17.0 | Edge |
| 288 | wearable | Linux | 11 | Opera |
| 317 | mobile | tvOS | 12 | Samsung Internet |
| 346 | desktop | watchOS | 13 | NULL |
| 375 | tablet | iOS | 10.0.19041 | Safari |
| 404 | smart_tv | Android | 14.1 | Chrome |
| 433 | wearable | Windows | NULL | Firefox |
| 462 | mobile | macOS | 14.0 | Edge |
Expected output
| device_type | user_count |
|---|---|
| desktop | 12 |
| smart_tv | 13 |
| tablet | 13 |
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.
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 1returns whichever 13 the engine emits first and never sees 'desktop'. Raising it toLIMIT 3happens 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
- How would you return the top three device types by reach, keeping every type tied at the third spot? (Tests whether the candidate reaches for a tie-aware ordering rather than a bare `LIMIT 3`.)
- The device team now wants the 90% band per operating system. What changes? (The leader stops being one global value and becomes one per `os_name`, so the maximum must be scoped to each group.)
- What if a session should count toward reach only when it lasted at least a minute? (A row filter on `session_duration_sec` before counting, plus a decision about its `NULL` durations.)