Attribute Orders to the Last Ad Click Within Seven Days
Last updated: November 29, 2026
Quick Overview
Given ad click and order tables, attribute each order to the customer's most recent ad click in the 7 days before the order (last-touch attribution), and report orders and revenue per campaign, including unattributed orders.
Amazon
November 29, 20260
0
0 solved
Given ad click and order tables, attribute each order to the customer's most recent ad click in the 7 days before the order (last-touch attribution), and report orders and revenue per campaign, including unattributed orders.
Amazon Ads reports sponsored product performance using attribution windows, so this question is common for Data Scientists and BI engineers working near advertising. The interviewer checks range joins, choosing one row per order with window functions, and keeping orders with no matching click.
What the Interviewer Expects
- LEFT JOIN orders to clicks on customer and a time-window condition
- Click must precede the order
- ROW_NUMBER to keep only the latest click per order
- Preserve unattributed orders
- Deterministic tie-breaking
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you implement linear (multi-touch) attribution?
- How would you restrict attribution to clicks on the same product that was ordered?
- How would you make this efficient for billions of clicks?
- How do you handle multiple orders after one click?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
ad_clicks(click_id, customer_id, campaign_id, click_ts)orders(order_id, customer_id, order_ts, amount)- Window: click_ts in
[order_ts - 7 days, order_ts]. A click after the order never cou...
Approach
- LEFT JOIN each order to every eligible click of the same customer in the window.
- Rank candidate clicks per order by
click_ts DESC, click_id DESC. - Keep rank 1; orders with no click have NULL...