Ghosts in the Campaign

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
City and County of San Francisco

Problem

Some rows in ad_impressions were logged with no user we can match in users, and they still count toward a campaign's reach. Pull every impression from the holiday promo campaign with all of its fields and its user's details alongside, in the order the impressions were logged, earliest first.

The holiday sale campaign. How did it do?

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

users

Columns: user_id INTEGER, username TEXT, email TEXT, signup_date TEXT, account_status TEXT, age_bucket TEXT

users: 10 of 200 rows
user_idusernameemailsignup_dateaccount_statusage_bucket
100alicealice@example.com2025-02-02inactive25-34
197aaron42aaron42@example.com2026-03-03suspended35-44
294 ameliaamelia@example.com2024-04-04pending_verification45-54
391arjun arjun@example.com2025-05-05active55-64
488ava99ava99@example.com2026-06-06inactive65+
585andrew_kandrew_k@example.com2024-07-07suspendedNULL
682anikaanika@example.com2025-08-08 16:34:00pending_verification18-24
779aidenaiden@example.com2026-09-09 14:53:00active25-34
876aria_baria_b@example.com2024-10-10inactive35-44
973brianbrian@example.com2025-11-11suspended45-54

Expected output

First 5 of 16 rows
impression_iduser_idad_campaignimpression_timeclickedrevenueusernameemailsignup_dateaccount_statusage_bucket
10009487NULLHOLIDAY_PROMO2023-12-13 12:24:000NULLNULLNULLNULLNULLNULL
10009479391HOLIDAY_PROMO2025-04-05 04:28:000NULLarjun arjun@example.com2025-05-05active55-64
2108NULLHOLIDAY_PROMO2026-01-09 12:12:000NULLNULLNULLNULLNULLNULL
5500973HOLIDAY_PROMO2026-02-17 04:40:0013.05brianbrian@example.com2025-11-11suspended45-54
10009495973HOLIDAY_PROMO2026-02-21 20:20:0010.65brianbrian@example.com2025-11-11suspended45-54

Worked solution and explanation

What this is really about

Beneath the campaign-report costume, this is a join that is not allowed to lose rows. The impressions are the driving set: every holiday impression is part of the campaign's reach whether or not anyone can be attributed to it. On this data five of the sixteen 'HOLIDAY_PROMO' impressions carry a NULL user_id, so the reflexive inner join to users quietly returns eleven rows and the report undercounts reach by almost a third. Anyone can write the join; the tell is choosing which side survives it.

Two quieter traps sit around it. The campaign filter is a pattern match, and LIKE compares case exactly, so the pattern has to meet the stored uppercase spelling. And 'the order the impressions were logged' means impression_time, not impression_id: the later-loaded rows carry ids above ten million with earlier timestamps.


The build

Step 1: Match the campaign as it is stored

ad_campaign holds 'HOLIDAY_PROMO' in uppercase, and LIKE is case-sensitive here, so ai.ad_campaign LIKE 'HOLIDAY%' finds it while LIKE '%holiday%' returns nothing at all. Read the sample rows before writing a pattern: the spelling on disk is the spelling the filter has to meet.

Step 2: Anchor on ad_impressions and LEFT JOIN users

A NULL user_id never equals anything, so under an inner join those five impressions have no partner and vanish. With LEFT JOIN users AS u ON ai.user_id = u.user_id they come back with username, email and the other user columns empty, which is exactly what an unattributed impression should look like.

Step 3: Sort by impression_time, not by the id

The timestamps are 'YYYY-MM-DD HH:MM:SS' text, so they sort chronologically as strings. Sorting by impression_id agrees on the first rows, then pushes the three high-id rows to the bottom even though they were logged earlier.


The solution

`LEFT JOIN` that keeps every holiday impression
SELECT
  ai.impression_id,
  ai.user_id,
  ai.ad_campaign,
  ai.impression_time,
  ai.clicked,
  ai.revenue,
  u.username,
  u.email,
  u.signup_date,
  u.account_status,
  u.age_bucket
FROM ad_impressions AS ai
LEFT JOIN users AS u
  ON ai.user_id = u.user_id
WHERE ai.ad_campaign LIKE 'HOLIDAY%'
ORDER BY ai.impression_time
INNER JOIN
Eleven rows. Every impression whose user_id is NULL has no partner in users and disappears, so the campaign looks smaller than it was.
LEFT JOIN
Sixteen rows. The five unattributed impressions stay, with username, email, signup_date, account_status and age_bucket empty.

Common follow-up questions