Skip to content

Purchase funnel inside a single session

HardFull bankFunnelsCTEs and subqueriesNULL handling25 min2 hidden datasets

The task

The product team wants a session-level funnel: how many browsing sessions turn into a purchase without the user leaving the session.

A session is the set of events that share the same session_id. Events with a NULL session_id (sent from the background) belong to no session and are ignored everywhere. The funnel steps must all happen in the same session, in order:

  • viewed: sessions with a view_item event
  • added_to_cart: sessions with an add_to_cart event later than that session's first view_item
  • purchased: sessions with a purchase event later than that first qualifying add_to_cart

"Later" means a later occurred_at; equal timestamps do not count. Use the first qualifying event at each step. session_id values are never shared between users.

Return one row per platform of the session's user, with platform, viewed, added_to_cart and purchased as session counts. A platform whose users have no sessions with a view still appears, with zeros. Sort by platform.

Expected output

platformviewedadded_to_cartpurchased
  • Sorted by platform ascending.
  • Checked on the sample and on 2 hidden datasets. How grading works

Tables Mobile shopping app · click a name to insert it

users360 rows in the sample

One row per user. Key: user_id.

intUnique id.
timestampSign-up time (UTC).
textTwo-letter country code.
textios, android or web.
textorganic, paid, referral or partner. NULL when unknown.
events7,616 rows in the sample

One row per tracked event. Key: event_id.

bigintUnique id.
intWho fired it.
textapp_open, search, view_item, add_to_cart, begin_checkout, purchase or share.
timestampWhen (UTC).
textSession the event belongs to. NULL for a few events sent from the background.
numeric(10,2)Order value for purchase events, NULL otherwise.
experiment_assignments349 rows in the sample

Which variant each user saw in each experiment. Key: user_id, experiment.

intThe user.
textnew_onboarding or one_tap_checkout.
textcontrol or treatment.
timestampWhen the user was assigned (UTC).

Related questions

Loading the editor and PostgreSQL