# Users Who Clicked Ads

> Ad clickers and their account details.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The ad retargeting pipeline needs a seed list of users who have clicked at least one ad. Return their user IDs.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests a simple filter with DISTINCT. Interviewers check whether you can identify users meeting a condition and deduplicate the result.

---

### Break down the requirements

#### Step 1: Filter to clicked impressions

`WHERE clicked = 1` restricts to ad impressions where the user actually clicked.

#### Step 2: Return distinct user IDs

`SELECT DISTINCT user_id` deduplicates users who clicked multiple ads.

---

### The solution

**Filter and deduplicate for seed list**

```sql
SELECT DISTINCT user_id
FROM ad_impressions
WHERE clicked = 1
```

> **Cost Analysis**
>
> Full scan of 250M rows. With a 50/50 click distribution, ~125M rows pass the filter. The DISTINCT reduces to ~10M user IDs. An index on `(clicked, user_id)` would enable an efficient index-only scan.

> **Interviewers Watch For**
>
> Whether you involve the users table unnecessarily. The prompt only asks for user IDs from impressions; joining to users adds cost without benefit here.

> **Common Pitfall**
>
> Forgetting DISTINCT. A power user who clicked 100 ads would appear 100 times without deduplication.

---

## Common follow-up questions

- How would you also return the number of clicks per user? _(GROUP BY user_id with COUNT(*) instead of DISTINCT.)_
- What if you needed usernames instead of user IDs? _(JOIN to the users table on user_id.)_
- How would you find users who saw ads but never clicked? _(Anti-pattern: WHERE user_id NOT IN (SELECT user_id WHERE clicked = 1).)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/users_who_clicked_ads)
- [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.