The Faithful Few

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
Quora

Problem

Each user_sessions row is one session on one device, and devices holds each device's type, so a user's history can span several types. Count, per device type, the users who have never been seen on any other type, most loyal type first.

Loyal to one platform only.

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_typeexclusive_user_count
tablet12
smart_tv12
wearable11
mobile11
desktop11

Worked solution and explanation

What this problem really is

Strip the costume and this is a per-user set-cardinality test wearing a product-analytics disguise. A user is loyal to a platform only when the set of device_type values across all of their sessions has exactly one member. Anyone can filter rows to device_type = 'mobile'. What separates candidates is seeing that a row filter answers 'who ever used mobile', never 'who used mobile and nothing else'. Get that wrong and every multi-device user is counted once for each type they touched, inflating every platform.


Break down the requirements

Step 1: Collapse each user's sessions to their set of types

Inner-join user_sessions to devices on device_id so every session carries its type, then group by user_id and keep only the users whose COUNT(DISTINCT device_type) is 1, in a HAVING. Sessions with a NULL device_id find no device and drop out of the join, so they neither add a type nor disqualify anyone.

Step 2: Re-count the survivors by their one type

Each surviving user has exactly one type, so MIN(device_type) reads it back unambiguously: the minimum of a one-value set is that value. That leaves one row per loyal user in exclusive_users, and a second GROUP BY device_type with COUNT(*) gives each platform's loyal base. The order of the two aggregations is the whole problem: filter per user first, count per type second.

Row filter
Join, then GROUP BY device_type with COUNT(DISTINCT user_id), counts everyone who ever opened a session on that type. A user with tablet and smart TV sessions lands in both totals.
Per-user constraint
GROUP BY user_id with HAVING COUNT(DISTINCT device_type) = 1 first, so that same user is dropped entirely, and only single-type users reach the per-type count.

The solution

Loyal users first, then a count per type
WITH exclusive_users AS (
    SELECT s.user_id, MIN(d.device_type) AS device_type
    FROM user_sessions s
    JOIN devices d ON s.device_id = d.device_id
    GROUP BY s.user_id
    HAVING COUNT(DISTINCT d.device_type) = 1
)
SELECT device_type, COUNT(*) AS exclusive_user_count
FROM exclusive_users
GROUP BY device_type
ORDER BY exclusive_user_count DESC

Common follow-up questions