← All SQL interview questions

Campaign spend efficiency with a CTE

MediumCTEsSwiggyZoomInfo

Performance marketing wants cost per signup by campaign, but only for campaigns that actually spent money.

Using a CTE, aggregate campaign_spend to one row per campaign_name (total spend and total signups), then return campaign_name, total_spend, total_signups and cost_per_signup (total spend divided by total signups, rounded to 2 decimals) for campaigns whose total spend is above 50000. Skip campaigns with zero signups.

Sample data

Table: campaign_spend

15 row(s) · showing first 8

row_idINTcampaign_nameTEXTspend_dateDATEspendNUMERIC(10,2)signupsINT
1Search Brand2026-01-0522000.00140
2Search Brand2026-01-1226500.00165
3Search Brand2026-01-1918000.0096
4Social Retarget2026-01-0612000.0045
5Social Retarget2026-01-1315500.0061
6Social Retarget2026-01-2014200.0052
7YouTube Preroll2026-01-0731000.0088
8YouTube Preroll2026-01-1428500.0074

Practising alone only goes so far — our live cohorts pair SQL depth with dashboards, agentic analytics projects and mock interviews.

Explore the program