New-Customer 90-Day LTV by Acquisition Cohort

Quick Overview

Write a SQL query that finds each customer's first order, sums their spend over the following 90 days, and reports cohort size, average and total 90-day value by acquisition month. It tests cohort assignment, half-open date windows, and aggregating at the customer level before the cohort level.

New-Customer 90-Day LTV by Acquisition Cohort

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

A customer's 90-day LTV is the sum of `amount` over their orders in `[first_date, first_date + 90 days)`, where `first_date` is their earliest `order_date`. Group customers by `cohort_month` (`YYYY-MM` of `first_date`) and return `cohort_month`, `new_customers`, `avg_ltv_90d` and `total_ltv_90d` (both rounded to 2 decimals), ordered by `cohort_month` ascending. ```hint Two levels of grouping Settle each customer's first date and 90-day total before you think about cohorts, and check which end of the window is open. ``` ### Input Table(s) Table `orders` (primary key `order_id`, all columns NOT NULL): | Column | Type | Meaning | |---|---|---| | order_id | INT | Identifier of the order | | customer_id | INT | Customer who placed the order | | order_date | DATE | Date of the order | | amount | DECIMAL(10,2) | Order value; non-negative | ### Output Contract One row per cohort month that has at least one new customer, with these columns in this order: | Column | Definition | |---|---| | cohort_month | `first_date` formatted as `YYYY-MM` (text) | | new_customers | Number of customers whose `first_date` falls in this month | | avg_ltv_90d | Average 90-day LTV of those customers, rounded to 2 decimals | | total_ltv_90d | Sum of the 90-day LTV of those customers, rounded to 2 decimals | Sort by `cohort_month` ascending. ### Example `orders`: | order_id | customer_id | order_date | amount | |---|---|---|---| | 1 | 1 | 2023-06-01 | 100.00 | | 2 | 1 | 2023-08-29 | 40.00 | | 3 | 1 | 2023-08-30 | 25.00 | | 4 | 2 | 2023-06-15 | 60.00 | Expected output: | cohort_month | new_customers | avg_ltv_90d | total_ltv_90d | |---|---|---|---| | 2023-06 | 2 | 100.00 | 200.00 | Customer 1 first purchased on 2023-06-01, so the window ends before 2023-08-30. Order 2 (2023-08-29) is inside, and order 3 (2023-08-30) is exactly 90 days later and excluded, so the LTV is 140.00. Customer 2 is also in cohort 2023-06, with an LTV of 60.00. The cohort has 2 customers, an average of 100.00 and a total of 200.00. ### Constraints and Clarifications - `orders` has 1 to 10,000 rows, and `amount` is non-negative. - No column is NULL. - The window is half-open: an order counts when `order_date >= first_date` and `order_date < first_date + 90 days`, so day 0 through day 89 are inside. - Every order row counts separately, including several orders on the same day. All orders on `first_date` are inside the window. - A customer belongs to exactly one cohort, the month of their first order. Later orders in other months do not create new cohort rows. - Use the data as given. A customer whose 90-day window runs past the last date in the table keeps the partial total, with no adjustment.

Overview: Write a SQL query that finds each customer's first order, sums their spend over the following 90 days, and reports cohort size, average and total 90-day value by acquisition month. It tests cohort assignment, half-open date windows, and aggregating at the customer level before the cohort level.

Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

A customer's 90-day LTV is the sum of amount over their orders in [first_date, first_date + 90 days), where first_date is their earliest order_date. Group customers by cohort_month (YYYY-MM of first_date) and return cohort_month, new_customers, avg_ltv_90d and total_ltv_90d (both rounded to 2 decimals), ordered by cohort_month ascending.

Input Table(s)

Table orders (primary key order_id, all columns NOT NULL):

ColumnTypeMeaning
order_idINTIdentifier of the order
customer_idINTCustomer who placed the order
order_dateDATEDate of the order
amountDECIMAL(10,2)Order value; non-negative

Output Contract

One row per cohort month that has at least one new customer, with these columns in this order:

ColumnDefinition
cohort_monthfirst_date formatted as YYYY-MM (text)
new_customersNumber of customers whose first_date falls in this month
avg_ltv_90dAverage 90-day LTV of those customers, rounded to 2 decimals
total_ltv_90dSum of the 90-day LTV of those customers, rounded to 2 decimals

Sort by cohort_month ascending.

Example

orders:

order_idcustomer_idorder_dateamount
112023-06-01100.00
212023-08-2940.00
312023-08-3025.00
422023-06-1560.00

Expected output:

cohort_monthnew_customersavg_ltv_90dtotal_ltv_90d
2023-062100.00200.00

Customer 1 first purchased on 2023-06-01, so the window ends before 2023-08-30. Order 2 (2023-08-29) is inside, and order 3 (2023-08-30) is exactly 90 days later and excluded, so the LTV is 140.00. Customer 2 is also in cohort 2023-06, with an LTV of 60.00. The cohort has 2 customers, an average of 100.00 and a total of 200.00.

Constraints and Clarifications

  • orders has 1 to 10,000 rows, and amount is non-negative.
  • No column is NULL.
  • The window is half-open: an order counts when order_date >= first_date and order_date < first_date + 90 days , so day 0 through day 89 are inside.
  • Every order row counts separately, including several orders on the same day. All orders on first_date are inside the window.
  • A customer belongs to exactly one cohort, the month of their first order. Later orders in other months do not create new cohort rows.
  • Use the data as given. A customer whose 90-day window runs past the last date in the table keeps the partial total, with no adjustment.