# Users Without Sessions

> Account created. Never logged in.

Canonical URL: <https://datadriven.io/problems/users_without_sessions>

Domain: SQL · Difficulty: medium · Seniority: L3

## Problem

Find users who have never started a session. Show the user ID, username, and email for each.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests the anti-join pattern. Interviewers verify that you can find unmatched records between two tables and return specific columns from the base table.

---

### Break down the requirements

#### Step 1: Left join users to sessions

`LEFT JOIN user_sessions ON users.user_id = user_sessions.user_id` preserves all users.

#### Step 2: Filter to unmatched users

`WHERE user_sessions.session_id IS NULL` isolates users with no session records.

#### Step 3: Return required columns

Select `user_id`, `username`, and `email` for each sessionless user.

---

### The solution

**Anti-join for users with no sessions**

```sql
SELECT u.user_id, u.username, u.email
FROM users u
LEFT JOIN user_sessions s ON u.user_id = s.user_id
WHERE s.session_id IS NULL
```

> **Cost Analysis**
>
> The LEFT JOIN between 10M users and 50M sessions is converted by the optimizer into an anti-join. An index on `user_sessions(user_id)` enables efficient probe lookups. The result set is ~6M users (10M minus 4M with sessions).

> **Interviewers Watch For**
>
> Whether you check IS NULL on a column from the right table (user_sessions), not the left table. Checking a left-table column for NULL would filter differently.

> **Common Pitfall**
>
> Checking `WHERE s.user_id IS NULL` works but is less explicit. If `user_id` were nullable in user_sessions, this could produce false positives. Using the primary key (`session_id`) is cleaner.

---

## Common follow-up questions

- How would you solve this with NOT EXISTS instead? _(WHERE NOT EXISTS (SELECT 1 FROM user_sessions WHERE user_sessions.user_id = users.user_id).)_
- What if you needed to find users who had sessions but none in the last 30 days? _(Combines anti-join on recent sessions with a join on historical sessions.)_
- Which approach is typically faster: LEFT JOIN IS NULL or NOT EXISTS? _(Performance depends on the optimizer; NOT EXISTS can short-circuit on the first match.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/users_without_sessions)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.