Compute an Ordered Search-to-Purchase Funnel Within Shopping Sessions

Last updated: November 29, 2026

Quick Overview

Given session-level clickstream events, compute how many sessions reached search, then a detail page view, then add to cart, then purchase, where each step must happen after the previous one in the same session.

Amazon
Data Manipulation (SQL/Python)
Data Scientist
Amazon
November 29, 2026
Data Scientist
Onsite
Data Manipulation (SQL/Python)
Hard

0

0

0 solved


Given session-level clickstream events, compute how many sessions reached search, then a detail page view, then add to cart, then purchase, where each step must happen after the previous one in the same session.

A classic Amazon retail analytics onsite query: the search and detail page funnel is how teams judge relevance and page changes. The interviewer is checking that you enforce step order with timestamps instead of just checking that each event exists, and that step-to-step rates use the right denominators.

What the Interviewer Expects
  • Enforce strict ordering: each step's timestamp must be after the matched previous step
  • Use the earliest qualifying event per step and justify why greedy matching is correct
  • Preserve sessions that drop off with LEFT JOINs so counts shrink step by step
  • Report both step conversion and overall conversion with safe division
  • State how sessions are defined and whether multiple purchases count once
Key Topics to Cover
funnel analysis
event ordering
LEFT JOIN chains
conditional counts
sessions
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 break the funnel down by device type or marketplace?
  • How would you require that the purchased ASIN is the same as the viewed ASIN?
  • How would you add a time limit, for example purchase within 30 minutes of search?
  • How would you write this with window functions instead of chained joins?
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

Table events(session_id, customer_id, event_ts, event_type) where event_type is one of search, detail_view, add_to_cart, purchase. Sessions are already defined upstream. Questions I would ...

Approach

An existence check (MAX(CASE WHEN type = 'purchase' ...)) overstates the funnel because it counts sessions where steps happened out of order. Instead I walk the steps:

  1. t1 = first search in the...

Submit Your Answer
Markdown supported