SQL interview question: 30-day customer lifetime value

HardProduct metrics

Problem

Shared data rules

For each acquisition channel, find LTV-30: average revenue per user over the first 30 days after signup. Count only users whose 30 days fit entirely in the data.

Formulas

Example

Input: users
user_idsignup_tsacq_channelRow note
12026-08-01 13:00ads
22026-08-10 12:00ads← no orders — still in the denominator
32026-09-10 15:00ads← 30 days don’t fit
42026-07-20 11:00organic
52026-07-01 11:00organic← before Jul 6 — doesn’t fit
Input: orders
order_iduser_idcreated_atstatusamount_usdRow note
20112026-08-05 15:00delivered30
20212026-08-31 12:00delivered18← 29 d 23 h after signup
20312026-08-31 14:00delivered14← 30 d 1 h — doesn’t count
20432026-09-12 11:00delivered12
20542026-07-25 18:00delivered40
Output
acq_channelcohort_usersbuyersrevenue_usdltv_30d_usd
ads214824
organic114040

ads: (30 + 18) / 2 = $24 per cohort user; organic: 40 / 1 = $40.

Output columns

buyers
cohort users with an order within their 30 days

Notes

Solve it free

Your query runs on PostgreSQL in your browser. No sign-up needed.

Skills it tests

More interview questions

All questions