Campaign Revenue Totals

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
Google

Problem

Every impression logged in ad_impressions, across all the years on record, counts toward the finance team's campaign revenue report. Show each campaign with its total revenue.

Every campaign has a price tag. Total them up.

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

Expected output

First 5 of 8 rows
ad_campaigntotal_revenue
BRAND_AWARENESS_Q13.6
FLASH_SALE_48H5.6
HOLIDAY_PROMO6.2
LOYALTY_PROGRAM215.35
NEW_USER_ACQU4.4

Worked solution and explanation

What this really is

This is a grain question in a finance costume. 'Total revenue' sounds like one number, and that reading is the trap. A candidate who sums the whole column, or puts ad_campaign next to SUM(revenue) without telling the engine where one campaign ends, hands finance a single figure. On this data that is about $260 of revenue stamped onto one arbitrary campaign name, and the other seven campaigns vanish from the report. The skill is fixing the output grain, one row per campaign, before you write the sum.

Getting there

Step 1: Fix the grain first

The report wants one row per campaign, so ad_campaign is the key every output row collapses to. Say that out loud before you touch the metric. It is the decision the whole query hangs on, and it is the one the trap skips.

Step 2: Sum revenue and let the gaps add nothing

SUM(revenue) totals each campaign. About half the impressions never clicked and carry a NULL revenue. SUM skips them, so those rows add nothing, and the campaign still appears with what its paying impressions earned. You need no filter and no default.

Step 3: Keep every year

Nothing in the ask narrows the window. The preview's 215.35 for the loyalty campaign only adds up across every year in impression_time. A 2026 filter here returns a smaller number, and it is wrong.

One row per campaign
SELECT ad_campaign, SUM(revenue) AS total_revenue
FROM ad_impressions
GROUP BY ad_campaign
No grouping
SELECT ad_campaign, SUM(revenue) FROM ad_impressions returns 1 row: the grand total beside whichever campaign name the engine read last.
Grouped by campaign
Adding GROUP BY ad_campaign returns 8 rows, each campaign beside only its own revenue.

Common follow-up questions