Targeted Ad Campaigns
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
Only clicked impressions belong in our audit, and only those served for the 'RETARGETING_CART' and 'BRAND_AWARENESS_Q1' campaigns. Pull those full impression records from ad_impressions.
High-value impressions. Targeted precisely.
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 |
|---|---|---|---|---|---|
| 1525 | 488 | BRAND_AWARENESS_Q1 | 2026-08-26 01:55:00 | 1 | 0.8 |
| 2055 | 488 | RETARGETING_CART | 2026-09-08 11:05:00 | 1 | 1.1 |
| 3645 | 488 | BRAND_AWARENESS_Q1 | 2026-03-10 17:35:00 | 1 | 2 |
| 4175 | 488 | RETARGETING_CART | 2026-04-20 03:45:00 | 1 | 2.3 |
| 10009500 | 488 | BRAND_AWARENESS_Q1 | 2026-07-26 01:55:00 | 1 | 0.8 |
Worked solution and explanation
This is a boolean-precedence check wearing an ad-audit costume. Anyone can name the two campaigns and the click flag. What separates candidates is whether the click condition lands on both campaigns or only on the second one. Write ad_campaign = 'RETARGETING_CART' OR ad_campaign = 'BRAND_AWARENESS_Q1' AND clicked = 1 and SQL reads it as RETARGETING_CART OR (BRAND_AWARENESS_Q1 AND clicked = 1), because AND binds tighter than OR. Every unclicked 'RETARGETING_CART' impression slides into the audit, and on this table that is most of them. The click audit comes back padded with non-clicks.
Getting there
Step 1: Pin the campaigns as one set
Treat the two labels as a single membership test: ad_campaign IN ('RETARGETING_CART', 'BRAND_AWARENESS_Q1'). Precedence cannot split a list, so whatever you AND onto it applies to the whole set. Match the labels exactly, because 'PRODUCT_LAUNCH_X' and 'SUMMER_SALE_2024' sit right beside them in ad_impressions.
Step 2: Keep only the clicks
clicked is an INTEGER flag, 1 for a click and 0 otherwise, so the predicate is clicked = 1. It is ANDed after the membership test, which covers both campaigns and keeps clicks only.
Step 3: Return the whole record
The audit wants each impression as stored, so the projection is the full row of ad_impressions. Some impressions carry a NULL user_id. They are still impressions, and nothing in the ask drops them, so add no extra filter.
SELECT *
FROM ad_impressions
WHERE ad_campaign IN ('RETARGETING_CART', 'BRAND_AWARENESS_Q1') AND clicked = 1SELECT *
FROM ad_impressions
WHERE ad_campaign = 'RETARGETING_CART'
OR ad_campaign = 'BRAND_AWARENESS_Q1' AND clicked = 1Parses as RETARGETING_CART OR (BRAND_AWARENESS_Q1 AND clicked = 1).
- Bare OR
- The click filter attaches only to '
BRAND_AWARENESS_Q1'. Every 'RETARGETING_CART' row returns, whatever itsclickedvalue. - Grouped membership
ad_campaign IN (...) AND clicked = 1, or the sameORwrapped in parentheses. The click filter covers both campaigns and only clicked rows survive.
Common follow-up questions
- If `ad_campaign` arrived in mixed case, such as '`Retargeting_Cart`', how would you keep the filter robust? (Normalizing case inside the membership test without breaking exact matching.)
- Now restrict the audit to the last 90 days of `impression_time`: where does that predicate go, and does it need parentheses? (Whether the candidate reapplies the precedence lesson to a third condition.)
- Instead of raw rows, return each campaign's click count and total `revenue`. (Moving from a filter to a grouped summary over the same predicate.)