Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Add-to-Cart to Checkout Conversion
00:00
5 left

Add-to-Cart to Checkout Conversion

HardSQL · PostgreSQL

Problem

Amazon.com wants to compare shopping-funnel performance across marketplaces. Write a PostgreSQL query that calculates the conversion rate from add-to-cart to checkout for sessions recorded during January 1 through January 2, 2025.

A session counts in the denominator when it contains an add_to_cart event. It counts as converted only when the same session contains a checkout event after the add-to-cart event and within 24 hours.

Requirements

  1. Aggregate events at the user and session level before calculating conversion.
  2. Report add-to-cart sessions, converted sessions, and conversion rate percentage by marketplace.
  3. Include marketplaces with customers but no qualifying add-to-cart sessions, returning a NULL rate when the denominator is zero.
  4. Exclude events for customer IDs that are not present in customers and avoid division by zero.

Output details

  • Return columns in this order: marketplace, add_to_cart_sessions, checkout_sessions, conversion_rate_pct.
  • Sort by marketplace ascending, placing NULL values first.
  • Treat the event range as inclusive at 2025-01-01 00:00:00 and exclusive at 2025-01-03 00:00:00.
  • Use each session's earliest add_to_cart timestamp and earliest checkout timestamp.
  • Require checkout strictly after add_to_cart and no later than 24 hours afterward.
  • Round conversion_rate_pct to 2 decimal places.

Schema

customers
ColumnTypeDescription
customer_idPKINTAmazon customer identifier
marketplaceVARCHAR(20)Amazon marketplace code
funnel_events
ColumnTypeDescription
event_idPKINTUnique funnel event identifier
customer_idINTCustomer associated with the event
session_idVARCHAR(20)Shopping session identifier
event_typeVARCHAR(20)Funnel event name
event_tsTIMESTAMPTimestamp when the event occurred
Tablescustomersfunnel_events
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.