Skip to content

Campaigns cheaper per sign-up than their channel

HardFull bankCTEs and subqueriesRatios and percentagesJoins22 min2 hidden datasets

The task

Marketing wants to know which campaigns beat the rest of their channel on cost per sign-up.

  • A campaign's spend is the sum of ad_daily.spend for the campaign.
  • A campaign's sign-ups are the rows in signups whose campaign_id is that campaign. Organic sign-ups (NULL campaign_id) belong to no campaign.
  • A campaign's cost_per_signup is its spend divided by its sign-ups.
  • A channel's channel_cost_per_signup is the total spend of all its campaigns divided by the total sign-ups of all its campaigns. A campaign with spend and no sign-ups adds its spend to the channel total, and adds nothing to the sign-ups.

Return the campaigns that have at least one sign-up and whose cost_per_signup is strictly lower than their channel's. Give campaign_id, channel, cost_per_signup and channel_cost_per_signup, both rounded to 2 decimal places, sorted by campaign_id.

Expected output

campaign_idchannelcost_per_signupchannel_cost_per_signup
  • Sorted by campaign_id ascending.
  • Numbers rounded to 2 decimal places.
  • Checked on the sample and on 2 hidden datasets. How grading works

Tables Marketing team · click a name to insert it

campaigns10 rows in the sample

One row per campaign. Key: campaign_id.

intUnique id.
textName.
textsearch, social, video, email or affiliate.
dateFirst day live.
dateLast day live. NULL if still running.
numeric(10,2)Budget per day, USD.
ad_daily969 rows in the sample

One row per campaign per day it served ads. Days with no delivery have no row. Key: campaign_id, day.

intThe campaign.
dateThe day.
intTimes an ad was shown.
intClicks.
numeric(10,2)Money spent, USD.
signups2,108 rows in the sample

One row per new user. campaign_id is the campaign credited with the sign-up. Key: user_id.

intUnique id.
timestampWhen they signed up (UTC).
intCredited campaign. NULL for organic sign-ups.
textTwo-letter country code.
purchases2,294 rows in the sample

One row per paid order by a user. Key: purchase_id.

intUnique id.
intBuyer.
timestampWhen (UTC).
numeric(10,2)Order value, USD.

Related questions

Loading the editor and PostgreSQL