A retention query can return a plausible percentage while quietly answering a different question. Here is a small SQLite example you can inspect end to end.
Question: Of the users whose first observed activity was January 1, 2026, what percentage were active exactly seven days later?
The fictional log has four users:
- User 1 first appears January 1 and returns January 8. Both dates have duplicate activity rows.
- User 2 first appears January 1 and returns January 7, not January 8.
- User 3 first appears December 31 and is also active January 1 and January 8.
- User 4 first appears January 1 and returns January 9.
Before reading the query, decide whether the answer should be 1/3 or 2/4.
Complete runnable SQLite setup and solution:
CREATE TABLE user_activity (
user_id INTEGER,
activity_date TEXT
);
INSERT INTO user_activity VALUES
(1, '2026-01-01'), (1, '2026-01-01'),
(1, '2026-01-08'), (1, '2026-01-08'),
(2, '2026-01-01'), (2, '2026-01-07'),
(3, '2025-12-31'), (3, '2026-01-01'),
(3, '2026-01-08'),
(4, '2026-01-01'), (4, '2026-01-09');
WITH params AS (
SELECT '2026-01-01' AS cohort_date
),
first_activity AS (
SELECT user_id, MIN(activity_date) AS first_activity_date
FROM user_activity
WHERE user_id IS NOT NULL AND activity_date IS NOT NULL
GROUP BY user_id
),
cohort AS (
SELECT f.user_id
FROM first_activity AS f
CROSS JOIN params AS p
WHERE f.first_activity_date = p.cohort_date
),
retained_users AS (
SELECT DISTINCT a.user_id
FROM user_activity AS a
CROSS JOIN params AS p
WHERE a.activity_date = date(p.cohort_date, '+7 days')
AND a.user_id IS NOT NULL
)
SELECT
COUNT(c.user_id) AS cohort_size,
COUNT(r.user_id) AS retained_users,
100.0 * COUNT(r.user_id) / NULLIF(COUNT(c.user_id), 0)
AS retention_rate_pct
FROM cohort AS c
LEFT JOIN retained_users AS r ON r.user_id = c.user_id;
The result is 3 cohort members, 1 retained user, 33.3333% retention.
Three details determine that answer:
MIN(activity_date) establishes each user's first observed activity using the full supplied history. Filtering to January 1 before computing the minimum would wrongly admit User 3.
SELECT DISTINCT deduplicates returning users, so repeated activity does not multiply their contribution.
- The
LEFT JOIN keeps users who did not return in the denominator. NULLIF makes the percentage NULL if the cohort is empty.
Counting anyone active on January 1 gives 2/4 = 50%, but that answers a different question. Exact day-7 retention also differs from returning at any point during the following week.
To run this without installing anything, open https://sqlite.org/fiddle/ . If it starts at a terminal prompt, choose Toggle view, paste the full SQL into the input box, and choose Run. Use a new example database; Reset DB clears the fictional table before rerunning the setup.
The data must contain the relevant history and validated ISO dates. First observed activity is not necessarily a signup date. With incomplete history, the query cannot establish a true signup cohort.
The same exercise, explanation and copyable SQL file on GitHub.
Disclosure: I'm the creator, Israel Diego Guerra. AI assisted preparation, and I executed this exact example in SQLite and its official browser Fiddle. The linked lesson ends with an optional link to my $79 interview pack; all data, the query and the answer are free above and on GitHub, without signup.