MediumAggregations
Subscription Churn Variance by Billing Frequency
Frequently Asked By:MicrosoftGoogleStripe
Interactive In-Browser Workspace
Practice this problem on live schema tables in PostgreSQL 16 WASM with instant row verification and visual EXPLAIN plans.
Problem Statement
Group records in `customer_subscriptions` by `billing_frequency`.
Calculate:
1. `billing_frequency`
2. `total_subscriptions`: Total subscriptions
3. `churned_count`: Subscriptions with `status = 'churned'`
4. `churn_rate_pct`: Percentage of subscriptions churned, rounded to 2 decimal places
Order results by `churn_rate_pct` descending.
Why Technical Interviewers Ask This
Subscription financial analysts evaluate churn variance between annual upfront commitments and monthly flexible billing plans to calibrate contract discounting.
Common Pitfalls & Interview Traps
Forgetting required sorting and deterministic tie-breaking.
Not accounting for NULL or zero-value denominators during calculations.
Using inefficient quadratic subqueries instead of single-pass aggregations constructs.
Canonical Modern SQL Solution
PostgreSQL 16 / ANSI SQLSELECT
billing_frequency,
COUNT(*) AS total_subscriptions,
COUNT(CASE WHEN status = 'churned' THEN 1 END) AS churned_count,
ROUND(COUNT(CASE WHEN status = 'churned' THEN 1 END) * 100.0 / COUNT(*), 2) AS churn_rate_pct
FROM customer_subscriptions
GROUP BY billing_frequency
ORDER BY churn_rate_pct DESC;Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Aggregations. It meets the target criteria: Subscription counts and churn rates by monthly vs annual frequency.Edge Cases to Verbally Clarify with Your Interviewer
The dataset contains zero matching records (must return valid empty result without crashing).
Multiple records tie on sorting criteria (must evaluate tie-breaking order deterministically).
Foreign keys or secondary attributes have NULL values.
Frequently Asked Follow-Up Questions
What makes this question a favorite among Microsoft and Google interviewers?
Interviewers use this problem to test whether candidates write defensive, scalable Aggregations queries that run efficiently on multi-million row production databases without excessive memory usage.
How does the query optimizer execute this query?
Using single-pass table scans with index lookups or sort buffers. Running EXPLAIN (ANALYZE, BUFFERS) in the DB Exercises workbench visualizes the execution nodes and buffer hit cache.