Users Per Device Type

An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
easy
Seniority
L3
Asked in
Walmart

Problem

Our user_sessions log records the device behind each session, but some sessions point at a device that is missing from the devices catalog or name no device at all. For each device type, count how many different users had a session on it, and keep the sessions we can't tie to a known device together as one group with no device type.

Users per device. The split.

Tables

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 6 rows
device_typeuser_count
NULL16
desktop12
mobile11
smart_tv13
wearable11

Worked solution and explanation

The real problem hiding in a simple count

This looks like a tidy count of users per device type, but the real test is what happens to the sessions you cannot place. Attributing a fact table to a dimension is an outer join anchored on user_sessions, not on devices: some session rows carry a device_id that is null or points at a device absent from devices. Reach for an inner join, or anchor the join on devices, and every one of those sessions silently vanishes, taking its users with it, so nobody sees the logging gap. Anyone can group and count; the tell is preserving the unmatched sessions as their own group and using COUNT(DISTINCT s.user_id) so a user with fifty sessions counts once.


Walking through it

Step 1: Anchor the join on sessions

Start FROM user_sessions s and LEFT JOIN devices d ON s.device_id = d.device_id. Direction is the whole point: user_sessions is the fact table whose every row must survive, so it is the left side. A session whose device_id is null or missing from devices still comes through, with d.device_type returning as null.

Step 2: Group and count unique users

GROUP BY d.device_type folds every matched session into its type and collapses every unmatched session into a single null-device_type group. COUNT(DISTINCT s.user_id) then dedupes repeat sessions so each person is counted once per group.


The solution

Left join anchored on sessions, distinct users per device type
SELECT d.device_type, COUNT(DISTINCT s.user_id) AS user_count
FROM user_sessions s
LEFT JOIN devices d ON s.device_id = d.device_id
GROUP BY d.device_type

Common follow-up questions