First-touch attribution: each user's first marketing channel on or before their first transaction

Quick Overview

Write a SQL query that finds, for every user who has made a transaction, the first marketing channel they interacted with on or before their first transaction date. It tests aggregation, window functions with a deterministic tiebreak, inclusive date filtering, and excluding users who lack a qualifying touch.

First-touch attribution: each user's first marketing channel on or before their first transaction

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

For each user, find the first marketing channel they interacted with on or before their first transaction date. Return `user_id`, `first_transaction_date`, `first_touch_channel` and `first_interaction_date`, ordered by `user_id` ascending. Include only users who have at least one transaction and at least one interaction on or before their first transaction date. Order a user's interactions by `interaction_date`, and break ties with the lowest `interaction_id`. ```hint Split the work Work out each user's first transaction date on its own before you look at the interactions. What is left is choosing one interaction row per user. ``` ### Input Table(s) `marketing_interactions` (primary key `interaction_id`) | Column | Type | Meaning | |---|---|---| | interaction_id | INT | Unique id of the interaction (primary key) | | user_id | INT | User who had the interaction, NOT NULL | | channel | VARCHAR(50) | Marketing channel of the interaction, NOT NULL | | interaction_date | DATE | Date of the interaction, NOT NULL | `transactions` (primary key `transaction_id`) | Column | Type | Meaning | |---|---|---| | transaction_id | INT | Unique id of the transaction (primary key) | | user_id | INT | User who made the transaction, NOT NULL | | amount | DECIMAL(10,2) | Transaction amount, NOT NULL (not needed for this task) | | transaction_date | DATE | Date of the transaction, NOT NULL | ### Output Contract One row per qualifying user, with these columns in this order: | Column | Type | Meaning | |---|---|---| | user_id | INT | The user | | first_transaction_date | DATE | The user's earliest `transaction_date` | | first_touch_channel | VARCHAR(50) | `channel` of the chosen interaction | | first_interaction_date | DATE | `interaction_date` of the chosen interaction | Sort by `user_id` ascending. Each user appears at most once, so this order is unique. No rounding is involved. ### Example `marketing_interactions` | interaction_id | user_id | channel | interaction_date | |---|---|---|---| | 1 | 1 | email | 2024-01-03 | | 2 | 1 | paid_search | 2024-01-01 | | 3 | 2 | email | 2024-01-05 | | 4 | 2 | social | 2024-01-05 | | 5 | 3 | display | 2024-02-10 | | 6 | 4 | referral | 2024-01-20 | `transactions` | transaction_id | user_id | amount | transaction_date | |---|---|---|---| | 101 | 1 | 50.00 | 2024-01-10 | | 102 | 1 | 20.00 | 2024-01-02 | | 103 | 2 | 75.00 | 2024-01-05 | | 104 | 3 | 30.00 | 2024-02-01 | | 105 | 5 | 10.00 | 2024-03-01 | Expected result: | user_id | first_transaction_date | first_touch_channel | first_interaction_date | |---|---|---|---| | 1 | 2024-01-02 | paid_search | 2024-01-01 | | 2 | 2024-01-05 | email | 2024-01-05 | User 1 first paid on 2024-01-02, and only the `paid_search` touch on 2024-01-01 comes on or before that date; the `email` touch on 2024-01-03 is after it. User 2 has two touches on the day of the first transaction, and `email` wins because its `interaction_id` (3) is lower. User 3's only touch comes after their first transaction, user 4 never transacted, and user 5 has no interactions, so none of them appear. ### Constraints and Clarifications - Each table has between 1 and 10,000 rows. - `user_id` is a positive integer, and every column is NOT NULL, so no NULL handling is needed. - "On or before" is inclusive: an interaction dated the same day as the first transaction counts. - Only the first transaction date matters. An interaction between a user's first and second transactions does not qualify. - A user can have several transactions on their first transaction date. They still produce one output row. - Users who appear in only one of the two tables are left out, and so are users whose interactions all come after their first transaction. - When several interactions share the earliest qualifying date, the one with the lowest `interaction_id` is the first touch.

Overview: Write a SQL query that finds, for every user who has made a transaction, the first marketing channel they interacted with on or before their first transaction date. It tests aggregation, window functions with a deterministic tiebreak, inclusive date filtering, and excluding users who lack a qualifying touch.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

For each user, find the first marketing channel they interacted with on or before their first transaction date. Return user_id, first_transaction_date, first_touch_channel and first_interaction_date, ordered by user_id ascending.

Include only users who have at least one transaction and at least one interaction on or before their first transaction date. Order a user's interactions by interaction_date, and break ties with the lowest interaction_id.

Input Table(s)

marketing_interactions (primary key interaction_id)

ColumnTypeMeaning
interaction_idINTUnique id of the interaction (primary key)
user_idINTUser who had the interaction, NOT NULL
channelVARCHAR(50)Marketing channel of the interaction, NOT NULL
interaction_dateDATEDate of the interaction, NOT NULL

transactions (primary key transaction_id)

ColumnTypeMeaning
transaction_idINTUnique id of the transaction (primary key)
user_idINTUser who made the transaction, NOT NULL
amountDECIMAL(10,2)Transaction amount, NOT NULL (not needed for this task)
transaction_dateDATEDate of the transaction, NOT NULL

Output Contract

One row per qualifying user, with these columns in this order:

ColumnTypeMeaning
user_idINTThe user
first_transaction_dateDATEThe user's earliest transaction_date
first_touch_channelVARCHAR(50)channel of the chosen interaction
first_interaction_dateDATEinteraction_date of the chosen interaction

Sort by user_id ascending. Each user appears at most once, so this order is unique. No rounding is involved.

Example

marketing_interactions

interaction_iduser_idchannelinteraction_date
11email2024-01-03
21paid_search2024-01-01
32email2024-01-05
42social2024-01-05
53display2024-02-10
64referral2024-01-20

transactions

transaction_iduser_idamounttransaction_date
101150.002024-01-10
102120.002024-01-02
103275.002024-01-05
104330.002024-02-01
105510.002024-03-01

Expected result:

user_idfirst_transaction_datefirst_touch_channelfirst_interaction_date
12024-01-02paid_search2024-01-01
22024-01-05email2024-01-05

User 1 first paid on 2024-01-02, and only the paid_search touch on 2024-01-01 comes on or before that date; the email touch on 2024-01-03 is after it. User 2 has two touches on the day of the first transaction, and email wins because its interaction_id (3) is lower. User 3's only touch comes after their first transaction, user 4 never transacted, and user 5 has no interactions, so none of them appear.

Constraints and Clarifications

  • Each table has between 1 and 10,000 rows.
  • user_id is a positive integer, and every column is NOT NULL, so no NULL handling is needed.
  • "On or before" is inclusive: an interaction dated the same day as the first transaction counts.
  • Only the first transaction date matters. An interaction between a user's first and second transactions does not qualify.
  • A user can have several transactions on their first transaction date. They still produce one output row.
  • Users who appear in only one of the two tables are left out, and so are users whose interactions all come after their first transaction.
  • When several interactions share the earliest qualifying date, the one with the lowest interaction_id is the first touch.