Databases & SQL Master Course — 183. Subscription Cohorts
Lesson 183 Subscription Cohorts
Prerequisites and objectives
Complete Lesson 182 and review the earlier concepts named in this lesson. You will define fixed cohort membership, create observable cohort-age cells, deduplicate repeated activity, and calculate a retention denominator correctly. Review left joins, CTEs, integer arithmetic, and conditional aggregation. Month numbers one, two, and three represent January, February, and March of one fictional year.
Define membership, activity, and observation separately
A cohort groups users by a chosen entry event. Here cohort_month is a fixed assigned entry month. Activity means at least one qualifying recorded event in a month. Retention at age one means activity in the month immediately after entry; it does not mean uninterrupted daily use, a paid renewal, or survival without cancellation.
The observation cutoff is the start of month three. Only months one and two are treated as complete and available for this report. March data happens to exist in the fixture, but it is deliberately outside the accepted observation window. Data presence alone does not make a month complete or comparable.
The denominator for each observable cohort-age cell is the number of members of that cohort. Do not let a join to activity define the denominator, because that would discard inactive members and can produce an artificial one hundred percent rate. A fixed membership relation anchors the measurement.
CREATE TABLE lab_member183(user_id INTEGER PRIMARY KEY,cohort_month INTEGER NOT NULL CHECK(cohort_month BETWEEN 1 AND 3));
CREATE TABLE lab_activity183(event_id INTEGER PRIMARY KEY,user_id INTEGER NOT NULL,month_no INTEGER NOT NULL);
INSERT INTO lab_member183 VALUES(1,1),(2,1),(3,2),(4,3);
INSERT INTO lab_activity183 VALUES(1,1,1),(2,2,1),(3,1,2),(4,1,2),(5,3,2),(6,4,3);
| Cohort and age | Retained / eligible | Retention |
|---|---|---|
| January, age 0 | 2 / 2 | 100.0% |
| January, age 1 | 1 / 2 | 50.0% |
| February, age 0 | 1 / 1 | 100.0% |
Build a calendar scaffold rather than guessing missing cells
Cross joining members with the requested ages creates possible observation cells. A cutoff predicate removes cells whose target month is not complete. The remaining left join attaches deduplicated activity while preserving inactive members. This sequence separates “eligible but inactive” from “not yet observable.”
The fixture’s month numbers make age addition easy to inspect. A real multi-year implementation needs a stable calendar-month key or tested date arithmetic; adding one to a YYYYMM number fails at year boundaries. Document timezone and boundary conversion before assigning source timestamps to months.
Exercise 1
Establish cohort sizes independently of activity.
Worked solution
-- Connection A
SELECT cohort_month,COUNT(*) FROM lab_member183 GROUP BY cohort_month ORDER BY cohort_month;
January has two members, February one, and March one. These membership counts remain valid even if a member has no activity. The March cohort exists but contributes no completed cell under the accepted cutoff; existence and observability are different facts.
Exercise 2
Compare raw events with distinct active users in February.
Worked solution
-- Connection A
SELECT COUNT(*),COUNT(DISTINCT user_id) FROM lab_activity183 WHERE month_no=2;
User one has two February events, while user three has one. Three events therefore represent two active users. Retention counts whether a user was active, not how many events that user generated. Deduplicate at user-month grain before joining to the cohort scaffold.
Exercise 3
Produce the observable retention cells.
Worked solution
-- Connection A
WITH ages(age) AS (VALUES(0),(1)), active AS (SELECT DISTINCT user_id,month_no FROM lab_activity183 WHERE month_no<3) SELECT m.cohort_month,g.age,COUNT(*) AS eligible,SUM(CASE WHEN a.user_id IS NOT NULL THEN 1 ELSE 0 END) AS retained,ROUND(100.0*SUM(CASE WHEN a.user_id IS NOT NULL THEN 1 ELSE 0 END)/COUNT(*),1) AS pct FROM lab_member183 m CROSS JOIN ages g LEFT JOIN active a ON a.user_id=m.user_id AND a.month_no=m.cohort_month+g.age WHERE m.cohort_month+g.age<3 GROUP BY m.cohort_month,g.age ORDER BY m.cohort_month,g.age;
January age one retains one of its original two members. February age one and March age zero are not reported because they require month three. Their absence is not evidence of zero retention. The decimal literal prevents integer division from truncating the percentage.
Exercise 4
Create a genuine observed zero without changing eligibility.
Worked solution
-- Connection A
BEGIN;
DELETE FROM lab_activity183 WHERE user_id=1 AND month_no=2;
WITH ages(age) AS (VALUES(0),(1)), active AS (SELECT DISTINCT user_id,month_no FROM lab_activity183 WHERE month_no<3) SELECT m.cohort_month,g.age,COUNT(*) AS eligible,SUM(CASE WHEN a.user_id IS NOT NULL THEN 1 ELSE 0 END) AS retained,ROUND(100.0*SUM(CASE WHEN a.user_id IS NOT NULL THEN 1 ELSE 0 END)/COUNT(*),1) AS pct FROM lab_member183 m CROSS JOIN ages g LEFT JOIN active a ON a.user_id=m.user_id AND a.month_no=m.cohort_month+g.age WHERE m.cohort_month+g.age<3 GROUP BY m.cohort_month,g.age ORDER BY m.cohort_month,g.age;
ROLLBACK;
January age one remains observable and keeps denominator two, but now no member is active. It correctly becomes zero percent. The contrast with an unobserved future cell is the important distinction. Rollback restores the original repeated events and fifty-percent result.
State what this retention measure can and cannot mean
Different products define retention using sessions, purchases, paid entitlement, or successful renewals. The same SQL pattern can support several definitions, but their business interpretations differ. A user returning after a missed month may count in this period-specific activity measure even though they would fail an uninterrupted-survival measure.
Cohort membership should not drift when later activity arrives unless the entry definition itself was corrected. Recomputing a cohort from whichever event is currently visible can move users across cohorts as backfilled data appears. Preserve source cutoff, membership version, and correction policy for reproducible comparisons.
Partial months, trial users, internal accounts, and multiple subscriptions per person require explicit eligibility rules. A subscription-level denominator is not automatically a user-level denominator. If one person holds two subscriptions, choose the analysis unit deliberately and build identifiers that support that choice.
Do not interpret differences between these tiny fictional cohorts as evidence that a product change caused improvement. The lesson prepares a transparent descriptive dataset. Causal claims require a separate design and analysis, including uncertainty and possible selection differences. SQL correctness is necessary for that work but not sufficient.
Review the output before drawing a chart
A useful cohort chart marks unobserved cells distinctly, labels age and calendar boundaries, and shows cohort sizes. Zero, missing data, and future ineligibility should not share a visual color without explanation. Include the metric definition and cutoff next to the chart so viewers do not compare unequal observation windows.
Test repeated events, no activity, an empty cohort, late-arriving activity, and a year boundary in a real calendar implementation. Reconcile the numerator to a list of retained identities when possible. A percentage alone hides the denominator and makes small cohorts look more stable than their evidence supports.
Pitfalls and summary
Anchor membership, deduplicate activity, scaffold only observable cells, and preserve inactive members in the denominator. Distinguish observed zero from not-yet-observed, and label period-specific activity retention without implying renewal, uninterrupted survival, or causality.
Knowledge check
Question 1
What is January age-one retention in the initial fixture?
- Two events divided by two members, or one hundred percent
- One active member divided by all four members, or twenty-five percent
- One active member divided by one active member, or one hundred percent
- One active member divided by two cohort members, or fifty percent
Answer and explanation
D. The fixed cohort denominator is two; repeated events do not add retained users.
Question 2
Why is February age one absent?
- Its retained numerator has been observed as zero
- Its target month is outside the completed observation window
- February has no cohort members
- The calculation intentionally excludes every age-one cell
Answer and explanation
B. A future or incomplete cell is not an observed zero.
Question 3
Why deduplicate at user-month grain?
- Each user should be reduced to one event across all months
- Only a user’s latest activity month should be counted
- Deduplication should remove inactive cohort members
- Several events can represent one active user in the same month
Answer and explanation
D. The numerator’s unit is users, not events.
Question 4
What would an inner join to activity risk?
- Dropping inactive members from the denominator
- Counting only activity rows still preserves the fixed cohort denominator
- Preserving every zero-activity member by default
- Treating all absent activity as future ineligibility
Answer and explanation
A. The membership scaffold needs a left join to retain eligible inactive users.
Question 5
What does the rollback-only deletion create?
- An unobservable future cell
- A smaller January cohort
- An observable zero-retention cell with an unchanged denominator
- A change from user grain to subscription grain
Answer and explanation
C. Eligibility remains fixed while qualifying activity is removed in the controlled trial.
Execution scope
These scenarios were executed with SQLite 3.53.1 using separate connections to newly created disposable local database files. No existing or external database was used. Each scenario starts from a fresh fixture. Python was an internal verification harness, not a learner prerequisite. Follow the labeled connection order in SQL clients that open the same disposable file.
Primary references
- SELECT and joins: Scaffolding and left-join preservation.
- Aggregates: Distinct users and conditional counts.
- CTEs: Readable staged transformations.
- Date functions: Calendar conversion considerations for extended implementations.