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
Data Manipulation (SQL/Python)
Data Scientist
Amazon
November 29, 2026
Data Scientist
Onsite
Data Manipulation (SQL/Python)
Hard

0

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
Last-touch attribution
Range joins
ROW_NUMBER
LEFT JOIN
Advertising metrics
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. 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 Problems
Sample 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
  1. LEFT JOIN each order to every eligible click of the same customer in the window.
  2. Rank candidate clicks per order by click_ts DESC, click_id DESC.
  3. Keep rank 1; orders with no click have NULL...

Submit Your Answer
Markdown supported