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)
| 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.