Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Joins and Aggregations for Performance
00:00
5 left

SQL Joins and Aggregations for Performance

MediumSQL · PostgreSQL

Problem

Can you write a SQL query using joins, subqueries, and group by to analyze marketing database performance?

Use the provided marketing tables for the January 2025 reporting period. Include active campaigns whose dates overlap January 2025, including campaigns with no matching activity.

Output

  1. One row per qualifying campaign.
  2. Return campaign_id, campaign_name, channel, total_spend, impressions, clicks, conversions, revenue, ctr_percent, conversion_rate_percent, roas, channel_avg_spend, and roas_rank.
  3. Sort by channel, roas_rank, and campaign_id.

Schema

campaigns
ColumnTypeDescription
campaign_idPKINTUnique campaign identifier
campaign_nameVARCHAR(100)Campaign display name
channelVARCHAR(50)Marketing channel
start_dateDATECampaign start date
end_dateDATECampaign end date
budgetDECIMAL(12,2)Allocated campaign budget
statusVARCHAR(20)Campaign lifecycle status
campaign_metrics
ColumnTypeDescription
metric_idPKINTUnique metric record identifier
campaign_idINTReferenced campaign
metric_dateDATEDate of measured activity
impressionsINTAd impressions
clicksINTAd clicks
spendDECIMAL(12,2)Spend recorded for the metric date
attributed_conversions
ColumnTypeDescription
conversion_idPKINTUnique conversion record identifier
campaign_idINTCampaign credited with the conversions
conversion_dateDATEDate of attributed conversions
conversionsINTNumber of conversions
revenueDECIMAL(12,2)Revenue attributed to conversions
Tablescampaignscampaign_metricsattributed_conversions
Interviewer

Your question is SQL Joins and Aggregations for 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.