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
| user_id | username | signup_date | account_status | age_bucket | |
|---|---|---|---|---|---|
| 100 | alice | alice@example.com | 2025-02-02 | inactive | 25-34 |
| 197 | aaron42 | aaron42@example.com | 2026-03-03 | suspended | 35-44 |
| 294 | amelia | amelia@example.com | 2024-04-04 | pending_verification | 45-54 |
| 391 | arjun | arjun@example.com | 2025-05-05 | active | 55-64 |
| 488 | ava99 | ava99@example.com | 2026-06-06 | inactive | 65+ |
| 585 | andrew_k | andrew_k@example.com | 2024-07-07 | suspended | NULL |
| 682 | anika | anika@example.com | 2025-08-08 16:34:00 | pending_verification | 18-24 |
| 779 | aiden | aiden@example.com | 2026-09-09 14:53:00 | active | 25-34 |
| 876 | aria_b | aria_b@example.com | 2024-10-10 | inactive | 35-44 |
| 973 | brian | brian@example.com | 2025-11-11 | suspended | 45-54 |
user_sessions
Columns: session_id INTEGER, user_id INTEGER, device_id INTEGER, session_start TIMESTAMP, 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 |
Expected output
| user_id | username |
|---|---|
| 2040 | cynthia |
| 2137 | colin42 |
| 2234 | cara_z |
| 2428 | celine |
| 2525 | daniel |
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
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_idCommon follow-up questions
- How would you show how long each idle user has been registered? (Tests date arithmetic on `signup_date`, which is stored as text.)
- What if idle means no sessions in the last 30 days, not ever? (Tests moving the date filter into the `ON` clause of the `LEFT JOIN` so unmatched users survive.)
- How would you count idle versus active users in one pass? (Tests conditional aggregation over the left join.)