All problems

Time to First Purchase

mediumSQLJoinsGroupByDates

Growth at Whetcode Supply is trying to work out how long a new signup takes to become a buyer, and the first half of that calculation is the day each buyer finally placed an order for the first time. Later orders are irrelevant to the question — what matters is the moment the hesitation ended.

The list is read in the sequence those first purchases happened, so the earliest converter is at the top. Two customers converting on the same day would need a rule, so one is specified even though this data does not force it.

customers — one row per registered customer. city is the city given at signup and signup_date is the day they registered, written year-month-day.

id name city signup_date
1 Priya Nair Austin 2022-01-15
2 Tom Becker Berlin 2022-03-02
3 Sofia Rossi Milan 2022-05-19
4 Liam OConnor Dublin 2023-01-08
5 Wei Zhang Austin 2023-04-27

orders — one row per order. customer_id is the id of the customer who placed it, product_id the id of the item bought, quantity how many units of that item, and order_date the day it was placed. An order row covers one item only.

id customer_id product_id quantity order_date
1 1 1 2 2023-02-01
2 1 4 1 2023-02-01
3 2 2 1 2023-02-10
4 3 3 3 2023-03-05
5 4 5 5 2023-03-11
6 1 3 1 2023-04-02
7 5 1 4 2023-05-20
8 2 5 2 2023-05-22
9 3 4 1 2023-06-01
10 5 2 1 2023-06-15

The tables already exist in the database — there is nothing to create or load.

Task: Write a query returning two columns, name (the customer's name) and first_order (the date of that customer's earliest order, returned exactly as stored, in year-month-day text), one row per customer who has placed at least one order, earliest first_order first, and alphabetically by name if two customers share a first order date. A registered customer who has never ordered does not appear.

Example output — shape only, on invented customers and dates:

name first_order
Rosa Delgado 2019-01-05
Kenji Aoki 2019-01-19
Bram Visser 2019-02-02
Yara Osei 2019-03-14
Milo Fontaine 2019-04-08

Sign in to solve this problem

Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.

Sign in

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…