Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Attribution First vs Last Touch
00:00
5 left

SQL Attribution First vs Last Touch

HardSQL · PostgreSQL

Problem

Write a SQL query to compare first-touch and last-touch attribution across marketing channels. Attribute each conversion to the earliest and latest marketing touch from the same customer within the preceding 30 days, using touch time and touch ID to break ties. Include channels with no attributed conversions.

Output

  1. One row per marketing channel with columns channel_name, first_touch_conversions, first_touch_revenue, last_touch_conversions, last_touch_revenue, and revenue_difference
  2. Include all channels, including zero-attribution channels
  3. Order by channel_name ascending

Schema

channels
ColumnTypeDescription
channel_idPKINTUnique marketing channel identifier
channel_nameVARCHAR(100)Marketing channel name
marketing_touchpoints
ColumnTypeDescription
touch_idPKINTUnique marketing touch identifier
customer_idINTCustomer associated with the touch
channel_idINTMarketing channel associated with the touch
touch_timestampTIMESTAMPTime the customer interacted with the channel
conversions
ColumnTypeDescription
conversion_idPKINTUnique conversion identifier
customer_idINTCustomer who converted
conversion_timestampTIMESTAMPTime the conversion occurred
revenueNUMERIC(10,2)Revenue generated by the conversion
Tableschannelsmarketing_touchpointsconversions
Interviewer

Your question is SQL Attribution First vs Last Touch. 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.