SQL interview question: time between consecutive purchases

MediumCompare with previous

Problem

Shared data rules

For each acquisition channel, find the gaps between July–August 2026 purchases and the same buyer’s next purchase. Return their count, average length in days, and the share of gaps up to 7 days.

Formulas

Example

Input: users
user_idacq_channel
1ads
2referral
Input: orders
order_iduser_idcreated_atstatusRow note
10112026-08-10 12:00delivered
10212026-09-01 01:15delivered← September — next after 101
10322026-08-20 15:00delivered
10422026-08-27 15:00delivered← exactly 7 × 24 h — counts
10522026-09-10 11:00delivered← September — next after 104
Output
acq_channelgapsavg_gap_daysshare_within_7d
ads121.550
referral210.420.5

ads: Aug 10 to Sep 1 — 21.55 days. referral: 7 and 13.83 days — average 10.42; only the first is within 7 days, share 0.5.

Notes

Solve it free

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

Skills it tests

More interview questions

All questions