Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL for Campaign Performance
00:00
5 left

SQL for Campaign Performance

MediumSQL · PostgreSQL

Problem

Write a SQL query to find the top-performing marketing campaigns based on customer acquisition cost and lifetime value. Return the three campaigns with at least two acquired customers, ranked by lifetime value divided by customer acquisition cost. Use lifetime value as the average non-null customer lifetime value.

Output

  1. One row per qualifying campaign
  2. Columns: campaign_id, campaign_name, total_spend, acquired_customers, customer_acquisition_cost, lifetime_value, ltv_to_cac_ratio
  3. Order by ltv_to_cac_ratio descending, then customer_acquisition_cost ascending, then campaign_id ascending

Schema

campaigns
ColumnTypeDescription
campaign_idPKINTUnique campaign identifier
campaign_nameVARCHAR(100)Marketing campaign name
channelVARCHAR(50)Primary acquisition channel
campaign_spend
ColumnTypeDescription
spend_idPKINTUnique spend record identifier
campaign_idINTReferenced campaign
spend_dateDATEDate of the spend
spend_amountNUMERIC(12,2)Amount spent on the campaign
customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
campaign_idINTCampaign that acquired the customer
acquired_atDATECustomer acquisition date
lifetime_valueNUMERIC(12,2)Customer lifetime value
Tablescampaignscampaign_spendcustomers
Interviewer

Your question is SQL for Campaign Performance. Start with the requirements and the three tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.