How would you assemble a cohort or retention grid in SQL? Explain the query technique for grouping entities by first-activity period and measuring activity in later periods.

Last updated 13 July 2026

  • Assign each user a cohort: the period of their first activity, e.g. DATE_TRUNC('month', MIN(event_date)) grouped by user.

  • Join activity back to the cohort and compute a period offset (periods since the cohort start, 0 = first period). Then group by cohort and offset with COUNT(DISTINCT user_id).

  • Divide each cell by the period-0 size to get retention %. Recent cohorts have fewer observable periods, which is not churn.

  • https://medium.com/@BIWave/9-ways-to-master-advanced-sql-analytics-c218cee830b9
  • https://dev.to/gervaisamoah/advanced-sql-part-1-window-functions-explained-with-precision-146o
  • https://sotaaz.com/post/sql-cohort-analysis-en
  • https://medium.com/towardsdev/monthly-cohort-retention-in-sql-crack-this-databricks-interview-question-like-a-pro-dac73335f8ec
  • https://www.stratascratch.com/blog/retention-in-sql-how-to-calculate-user-and-cohort-retention
  • https://dev.to/gowthampotureddi/salesforce-data-engineering-interview-questions-5bo9
All Advanced SQL interview questions