The Ad Ledger

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
General Assembly

Problem

Closing the books on the second quarter of 2024, a finance analyst needs the total ad revenue booked in that quarter alone, drawn from ad_impressions.

Annual ad revenue. On the record.

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 1 of 1 rows
total_revenue
8.25

Worked solution and explanation

What this problem is really testing

Under the finance costume, this is a SUM fenced to a single calendar quarter that sits in the INTERIOR of a table holding several years and all four quarters. Everyone writes SUM(revenue). What separates people is realizing that BOTH ends of the date range are load-bearing: the lower bound drops Q1 and the earlier years, and the upper bound drops Q3, Q4 and everything after. Set only the lower bound and you quietly fold later periods into this quarter, and the closed number ties out wrong.


Reading the shape of the data

ad_impressions is not one quarter of data. It spans several years and every quarter, and the period you are closing is Q2, which sits between Q1 below it and Q3 above it. So the target quarter is boxed in on both sides: earlier periods below it, later periods above it. That is exactly why a one-sided filter is not enough here.

Step 1: Fence the quarter on both ends

Use a half-open range: impression_time >= '2024-04-01' AND impression_time < '2024-07-01'. The lower bound excludes Q1 and every earlier year, the upper bound excludes Q3, Q4 and everything after. Half-open (< the start of July) avoids any off-by-one on the June 30 boundary, and unlike strftime(...) it stays sargable against an index on impression_time.

Step 2: Let SUM handle the nulls

SUM(revenue) over the filtered rows is the whole computation. SUM ignores NULL revenue for free, so the many impressions that never earned money neither error nor count as zero. Do not wrap it in COALESCE to force zeros: that changes nothing for the total and only muddies intent.

Step 3: Return one clean scalar

Alias the scalar as total_revenue so the single-row, single-column output reads for itself.


The solution

Both-sided date range for the quarterly total
SELECT SUM(revenue) AS total_revenue
FROM ad_impressions
WHERE impression_time >= '2024-04-01'
  AND impression_time < '2024-07-01'

Common follow-up questions