Retargeting Campaign Impressions
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
- Meta
Problem
Impression numbers for the retargeting segment don't reconcile with what the ad operations team expects. Pull every field from ad_impressions for the campaigns whose name contains 'retarget' so they can check what is actually flowing into that segment.
Retargeting impressions. All of them.
Tables
ad_impressions
Columns: impression_id INTEGER, user_id INTEGER, ad_campaign TEXT, impression_time TEXT, clicked INTEGER, 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
| impression_id | user_id | ad_campaign | impression_time | clicked | revenue |
|---|---|---|---|---|---|
| 359 | 294 | RETARGETING_CART | 2026-04-04 03:21:00 | 0 | NULL |
| 783 | 100 | RETARGETING_CART | 2026-03-12 11:17:00 | 0 | NULL |
| 1207 | 876 | RETARGETING_CART | 2026-02-20 19:13:00 | 0 | NULL |
| 1631 | 682 | RETARGETING_CART | 2026-01-28 03:09:00 | 0 | NULL |
| 2055 | 488 | RETARGETING_CART | 2026-09-08 11:05:00 | 1 | 1.1 |
Worked solution and explanation
This is a case-mismatch filter dressed up as a reconciliation audit. You are handed the lowercase token 'retarget', but every stored value is the uppercase 'RETARGETING_CART'. Anyone can write SELECT * with a wildcard pattern. What separates candidates is noticing that the pattern, typed as given, is case-sensitive on the grader. Miss that and the audit comes back empty, which tells ad ops there is no retargeting traffic at all: a false answer that looks like a finding.
Getting to the query
Step 1: Read the stored values before writing the pattern
The sample rows show ad_campaign holding values like 'RETARGETING_CART' and 'BRAND_AWARENESS_Q1', all uppercase with underscores. The word you were given is lowercase. That gap is the whole problem, and you can see it in ten seconds of looking at the data.
Step 2: Match anywhere in the name, ignoring case
'retarget' sits at the start of 'RETARGETING_CART', but the ask is 'contains', so wrap it in % on both sides. Then make the comparison ignore case with ILIKE. Writing LIKE '%RETARGET%' in the stored case also works on this data; ILIKE keeps working if someone later ships 'Retargeting_Email'.
Step 3: Return the whole row
"Every field" means SELECT *. In an audit you don't yet know which column explains the discrepancy, so revenue (mostly NULL here because most impressions were never clicked), user_id and impression_time all have to come back.
- Case-sensitive match
ad_campaign LIKE '%retarget%'compares letter by letter, 'r' is not 'R', and zero rows come back. It reads as correct and fails silently.- Case-insensitive match
ad_campaign ILIKE '%retarget%'folds case before comparing, so every 'RETARGETING_CART' impression is returned, from all years in the table.
SELECT *
FROM ad_impressions
WHERE ad_campaign ILIKE '%retarget%'Common follow-up questions
- How would you return the matching impressions for only the last 30 days without scanning every partition? (Tests pruning on `impression_time`, the partition key.)
- What if a campaign named '`NON_RETARGETED_TEST`' appears and must be excluded? (Tests tightening a substring match with token boundaries or an explicit list.)
- How would you show retargeting impressions and clicks per month next to the totals ad ops reports? (Moves from row audit to reconciliation with a conditional count.)