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

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 6 rows
ad_campaignimpressionstotal_revenuectr
LOYALTY_PROGRAM84215.3584.5
SUMMER_SALE_20242116.733.3
PRODUCT_LAUNCH_X16525
HOLIDAY_PROMO166.225
BRAND_AWARENESS_Q1173.617.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

Grouped ratio with a count threshold
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 DESC

Common follow-up questions