Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
SQL for Weekly Lead KPIs
00:00
5 left

SQL for Weekly Lead KPIs

MediumSQL · PostgreSQL

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
Interviewer

Your question is SQL for Weekly Lead KPIs. 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
Sign up free to run your codeLog inLn 1
Run your query to see results here.