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
SQL: Purchase Rate by Country
00:00
5 left

SQL: Purchase Rate by Country

MediumSQL · PostgreSQL

Problem

Meta's Product Growth team is reviewing January 2021 conversion across countries for a purchase-oriented surface. The dataset below is a denormalized activity snapshot containing active-user events and purchase counts, similar to a product-growth extract used alongside AARRR Funnel reporting.

Write a PostgreSQL query to calculate the percentage of active users in each country who made at least one purchase during January 2021.

Requirements

  1. Include only active events from January 1 through January 31, 2021.
  2. Count each user once per country, even if the user has multiple activity rows.
  3. Return the country, distinct active-user count, distinct purchaser count, and purchase rate percentage, rounded to two decimals.

Output details

  • Name the output columns country, active_users, purchasers, and purchase_rate_pct, in that order.
  • Order rows by country ascending.
  • Treat NULL purchase_count values as zero, and count users as purchasers only when purchase_count exceeds zero.

Schema

meta_user_activity
ColumnTypeDescription
activity_idPKINTUnique activity-event identifier
user_idINTMeta user identifier
countryVARCHAR(50)User's country
activity_dateDATEDate of the activity event
is_activeBOOLEANWhether the event qualifies as active usage
purchase_countINTPurchases attributed to the user on that event
Tablesmeta_user_activity
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the one table 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.