Analytical SQL & Cohorts
Real-world business analytics: User Retention (Day 1, Day 7, Day 30), Cohort Analysis, and Funnels.
Core Learning Objectives
- •Build retention tables by joining initial signup cohort with subsequent activity logs
- •Calculate rolling 7-day active user (WAU) and 30-day active user (MAU) metrics
- •Analyze funnel step drop-off percentages with conditional aggregations
Interactive Cheatsheet
PostgreSQL SyntaxSELECT signup_month,
COUNT(DISTINCT user_id) AS cohort_size,
COUNT(DISTINCT CASE WHEN active_month = signup_month + INTERVAL '1 month' THEN user_id END)::FLOAT /
COUNT(DISTINCT user_id) AS m1_retention
FROM user_cohorts
GROUP BY signup_month;Curated Practice Problems (4)
7-Day Active Users Growth
The Growth team at Meta wants to track monthly active users (MAU) who were also active in the previous calendar month. Write a query to calculate the number of retained active users for each month. A retained user in month $M$ is defined as a user who performed at least one action in month $M$ AND performed at least one action in month $M-1$. Output the month (as an integer 1-12 or date formatted 'YYYY-MM') and the count of retained active users. Order by month ascending. Tables: - `user_actions` (`user_id` INT, `event_id` VARCHAR, `action_date` TIMESTAMP)
Binge-Watching Session Identification
A candidate reported this streaming analytics problem from a top media company interview. A "binge session" is defined as a series of 3 or more video watches by the same user where each subsequent watch began within 30 minutes of the previous watch ending. Write a query to identify all `user_id`s who had at least one binge session on `2026-02-14`. Output `user_id` in ascending order with no duplicates. Table: - `watch_events` (`event_id` INT, `user_id` INT, `start_time` TIMESTAMP, `end_time` TIMESTAMP)
Weekly Signup Retention Cohort Curves
Meta's Growth leadership tracks user retention curves across weekly signup cohorts. Write a SQL query to calculate Day-7 Retention Rate for each weekly cohort. A user signed up on date $D$ is defined as retained on Day-7 if they logged at least one activity between day $D+1$ and day $D+7$. Return the `cohort_week` (formatted as 'YYYY-MM-DD' representing the start of the week), `new_users_count`, `retained_users_count`, and `retention_rate_pct` rounded to 1 decimal place. Sort by `cohort_week` ascending. Tables: - `users` (`user_id` INT, `signup_date` DATE) - `user_activity` (`user_id` INT, `activity_date` DATE)
Month-over-Month Net Churn & Reactivations
Netflix Subscriber Analytics tracks monthly subscription continuity to compute net churn. Write a SQL query to identify subscribers who were active in January 2026 but did not renew or log in in February 2026 (churned users). Return the `plan_type`, total starting active users in January (`jan_subscribers`), and churned count in February (`churned_count`). Order by `churned_count` descending. Tables: - `subscriptions` (`user_id` INT, `plan_type` VARCHAR, `active_month` DATE)