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

ad_impressions: 10 of 200 rows
impression_iduser_idad_campaignimpression_timeclickedrevenue
253100BRAND_AWARENESS_Q12026-02-02 01:07:000NULL
306197PRODUCT_LAUNCH_X2026-03-03 02:14:000NULL
359294RETARGETING_CART2026-04-04 03:21:000NULL
412391HOLIDAY_PROMO2026-05-05 04:28:000NULL
465488NEW_USER_ACQU2026-06-06 05:35:0010.2
518NULLLOYALTY_PROGRAM2026-07-07 06:42:000NULL
571682FLASH_SALE_48H2026-08-08 07:49:000NULL
624779SUMMER_SALE_20242026-09-09 08:56:000NULL
677876BRAND_AWARENESS_Q12026-01-10 09:03:000NULL
730973PRODUCT_LAUNCH_X2026-02-11 10:10:0010.35

Expected output

First 5 of 5 rows
impression_iduser_idad_campaignimpression_timeclickedrevenue
1525488BRAND_AWARENESS_Q12026-08-26 01:55:0010.8
2055488RETARGETING_CART2026-09-08 11:05:0011.1
3645488BRAND_AWARENESS_Q12026-03-10 17:35:0012
4175488RETARGETING_CART2026-04-20 03:45:0012.3
10009500488BRAND_AWARENESS_Q12026-07-26 01:55:0010.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.

Clicked impressions for the two audited campaigns
SELECT *
FROM ad_impressions
WHERE ad_campaign IN ('RETARGETING_CART', 'BRAND_AWARENESS_Q1') AND clicked = 1
The ungrouped version that leaks non-clicks
SELECT *
FROM ad_impressions
WHERE ad_campaign = 'RETARGETING_CART'
   OR ad_campaign = 'BRAND_AWARENESS_Q1' AND clicked = 1

Parses 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 its clicked value.
Grouped membership
ad_campaign IN (...) AND clicked = 1, or the same OR wrapped in parentheses. The click filter covers both campaigns and only clicked rows survive.

Common follow-up questions