← All SQL interview questions

Checkout funnel with ordered steps

HardCTEsRazorpayFlipkart

A growth team measures a four-step purchase funnel: view_product, add_to_cart, begin_checkout, payment_success. A user only counts at a step if they completed every earlier step first, in order: each step must happen strictly after the user's first qualifying earlier step. Events out of order (for example adding to cart before ever viewing a product, or paying before starting checkout) do not count.

From funnel_events, return step_no (1 to 4), step_name, users_reached, pct_of_previous (users at this step as a % of the previous step, 1 decimal; NULL for step 1) and pct_of_first (as a % of step 1, 1 decimal). Order by step_no.

Sample data

Table: funnel_events

31 row(s) · showing first 8

event_idINTuser_idINTstepTEXTevent_tsTIMESTAMP
11view_product2026-10-20
21add_to_cart2026-10-20
31begin_checkout2026-10-20
41payment_success2026-10-20
52view_product2026-10-20
62add_to_cart2026-10-20
72begin_checkout2026-10-20
83view_product2026-10-20