Unclicked Searches by Campaign
A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- medium
- Seniority
- L3
- Asked in
- Apple
Problem
The per-campaign search report credits some campaigns with more searches than the users they reached ever ran. Searches sit in search_queries and campaign exposure in ad_impressions, linked only by the user behind each event: report how many searches each campaign's audience ran, broken down by the result position the searcher clicked, with searches that ended without a click as their own bucket.
Searched but never clicked.
Tables
search_queries
Columns: query_id INTEGER, user_id INTEGER, search_term TEXT, results_count INTEGER, clicked_result INTEGER, query_time TIMESTAMP
| 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 |
ad_impressions
Columns: impression_id INTEGER, user_id INTEGER, ad_campaign TEXT, impression_time TIMESTAMP, clicked BOOLEAN, revenue REAL
| impression_id | user_id | ad_campaign | impression_time | clicked | revenue |
|---|---|---|---|---|---|
| 253 | 100 | BRAND_AWARENESS_Q1 | 2026-02-02 01:07:00 | 0 | NULL |
| 306 | 197 | PRODUCT_LAUNCH_X | 2026-03-03 02:14:00 | 0 | NULL |
| 359 | 294 | RETARGETING_CART | 2026-04-04 03:21:00 | 0 | NULL |
| 412 | 391 | HOLIDAY_PROMO | 2026-05-05 04:28:00 | 0 | NULL |
| 465 | 488 | NEW_USER_ACQU | 2026-06-06 05:35:00 | 1 | 0.2 |
| 518 | NULL | LOYALTY_PROGRAM | 2026-07-07 06:42:00 | 0 | NULL |
| 571 | 682 | FLASH_SALE_48H | 2026-08-08 07:49:00 | 0 | NULL |
| 624 | 779 | SUMMER_SALE_2024 | 2026-09-09 08:56:00 | 0 | NULL |
| 677 | 876 | BRAND_AWARENESS_Q1 | 2026-01-10 09:03:00 | 0 | NULL |
| 730 | 973 | PRODUCT_LAUNCH_X | 2026-02-11 10:10:00 | 1 | 0.35 |
Expected output
| ad_campaign | clicked_result | event_count |
|---|---|---|
| BRAND_AWARENESS_Q1 | NULL | 4 |
| BRAND_AWARENESS_Q1 | 2 | 2 |
| BRAND_AWARENESS_Q1 | 6 | 2 |
| FLASH_SALE_48H | NULL | 4 |
| LOYALTY_PROGRAM | NULL | 8 |
Worked solution and explanation
The metric hiding behind the join
This is a per-campaign search count wearing a campaign-attribution costume. A campaign never touches a search directly: the only bridge is user_id, so you join search_queries to ad_impressions on the user, then count searches per campaign and per clicked_result. The trap is the grain of the impression side. Fire COUNT(*) over the raw join and you count search-by-impression pairs, not searches: a user shown one campaign across 3 impressions has each of their searches counted 3 times. Every heavy user inflates their campaign's totals, and the position breakdown lands wrong for exactly the users who matter most.
- search_queries: query_id INTEGER PK, user_id INTEGER FK, clicked_result INTEGER, query_time TEXT
- ad_impressions: impression_id INTEGER PK, user_id INTEGER FK, ad_campaign TEXT, impression_time TEXT
- search_queries.user_id references ad_impressions.user_id
Build it
Step 1: Collapse impressions to the audience
The question is about who a campaign reached, and the impressions log records every time it reached them. Group ad_impressions by user_id and ad_campaign in a derived table, so the audience holds one row per user per campaign. That one move fixes the grain before anything multiplies.
Step 2: Attribute and count each search once
Inner-join search_queries to that audience on user_id. Each search now meets each of its user's campaigns exactly once, so a plain COUNT(*) grouped by ad_campaign and clicked_result counts real searches. Searches with no user, or by a user never shown an ad, fall out here, and that is correct: they belong to no campaign.
Step 3: Keep the no-click bucket
A search that ended without a click carries a NULL in clicked_result, and GROUP BY keeps it as its own group. Never filter it away: it is the no-click bucket every campaign reports, and on this data it is usually the biggest one.
SELECT
audience.ad_campaign,
s.clicked_result,
COUNT(*) AS event_count
FROM search_queries s
JOIN (
SELECT user_id, ad_campaign
FROM ad_impressions
GROUP BY user_id, ad_campaign
) AS audience ON s.user_id = audience.user_id
GROUP BY audience.ad_campaign, s.clicked_resultSELECT
a.ad_campaign,
s.clicked_result,
COUNT(*) AS event_count
FROM search_queries s
JOIN ad_impressions a ON s.user_id = a.user_id
GROUP BY a.ad_campaign, s.clicked_resultCounts joined rows, so every repeat impression multiplies its user's searches.
- Count the raw join
COUNT(*)over the raw join: one search by a user shown a campaign in 3 impressions counts 3 times. The totals look busier than reality, and the position breakdown skews toward the heaviest users.- Fix the grain, then count
- Collapse
ad_impressionsto (user_id,ad_campaign) pairs first, thenCOUNT(*). Each search counts once per campaign no matter how many impressions carried it, which is the number the business asked for.
Common follow-up questions
- How would you report the click-through rate per campaign, not just raw counts? (Count the searches where `clicked_result IS NOT NULL`, divided by the campaign's total search count at the same grain.)
- What if a search should only attribute to impressions within an hour of the query? (The audience can no longer be collapsed up front: add a time-window predicate correlating `query_time` with `impression_time`, then count each search once per campaign.)
- How would you pivot clicked versus unclicked into 2 columns per campaign? (Conditional aggregation over the attributed searches, testing `clicked_result` for `NULL` to fill the 2 columns.)