Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
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
Experiences
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
Build my plan
SQL Top Products Within 24 Hours
00:00
5 left

SQL Top Products Within 24 Hours

MediumSQL · PostgreSQL

Problem

Salesforce Marketing Cloud wants to identify which products attract users who convert shortly afterward. Write a PostgreSQL query that finds the top three products viewed by users who made a purchase within 24 hours after the view.

Requirements

  1. Consider only interaction rows where interaction_type = 'view'.
  2. A view qualifies when the same user has a purchase at or after the view and no more than 24 hours later. A view may qualify against multiple purchases, but count that view only once.
  3. Aggregate by product and return the number of distinct qualifying users and qualifying views.
  4. Rank products by distinct qualifying users descending, then qualifying views descending, then product_id ascending as a deterministic tie-breaker.
  5. Return only the top three catalog products.

Schema

interactions
ColumnTypeDescription
event_idPKINTUnique interaction event identifier
user_idINTUser who generated the interaction
product_idINTViewed product identifier
interaction_typeVARCHAR(30)Type of interaction, such as view or add_to_cart
occurred_atTIMESTAMPTZTimestamp when the interaction occurred
purchases
ColumnTypeDescription
purchase_idPKINTUnique purchase identifier
user_idINTUser who made the purchase
product_idINTPurchased product identifier
purchased_atTIMESTAMPTZTimestamp when the purchase occurred
products
ColumnTypeDescription
product_idPKINTUnique Salesforce product identifier
product_nameVARCHAR(120)Product name in the catalog
Tablesinteractionspurchasesproducts
Interviewer

Your question is SQL Top Products Within 24 Hours. Start with the requirements and the three tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

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.