Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL for Weekly Lead KPIs

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL for Weekly Lead KPIs. Start with the requirements and the three tables on the right.

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.

Problem

Stanley Martin Homes marketing analysts need a weekly view of lead performance across acquisition channels used by the Stanley Martin Homes website and CRM. Write a PostgreSQL query for the period from 2025-01-06 through 2025-02-02.

Requirements

  1. Return one row for every marketing channel and calendar week, including channel-week combinations with no leads.
  2. Calculate lead volume, conversion rate as a percentage of leads where converted = TRUE, and average cost per lead as weekly spend divided by lead volume.
  3. Treat missing lead counts as zero and avoid division-by-zero errors. Treat nullable spend as zero when aggregating.
  4. Order results by channel name and week start.

Schema

marketing_channels
ColumnTypeDescription
channel_idPKINTPrimary key for a marketing channel
channel_nameVARCHAR(80)Marketing acquisition channel name
leads
ColumnTypeDescription
lead_idPKINTPrimary key for a lead
channel_idINTMarketing channel attributed to the lead
created_atDATEDate the lead entered the Stanley Martin Homes CRM
convertedBOOLEANWhether the lead converted
marketing_spend
ColumnTypeDescription
spend_idPKINTPrimary key for a spend record
channel_idINTMarketing channel receiving the spend
spend_dateDATEDate the spend was recorded
amountDECIMAL(10,2)Spend amount for the channel and date
Tablesmarketing_channelsleadsmarketing_spend
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results