# Average Brand Campaign Revenue

> A quick benchmark on brand campaigns.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The marketing analytics team wants to benchmark brand campaign performance. What is the average revenue per impression across all ad impressions whose campaign name contains 'brand'?

## Worked solution and explanation

### Why this problem exists in real interviews

The core skill being tested is grouped aggregation over `ad_impressions`. Candidates must decide how `ad_campaign`, `impression_time`, `clicked` interact before choosing a join strategy or aggregation level.

---

### Break down the requirements

#### Step 1: Group by `ad_campaign`

`GROUP BY` at the correct grain produces one row per group.

#### Step 2: Compute `AVG(revenue)`

The AVG function computes the avg per group.

#### Step 3: Order by the metric

Sort by `avg_revenue` desc for readability.

---

### The solution

**Group-aggregate for average brand campaign revenue**

```sql
SELECT
    ad_campaign,
    AVG(revenue) AS avg_revenue
FROM ad_impressions
GROUP BY ad_campaign
ORDER BY avg_revenue DESC
```

> **Cost Analysis**
>
> The main table has 150M rows (29 GB). Partitioned on `impression_time`, so queries filtering on that column skip most partitions. The GROUP BY reduces the row count early, keeping downstream operations cheap.

> **Interviewers Watch For**
>
> Strong candidates state the correct `GROUP BY` grain before writing any SQL, showing they think about the output shape first.

> **Common Pitfall**
>
> Selecting a non-aggregated column without including it in `GROUP BY` is the most common error. Some engines reject it; others silently return arbitrary values.

---

## Common follow-up questions

- What happens to your results if `ad_campaign` in `ad_impressions` contains trailing whitespace or mixed casing? _(Tests awareness of text normalization issues that silently fragment GROUP BY results.)_
- Your GROUP BY aggregates `impression_id` from `ad_impressions`. If two groups have the same aggregate value, how is the output ordered, and is that deterministic? _(Tests awareness that ORDER BY on a non-unique value produces non-deterministic row order without a tiebreaker.)_
- `impression_id` in `ad_impressions` has ~150M distinct values. What index strategy keeps your query from doing a full table scan? _(Tests whether the candidate can design indexes for high-cardinality columns and understands selectivity.)_
- If the business definition of `ad_campaign` changed mid-quarter (e.g., a status value was renamed), how would you handle historical consistency? _(Tests awareness of slowly changing dimensions and backward-compatible query design.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/average_brand_campaign_revenue)
- [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.