Skip to content

Average first purchase value by campaign, organic included

MediumFull bankRanking and top NJoinsNULL handling15 min2 hidden datasets

The task

The growth team wants to compare how much users spend on their first order, by where they came from. A user's first purchase is their earliest purchased_at in purchases. If two purchases share the same purchased_at, the one with the lower purchase_id is first. Users with no purchase are ignored.

Group users by the campaign credited in signups.campaign_id. Sign-ups with a NULL campaign_id are organic and are shown under the label Organic; every other group uses the campaign_name.

Return campaign (the name or Organic), buyers (users whose first purchase falls in the group) and avg_first_purchase (the average amount of those first purchases, rounded to 2 decimal places). Sort by buyers descending, then campaign ascending.

Expected output

campaignbuyersavg_first_purchase
  • Sorted by buyers descending, then campaign 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