SQL interview question: weekly cohort retention table

HardRetention

Problem

Shared data rules

Build a weekly cohort retention table for signup weeks starting Jul 6 through Aug 31, 2026. For each cohort, find the share who bought in each of weeks 1–4 after the signup week.

Formula

Example

Input: users
user_idsignup_tsRow note
12026-08-31 10:00
22026-09-02 13:00
32026-09-03 13:00← never bought
Input: orders
order_iduser_idcreated_atstatusRow note
10112026-09-08 15:00delivered← week 1
10222026-09-03 18:00delivered← week 0 — not in the table
10322026-09-10 13:00delivered← week 1
10412026-09-21 11:00delivered← week 3
10522026-09-28 01:30delivered← week 4 — not over yet
Output
cohort_weekweek_numcohort_sizeactive_usersretention
2026-08-311320.6667
2026-08-312300
2026-08-313310.3333

The Aug 31 cohort has 3 users. Week 2 has no purchases but stays as a zero row, and week 4 (Sep 28 – Oct 4) hasn’t ended in the data, so it has no row.

Output columns

week_num
1–4: weeks after the signup week (that one is week 0)
cohort_size
all cohort users, including those who never bought
Solve it free

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

Skills it tests

More interview questions

All questions