Unique Searchers
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
- L3
- Asked in
- Airbnb
Problem
The search team is measuring platform penetration: how many unique users have performed at least one search?
How many users actually searched?
Tables
search_queries
Columns: query_id INTEGER, user_id INTEGER, search_term TEXT, results_count INTEGER, clicked_result INTEGER, query_time TEXT
| query_id | user_id | search_term | results_count | clicked_result | query_time |
|---|---|---|---|---|---|
| 2071 | 100 | laptop deals | 17 | 2 | 2026-02-02 01:11:00 |
| 2142 | 197 | phone case iphone 15 | 34 | 3 | 2026-03-03 02:22:00 |
| 2213 | 294 | usb-c cable 6ft | 51 | NULL | 2026-04-04 03:33:00 |
| 2284 | NULL | gaming mouse | 68 | 5 | 2026-05-05 04:44:00 |
| 2355 | 488 | mechanical keyboard | 85 | 6 | 2026-06-06 05:55:00 |
| 2426 | 585 | 4k monitor | 102 | NULL | 2026-07-07 06:06:00 |
| 2497 | 682 | standing desk | 119 | 8 | 2026-08-08 07:17:00 |
| 2568 | NULL | webcam 1080p | 136 | 9 | 2026-09-09 08:28:00 |
| 2639 | 876 | bluetooth speaker | 153 | NULL | 2026-01-10 09:39:00 |
| 2710 | 973 | smart watch | 170 | 1 | 2026-02-11 10:50:00 |
Expected output
| unique_searchers |
|---|
| 75 |
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
SELECT COUNT(DISTINCT user_id) AS unique_searchers
FROM search_queriesCommon 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.)