SQL interview question: Monthly User Retention Rate

Calculate Monthly Retention Rate

Product managers need to understand how many users from each month return the following month.

Calculate month-over-month retention rate. A user is "retained" if they were active in both the current month AND the previous month.

Return month (YYYY-MM format) and retention_rate, rounded to 2 decimal places. Only include months that have a previous month of activity. Formula: (Retained Users / Previous Month Users) × 100

Inspired by Meta's data science interview questions

Tables

user_events 13 rows
  • event_id integer PK
  • user_id integer
  • event_time text
  • event_type text
event_iduser_idevent_timeevent_type
112025-01-05 10:00:00page_view
212025-01-05 10:05:00click
312025-01-05 10:15:00page_view

Keep going