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 Date
00:00
5 left

Second Purchase Date

MediumSQL · PostgreSQL

Problem

Amazon’s customer analytics team wants to identify when each customer made their second purchase. Write a PostgreSQL query that returns every customer, including customers who have not yet made two purchases.

For this exercise, multiple purchases by the same customer on the same calendar date count as one purchase date.

Requirements

  1. Rank each customer’s distinct purchase dates chronologically using a window function.
  2. Return the customer ID, customer name, and second purchase date.
  3. Include customers with fewer than two purchase dates, using NULL when no second date exists.
  4. Exclude purchases with a missing customer ID or purchase date.

Output details

  • Return columns named customer_id, customer_name, and second_purchase_date, in that order.
  • Order rows by customer_id ascending.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique Amazon customer identifier
customer_nameVARCHAR(100)Customer’s name
purchases
ColumnTypeDescription
purchase_idPKINTUnique purchase identifier
customer_idINTCustomer who made the purchase
purchase_dateDATECalendar date of the purchase
purchase_channelVARCHAR(30)Amazon purchase channel
Tablescustomerspurchases
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.