Idle Team Members

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
L5
Asked in
Chubb

Problem

Each row in user_sessions is one session logged against a user_id from users, and some accounts have never logged a single one. Return the user ID and username of every account with zero session history, lowest user ID first.

They signed up. Then the trail went cold.

Tables

users

Columns: user_id INTEGER, username TEXT, email TEXT, signup_date TEXT, account_status TEXT, age_bucket TEXT

users: 10 of 200 rows
user_idusernameemailsignup_dateaccount_statusage_bucket
100alicealice@example.com2025-02-02inactive25-34
197aaron42aaron42@example.com2026-03-03suspended35-44
294 ameliaamelia@example.com2024-04-04pending_verification45-54
391arjun arjun@example.com2025-05-05active55-64
488ava99ava99@example.com2026-06-06inactive65+
585andrew_kandrew_k@example.com2024-07-07suspendedNULL
682anikaanika@example.com2025-08-08 16:34:00pending_verification18-24
779aidenaiden@example.com2026-09-09 14:53:00active25-34
876aria_baria_b@example.com2024-10-10inactive35-44
973brianbrian@example.com2025-11-11suspended45-54

user_sessions

Columns: session_id INTEGER, user_id INTEGER, device_id INTEGER, session_start TIMESTAMP, 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 141 rows
user_idusername
2040cynthia
2137colin42
2234cara_z
2428celine
2525daniel

Worked solution and explanation

An anti-join wearing a growth-team costume

This is a set difference dressed up as a reactivation list: every account in users minus every account that appears in user_sessions. Anyone can relate the 2 tables. The trap is that the relationship you reach for first, an inner join, keeps exactly the rows you need to throw away. On the seed that returns the 59 accounts with sessions, once per session, and none of the 141 that have never logged one.


Building it

Step 1: Keep every account with a LEFT JOIN

LEFT JOIN user_sessions s ON u.user_id = s.user_id keeps every row of users. An account with sessions fans out to one row per session. An account with none survives once, with every s column set to NULL.

Step 2: Test the session side for the miss

WHERE s.user_id IS NULL is true only on those padded rows, because a matched row always carries the session's user_id. If you test u.user_id instead, nothing comes back: it is the primary key and is never NULL.

Step 3: Return 2 columns, lowest ID first

Select u.user_id and u.username and sort by u.user_id. The filter has already removed every matched row, so each idle account appears exactly once and you don't need a dedup step.

The solution

Anti-join for users with no sessions
SELECT u.user_id, u.username
FROM users u
LEFT JOIN user_sessions s ON u.user_id = s.user_id
WHERE s.user_id IS NULL
ORDER BY u.user_id

Common follow-up questions