cswork / libraryOpen practice workspace

Coding questions · Meta

Compute Daily Revenue by Creation Source

This question evaluates proficiency in data manipulation and analytical SQL/Python skills, including aggregation, date truncation, joins, window functions, and period-over-period comparisons for revenue metrics.

active_ads

date | ad_id | advertiser_id | creation_source | revenue

2023-09-01 | 1001 | 17 | mobile_app | 150.00

2023-09-01 | 1002 | 18 | api | 75.00

2023-09-02 | 1003 | 17 | web | 200.00

advertiser_info

advertiser_id | advertiser_name | advertiser_country

17 | Alpha Corp | US

18 | Beta Ltd | UK

19 | Gamma GmbH | DE

Scenario

Advertising revenue reporting for a multi-source ads platform using relational data in tables active_ads and advertiser_info.

Question

Q1. Write a query to compute daily total revenue grouped by creation_source. Q2. Write a query to list the countries of the ten advertisers with the fewest active ads (break ties arbitrarily). Q3. For each creation_source, compute the proportion of advertisers whose spend in the current year exceeds their spend in the previous year by at least 1,000 USD.

Hints

Use date truncation, aggregation, LEFT JOINs, window functions or CTEs for yearly spend, then COUNTIF / COUNT ratio.

Daily Total Revenue by Creation Source

You are given an ads revenue table active_ads. Write a SQL query to compute the daily total revenue grouped by creation_source.

Return one row per (ad_date, creation_source) with the summed revenue for that date and source.

Table active_ads

columntypeconstraints
ad_dateDATENOT NULL
ad_idINTPRIMARY KEY
advertiser_idINTNOT NULL
creation_sourceVARCHAR(20)NOT NULL
revenueDECIMAL(10,2)NOT NULL

Sample data:

ad_datead_idadvertiser_idcreation_sourcerevenue
2023-09-01100117mobile_app150
2023-09-01100218api75
2023-09-02100317web200
2023-09-02100419mobile_app50
2023-09-03100520api120
2023-09-03100618web80
2023-09-03100721mobile_app60
2023-09-04100822api40
2023-09-04100922web30
2023-09-05101023web20
2023-09-05101123web25
2023-09-06101223api10
2023-09-06101324mobile_app70
2024-02-10200117api300
2024-05-15200217api200
2024-03-01200318api1000
2024-06-20200417web200
2024-04-05200518web700
2024-07-07200619mobile_app600
2024-08-08200720mobile_app100
2025-01-10300117api1000
2025-02-11300217api800
2025-03-12300318api1500
2025-04-13300422api900
2025-01-05300517web1500
2025-02-06300618web400
2025-05-07300723web1200
2025-03-03300819mobile_app1300
2025-04-04300920mobile_app1200
2025-05-05301021mobile_app500

Expected output

Each row contains a date, a creation_source, and the summed revenue for that date/source.

ad_datecreation_sourcetotal_revenue
2023-09-01api75
2023-09-01mobile_app150
2023-09-02mobile_app50
2023-09-02web200
2023-09-03api120
2023-09-03mobile_app60
2023-09-03web80
2023-09-04api40
2023-09-04web30
2023-09-05web45
2023-09-06api10
2023-09-06mobile_app70
2024-02-10api300
2024-03-01api1000
2024-04-05web700
2024-05-15api200
2024-06-20web200
2024-07-07mobile_app600
2024-08-08mobile_app100
2025-01-05web1500
2025-01-10api1000
2025-02-06web400
2025-02-11api800
2025-03-03mobile_app1300
2025-03-12api1500
2025-04-04mobile_app1200
2025-04-13api900
2025-05-05mobile_app500
2025-05-07web1200

Hints

  1. Group by both the date and creation_source.
  2. Use SUM(revenue) for totals.

Countries of the 10 Advertisers with the Fewest Active Ads

You are given tables active_ads and advertiser_info.

Write a SQL query to list the countries of the ten advertisers with the fewest active ads. Count active ads as the number of distinct ad_id values in active_ads per advertiser. Include advertisers with zero active ads. Break ties arbitrarily.

Return (advertiser_id, advertiser_country, active_ad_count) for the 10 advertisers with the smallest counts.

Table advertiser_info

columntypeconstraints
advertiser_idINTPRIMARY KEY
advertiser_nameVARCHAR(100)NOT NULL
advertiser_countryVARCHAR(2)NOT NULL

Sample data:

advertiser_countryadvertiser_idadvertiser_name
US17Alpha Corp
UK18Beta Ltd
DE19Gamma GmbH
FR20Delta SAS
JP21Epsilon KK
AU22Zeta Pty
IT23Eta SpA
NL24Theta BV
SE25Iota AB
ES26Kappa SL
BR27Lambda SA
CA28Mu Co

Table active_ads

columntypeconstraints
ad_dateDATENOT NULL
ad_idINTPRIMARY KEY
advertiser_idINTNOT NULL
creation_sourceVARCHAR(20)NOT NULL
revenueDECIMAL(10,2)NOT NULL

Sample data:

ad_datead_idadvertiser_idcreation_sourcerevenue
2023-09-01100117mobile_app150
2023-09-01100218api75
2023-09-02100317web200
2023-09-02100419mobile_app50
2023-09-03100520api120
2023-09-03100618web80
2023-09-03100721mobile_app60
2023-09-04100822api40
2023-09-04100922web30
2023-09-05101023web20
2023-09-05101123web25
2023-09-06101223api10
2023-09-06101324mobile_app70
2024-02-10200117api300
2024-05-15200217api200
2024-03-01200318api1000
2024-06-20200417web200
2024-04-05200518web700
2024-07-07200619mobile_app600
2024-08-08200720mobile_app100
2025-01-10300117api1000
2025-02-11300217api800
2025-03-12300318api1500
2025-04-13300422api900
2025-01-05300517web1500
2025-02-06300618web400
2025-05-07300723web1200
2025-03-03300819mobile_app1300
2025-04-04300920mobile_app1200
2025-05-05301021mobile_app500

Expected output

10 rows: the advertisers with the smallest distinct ad_id counts (including zero), showing their country and count.

active_ad_countadvertiser_countryadvertiser_id
0SE25
0ES26
0BR27
0CA28
1NL24
2JP21
3DE19
3FR20
3AU22
4IT23

Hints

  1. Use a LEFT JOIN to keep advertisers with 0 ads.
  2. COUNT(DISTINCT ad_id) counts ads per advertiser.

YoY Advertiser Growth Proportion by Creation Source (2025 vs 2024)

Assume the current date is 2025-06-01. Using active_ads, for each creation_source, compute the proportion of advertisers whose spend (sum of revenue) in 2025 exceeds their spend in 2024 by at least 1,000 USD.

Definitions:

  • 2025 spend = sum(revenue) for rows with ad_date between 2025-01-01 and 2025-12-31.
  • 2024 spend = sum(revenue) for rows with ad_date between 2024-01-01 and 2024-12-31.
  • Consider an advertiser part of a creation_source group if they have any spend in 2024 or 2025 for that creation_source.
  • Treat missing spend in a year as 0.

Return one row per creation_source with:

  • total_advertisers
  • advertisers_meeting_threshold
  • proportion_meeting_threshold (advertisers_meeting_threshold / total_advertisers as a decimal).

Table active_ads

columntypeconstraints
ad_dateDATENOT NULL
ad_idINTPRIMARY KEY
advertiser_idINTNOT NULL
creation_sourceVARCHAR(20)NOT NULL
revenueDECIMAL(10,2)NOT NULL

Sample data:

ad_datead_idadvertiser_idcreation_sourcerevenue
2023-09-01100117mobile_app150
2023-09-01100218api75
2023-09-02100317web200
2023-09-02100419mobile_app50
2023-09-03100520api120
2023-09-03100618web80
2023-09-03100721mobile_app60
2023-09-04100822api40
2023-09-04100922web30
2023-09-05101023web20
2023-09-05101123web25
2023-09-06101223api10
2023-09-06101324mobile_app70
2024-02-10200117api300
2024-05-15200217api200
2024-03-01200318api1000
2024-06-20200417web200
2024-04-05200518web700
2024-07-07200619mobile_app600
2024-08-08200720mobile_app100
2025-01-10300117api1000
2025-02-11300217api800
2025-03-12300318api1500
2025-04-13300422api900
2025-01-05300517web1500
2025-02-06300618web400
2025-05-07300723web1200
2025-03-03300819mobile_app1300
2025-04-04300920mobile_app1200
2025-05-05301021mobile_app500

Expected output

One row per creation_source showing how many advertisers exist for that source (in 2024 or 2025), how many increased by at least 1000, and the resulting proportion.

advertisers_meeting_thresholdcreation_sourceproportion_meeting_thresholdtotal_advertisers
1api0.33333
1mobile_app0.33333
2web0.66673

Hints

  1. Compute yearly spend per (creation_source, advertiser_id) using conditional aggregation.
  2. Treat missing year spend as 0 by using ELSE 0 in SUM(CASE...).
  3. Proportion = qualifying_count / total_count; use CAST to avoid integer division.