Between Worlds

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

Problem

A user who moves between device types, a phone one day and a laptop the next, is what the growth team's cross-device review is chasing, and only sessions tied to a known device in devices say anything about that. From user_sessions, return every such session for those users, with the user ID, OS name, device type and session start.

iOS today, Android tomorrow.

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 6 rows
user_idos_namedevice_typesession_start
100iOSdesktop2026-03-21 20:00:00
100tvOSdesktop2026-07-05 12:00:00
100Androiddesktop2026-02-17 04:00:00
100Windowstablet2026-06-15 11:00:00
100macOSsmart_tv2026-06-15 11:30:00

Worked solution and explanation

What they're really testing

This is a unique-value count wearing a cross-device costume. Everyone gets the join and the grouping right; the whole problem turns on what you count per user. COUNT(device_type) counts sessions that have a device, while COUNT(DISTINCT device_type) counts the different kinds of device a user actually touched. Almost every user in this session log stays on one device type for every session, so the session tally clears 'more than one' for nearly everybody and the answer balloons from one user's 6 sessions to 169. The second half is the rejoin: the grouping hands you the qualifying user_ids, but the ask wants every session row back.


Break down the requirements

Step 1: Join user_sessions to devices

Inner-join on device_id to put device_type and os_name on every session. A session with a NULL device_id has no device, so the join drops it, which is exactly what 'tied to a known device' asks for.

Step 2: Find the cross-device users

Group the joined rows by user_id and keep HAVING COUNT(DISTINCT device_type) > 1. The DISTINCT decides the answer here: without it you count sessions, not kinds of device.

Step 3: Bring back every session

Filter the joined rows with WHERE user_id IN (...) and return user_id, os_name, device_type and session_start, one row per session, not one per user.


The solution

Find qualifying users, then rejoin for detail
SELECT s.user_id, d.os_name, d.device_type, s.session_start
FROM user_sessions s
JOIN devices d ON s.device_id = d.device_id
WHERE s.user_id IN (
    SELECT s2.user_id
    FROM user_sessions s2
    JOIN devices d2 ON s2.device_id = d2.device_id
    GROUP BY s2.user_id
    HAVING COUNT(DISTINCT d2.device_type) > 1
)
Counting sessions
HAVING COUNT(d2.device_type) > 1 is true for any user with two device-linked sessions. Nearly every user qualifies and you return 169 sessions.
Counting device types
HAVING COUNT(DISTINCT d2.device_type) > 1 is true only when two sessions ran on different kinds of device. One user qualifies and you return their 6 sessions.

Common follow-up questions