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
Second Purchase Within 24 Hours
00:00
5 left

Second Purchase Within 24 Hours

MediumSQL · PostgreSQL

Problem

On Meta Marketplace, the analytics team wants to identify users whose second completed transaction happened within 24 hours of their first completed transaction.

Write a SQL query to return the users who meet this condition.

Requirements

  1. Consider only rows where status = 'completed'.
  2. For each user, identify their first and second completed transactions by transaction_time.
  3. Return only users whose second completed transaction occurred within 24 hours of their first.
  4. Output user_id, first_transaction_time, second_transaction_time, and the time difference as hours_between.
  5. Order the final result by user_id.

Output details

  • Break equal transaction_time values by transaction_id ascending.
  • Include transactions exactly 24 hours apart.
  • Round hours_between to two decimal places.
  • Order by user_id ascending.

Schema

marketplace_transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
user_idINTMeta user identifier
transaction_timeTIMESTAMPTimestamp when the transaction occurred
amountDECIMAL(10,2)Transaction amount
statusVARCHAR(20)Transaction status such as completed, failed, pending, or refunded
payment_methodVARCHAR(30)Payment method used for the transaction
Tablesmarketplace_transactions
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.