# Unique Searchers

> How many users actually searched?

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The search team is measuring platform penetration: how many unique users have performed at least one search?

## Worked solution and explanation

### Why this problem exists in real interviews

This is a basic `COUNT(DISTINCT ...)` problem. Interviewers use it to verify that you understand the difference between counting rows and counting unique entities.

---

### Break down the requirements

#### Step 1: Count unique users

`COUNT(DISTINCT user_id)` returns the number of unique users who have performed at least one search.

---

### The solution

**Single distinct count**

```sql
SELECT COUNT(DISTINCT user_id) AS unique_searchers
FROM search_queries
```

> **Cost Analysis**
>
> Full scan of 30M rows. The DISTINCT operation builds a hash set of 2M user IDs. Memory usage is proportional to the number of distinct values, not total rows.

> **Interviewers Watch For**
>
> Candidates who use a subquery with SELECT DISTINCT and then COUNT(*). While correct, `COUNT(DISTINCT ...)` is more concise and typically more efficient.

> **Common Pitfall**
>
> Using `COUNT(*)` which returns 30M (total searches) instead of 2M (unique users).

---

## Common follow-up questions

- How would you count unique searchers per month? _(Add GROUP BY with a date truncation function for monthly breakdown.)_
- What if user_id could be NULL for anonymous searches? _(COUNT(DISTINCT user_id) already excludes NULLs, but the business may want to count anonymous users separately.)_
- What is the difference between COUNT(DISTINCT user_id) and SELECT COUNT(*) FROM (SELECT DISTINCT user_id ...)? _(Functionally equivalent, but the inline DISTINCT is typically optimized better.)_

## Related

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