Active 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
Problem
The advertising team is reviewing lifetime campaign performance for the quarterly deck, and only campaigns that served more than 15 impressions make the cut. For each of those, show the impressions served, the total revenue earned, and the click-through rate as a percentage rounded to one decimal place, from the highest click-through rate down to the lowest.
Which campaigns are earning their keep?
Tables
ad_impressions
Columns: impression_id INTEGER, user_id INTEGER, ad_campaign TEXT, impression_time TIMESTAMP, clicked BOOLEAN, 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
| ad_campaign | impressions | total_revenue | ctr |
|---|---|---|---|
| LOYALTY_PROGRAM | 84 | 215.35 | 84.5 |
| SUMMER_SALE_2024 | 21 | 16.7 | 33.3 |
| PRODUCT_LAUNCH_X | 16 | 5 | 25 |
| HOLIDAY_PROMO | 16 | 6.2 | 25 |
| BRAND_AWARENESS_Q1 | 17 | 3.6 | 17.6 |
Worked solution and explanation
What this is really testing
Under the campaign-performance costume, this is a grouped ratio with a volume floor: click-through rate is clicks divided by impressions per campaign, and only campaigns with enough impressions to trust the rate make the list. Everyone gets the GROUP BY and the COUNT. What separates candidates is quieter. The volume floor tests COUNT(*), a number that only exists after grouping, so it lives in HAVING, where a row-level WHERE cannot reach it. Drop it and a campaign with 2 impressions and 1 click reports 50 percent and jumps to the top of a list meant for real performers. The division also has to be decimal: with integer operands SUM(clicked) / COUNT(*) comes back 0 for every campaign, and even 100 * SUM(clicked) / COUNT(*) quietly truncates 84.5 to 84.
Building it up
Step 1: Collapse to one row per campaign
In production there are about 200 campaigns across 100M rows, so ad_campaign is the grain. Collapse the rows with GROUP BY ad_campaign; COUNT(*) gives impressions and SUM(revenue) gives total_revenue. About half the revenue values on the seed are NULL, and SUM skips them: those rows still count as impressions and add nothing to revenue, which is exactly the total the deck wants.
Step 2: Compute the rate in decimal arithmetic
Click-through rate is 100.0 * SUM(clicked) / COUNT(*), wrapped in ROUND(..., 1) for one decimal place. ROUND only formats the value it is handed, so the float literal still has to sit inside the division.
Step 3: Filter on the count, then order
HAVING COUNT(*) > 15 runs after grouping, on each campaign's count. On the seed it removes the 2 campaigns sitting at exactly 15 impressions, and it guarantees every surviving denominator is at least 16, so there is no division by zero to guard. Then ORDER BY ctr DESC puts the best converters first. 'HOLIDAY_PROMO' and 'PRODUCT_LAUNCH_X' both land on 25.0 percent, and the deck accepts them in either order, so no tiebreaker is added.
The solution
SELECT
ad_campaign,
COUNT(*) AS impressions,
SUM(revenue) AS total_revenue,
ROUND(100.0 * SUM(clicked) / COUNT(*), 1) AS ctr
FROM ad_impressions
GROUP BY ad_campaign
HAVING COUNT(*) > 15
ORDER BY ctr DESCCommon follow-up questions
- What does `HAVING COUNT(*) > 15` actually buy you here, and how would you choose the threshold if tiny campaigns were producing noisy 100 percent or 0 percent rates? (Tests whether the candidate understands that the threshold is on the aggregate and how it interacts with rate stability.)
- If you needed click-through rate broken down by both campaign and geographic region, how would you restructure the grouping and what happens to the row count? (Tests whether the candidate can add a second grouping dimension and reason about grain.)
- About half of `revenue` values are NULL. Walk through how `SUM(revenue)` treats them, and what changes if you wanted those rows excluded from the impression count too. (Tests understanding of NULL handling in aggregation versus filtering.)
- The table is partitioned on `impression_time` but this query scans all of it. What one change limits the scan to a single quarter, and why does it help? (Tests whether the candidate can reason about partition pruning on a large table.)