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
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
| 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 | total_revenue |
|---|---|
| BRAND_AWARENESS_Q1 | 3.6 |
| FLASH_SALE_48H | 5.6 |
| HOLIDAY_PROMO | 6.2 |
| LOYALTY_PROGRAM | 215.35 |
| NEW_USER_ACQU | 4.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.
SELECT ad_campaign, SUM(revenue) AS total_revenue
FROM ad_impressions
GROUP BY ad_campaign- No grouping
SELECT ad_campaign, SUM(revenue) FROM ad_impressionsreturns 1 row: the grand total beside whichever campaign name the engine read last.- Grouped by campaign
- Adding
GROUP BY ad_campaignreturns 8 rows, each campaign beside only its own revenue.
Common follow-up questions
- What if campaigns with no impressions at all should still appear? (Tests whether the candidate reaches for a campaign dimension table and an outer join.)
- How would you add a row showing the grand total across all campaigns? (Tests `ROLLUP` or `UNION ALL` patterns for summary rows.)
- Should a campaign with no paying impressions show 0 or `NULL`? (`SUM` over only `NULL` values returns `NULL`; tests whether `COALESCE` is a deliberate choice.)