cswork / libraryOpen practice workspace

Coding questions · Mixpanel

Compare First-Week Retention by Plan Type

Compare first-week retention by recorded plan type with distinct-user counts, inclusive denominators, and four-decimal rates.

Compare First-Week Retention Across Plan Types

You have these tables:

TableColumnMeaning
usersuser_idUser identifier.
userssignup_dateThe user's signup date or timestamp.
usersplan_typeThe user's plan type.
eventsuser_idUser associated with an event.
eventsevent_tsEvent timestamp.

For this exercise, a user is retained if the user has at least one matching event whose event_ts is greater than or equal to signup_date and strictly less than signup_date plus seven days. Match events to user records by user_id.

Write one read-only PostgreSQL query that reports first-week retention separately for each plan_type present in users. Return these columns in this order:

  • plan_type: the plan group.
  • users: the number of distinct user identifiers in that plan group.
  • retained_users: the number of distinct users in that group meeting the first-week event condition.
  • retention_rate: retained_users divided by users, rounded to four decimal places.

Include users with no qualifying events in the denominator, and count each retained user once even when several events qualify. Include plan groups with zero retained users. Group by the recorded plan value; do not collapse plan types or filter to a subset of plans.

The output row order is unrestricted.

Compare First-Week Retention Across Plan Types

Table users

columntypeconstraints
user_idINTEGER
signup_dateTIMESTAMP
plan_typeTEXT

Sample data:

plan_typesignup_dateuser_id
free2026-09-01 12:00:001
free2026-09-01 12:00:002
free2026-09-01 18:30:003
pro2026-09-01 09:00:004
pro2026-09-02 10:00:005
pro2026-09-03 15:00:006
enterprise2026-09-01 13:00:007
trial2026-09-04 12:00:008
NULL2026-09-05 23:00:009
NULL2026-09-05 23:00:0010
enterpriseNULL11

Table events

columntypeconstraints
user_idINTEGER
event_tsTIMESTAMP

Sample data:

event_tsuser_id
2026-09-01 12:00:001
2026-09-03 12:00:001
2026-09-01 11:59:592
2026-09-08 12:00:002
2026-09-08 18:29:593
2026-09-07 12:00:004
2026-09-07 12:00:004
NULL5
2026-09-10 15:00:006
2026-09-01 12:59:597
2026-09-08 13:00:007
2026-09-05 23:00:009
2026-09-03 13:00:0011
2026-09-03 12:00:0099
2026-09-03 12:00:00NULL

Expected output

plan_typeusersretained_usersretention_rate
enterprise200
free320.6667
pro310.3333
trial100
NULL210.5