cswork / libraryOpen practice workspace

Coding questions · Mixpanel

Count Users Retained on Day Seven

Count users active on the seventh calendar day after signup, without double-counting repeated events.

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.

Write one read-only PostgreSQL query to count distinct users with at least one event on the calendar date seven days after their signup calendar date. Match events to users by user_id.

Compare the date components of event_ts and signup_date: time of day does not change whether an event is on day seven. This asks about the seventh calendar day, rather than any event during the first seven days. Count each qualifying user once even if that user has several events on that date.

Return exactly one row with one column named d7_retained_users. If nobody qualifies, the count is zero. Output row order is unrestricted.

Count Users Retained on Day Seven

Table users

columntypeconstraints
user_idINTEGER
signup_dateTIMESTAMP
plan_typeTEXT

Sample data:

plan_typesignup_dateuser_id
free2026-09-20 15:30:001
pro2026-09-20 18:00:002
enterprise2026-09-20 06:00:003
free2025-12-25 23:45:004
trial2024-02-22 13:00:005
NULL2026-09-20 09:00:006
proNULL7
free2026-09-20 08:00:008

Table events

columntypeconstraints
user_idINTEGER
event_tsTIMESTAMP

Sample data:

event_tsuser_id
2026-09-27 00:10:001
2026-09-27 23:59:001
2026-09-26 15:30:001
2026-09-26 18:00:002
2026-09-28 18:00:002
2026-09-20 06:00:003
2026-01-01 00:15:004
2024-02-29 00:00:005
2026-09-27 08:00:007
NULL8
2026-09-27 12:00:0099
2026-09-27 12:00:00NULL

Expected output

d7_retained_users
3