cswork / libraryOpen practice workspace

Coding questions · Meta

Compute active ad revenue by creation source

This question evaluates proficiency in data manipulation and analytics, testing skills such as joining event and impression tables, aggregating time-series revenue, and counting distinct active ads broken down by creation source.

You work on an ads platform and need to report active ad revenue broken down by the ad’s creation source.

Tables

ads

  • ad_id BIGINT PK
  • advertiser_id BIGINT
  • creation_source VARCHAR Examples: 'SELF_SERVE', 'MANAGED', 'API'
  • created_at TIMESTAMP

ad_impressions

  • impression_id BIGINT PK
  • ad_id BIGINT FK → ads.ad_id
  • impression_time TIMESTAMP

ad_revenue_events

  • event_id BIGINT PK
  • ad_id BIGINT FK → ads.ad_id
  • event_time TIMESTAMP
  • revenue_usd DECIMAL(18,6) (Assume revenue is recorded at the time it is earned, e.g., per impression/click.)

Definitions / assumptions

  • Timezone: UTC.
  • A paid ad is active on a day if it has ≥ 1 impression that day.
  • Active ad revenue on a day = sum of revenue_usd from ad_revenue_events that occurred that day for ads that are active that same day.

Task

Write a SQL query to compute, for each calendar day in a given date range (e.g., :start_date to :end_date, inclusive):

  • report_date
  • creation_source
  • active_ads (count of distinct active ad_id)
  • active_ad_revenue_usd (sum of revenue for active ads)

Order results by report_date, then creation_source.

Compute active ad revenue by campaign creation source

You are given ad campaign metadata and daily performance stats.

Define active ad revenue as the total spend generated between 2025-05-01 and 2025-05-31 (inclusive) by campaigns whose current status is 'ACTIVE'.

Write a SQL query to return, for May 2025:

  • creation_source
  • total active ad revenue (active_ad_revenue)

Group by creation_source and order results by active_ad_revenue descending.

Table ad_campaigns

columntypeconstraints
campaign_idINTPRIMARY KEY, NOT NULL
advertiser_idINTNOT NULL
creation_sourceVARCHAR(20)NOT NULL
created_atDATENOT NULL
statusVARCHAR(10)NOT NULL

Sample data:

advertiser_idcampaign_idcreated_atcreation_sourcestatus
11012025-03-10SELF_SERVEACTIVE
21022025-04-02MANAGEDACTIVE
31032025-02-15APIPAUSED
41042025-05-01SELF_SERVEACTIVE
51052025-01-05MANAGEDINACTIVE

Table ad_daily_stats

columntypeconstraints
campaign_idINTNOT NULL
stat_dateDATENOT NULL
impressionsINTNOT NULL
clicksINTNOT NULL
spendDECIMAL(10,2)NOT NULL

Sample data:

campaign_idclicksimpressionsspendstat_date
10112010000502025-04-28
10130025000120.52025-05-03
10121018000802025-05-20
102500400002002025-05-10
102410350001502025-05-29
103600500003002025-05-12
10413012000602025-05-01
10416014000702025-06-01
105900800005002025-05-15

Expected output

One row per creation_source, summing spend in May 2025 for campaigns whose status is ACTIVE. Columns: creation_source, active_ad_revenue.

active_ad_revenuecreation_source
350MANAGED
260.5SELF_SERVE

Hints

  1. Filter stats to the May 2025 date range using BETWEEN.
  2. Join daily stats to campaigns, then filter to campaigns with status = 'ACTIVE' and group by creation_source.