Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Join and Aggregate User Behavior

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

Your question is Join and Aggregate User Behavior. 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

Dentsu International's Merkury audience activation data includes users, campaigns, and behavioral events. Write a PostgreSQL query that summarizes campaign performance during January 2025.

Requirements

  1. Use campaigns as the reporting base so campaigns with no in-window activity remain in the output.
  2. Count distinct engaged users and calculate impressions, clicks, conversions, and total conversion value.
  3. Calculate click-through rate as clicks divided by impressions and conversion rate as conversions divided by clicks. Return NULL when the denominator is zero.
  4. Order results by campaign_id ascending.

Schema

users
ColumnTypeDescription
user_idPKINTUnique audience user identifier
user_nameVARCHAR(100)User display name
segmentVARCHAR(50)Audience segment assigned to the user
campaigns
ColumnTypeDescription
campaign_idPKINTUnique campaign identifier
campaign_nameVARCHAR(150)Campaign name
channelVARCHAR(50)Marketing activation channel
user_events
ColumnTypeDescription
event_idPKINTUnique behavioral event identifier
user_idINTUser associated with the event
campaign_idINTCampaign that generated the event
event_timeTIMESTAMPTimestamp when the event occurred
event_typeVARCHAR(30)Behavioral event type
event_valueDECIMAL(12,2)Attributed monetary value, generally populated for conversions
Tablesuserscampaignsuser_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results