# Targeted Ad Campaigns

> High-value impressions. Targeted precisely.

Canonical URL: <https://datadriven.io/problems/targeted_ad_campaigns>

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The ad ops team is auditing click-through data for two specific campaigns. Pull all impression records from 'RETARGETING_CART' or 'BRAND_AWARENESS_Q1' where the user actually clicked.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests whether a candidate can demonstrate filtering to a specific set of values. This is a foundational check that interviewers use early in a round to verify baseline proficiency.

---

### Break down the requirements

#### Step 1: Filter to the target set

The `IN` list restricts the query to only the specified values, avoiding a full-table scan of irrelevant rows.

#### Step 2: Select the target columns

The SELECT clause picks exactly the columns the prompt asks for. Returning extra columns or missing a required alias would fail the grading check.

---

### The solution

**Filter ad_impressions for two named campaigns where clicked is true**

```sql
SELECT *
FROM ad_impressions
WHERE ad_campaign IN ('RETARGETING_CART', 'BRAND_AWARENESS_Q1') AND clicked = 1
```

> **Cost Analysis**
>
> With ~200M rows, the query performs a single sequential scan. An index on the filter/join columns would reduce the scan to a seek.

> **Interviewers Watch For**
>
> Interviewers watch for whether the query returns exactly the columns and ordering the prompt specifies; how quickly you identify the core operation and write clean, minimal code.

> **Common Pitfall**
>
> Returning extra columns that the prompt did not ask for, or using the wrong column alias, causes a grading mismatch even when the logic is correct.

---

## Common follow-up questions

- If ad_campaign values have inconsistent casing ('Retargeting_Cart' vs 'RETARGETING_CART'), how do you make the filter robust? _(Tests defensive filtering with UPPER/LOWER to normalize campaign names.)_
- Would using WHERE ad_campaign = 'X' OR ad_campaign = 'Y' behave differently from WHERE ad_campaign IN ('X','Y')? _(Tests SQL equivalence knowledge; IN and chained ORs produce the same plan in most engines.)_
- If you needed to add a date range filter for impressions in the last 90 days, where would you place it relative to the existing predicates? _(Tests understanding of predicate ordering and readability, though the optimizer treats them equivalently.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/targeted_ad_campaigns)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.