Practice Free
HardBusiness Analytics High Frequency

Net Revenue Retention (NRR) Cohort Performance

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.

Solve in Workbench

Problem Statement

Write a query using a CTE to aggregate revenue metrics from `cohort_annual_revenue` for the `'2023'` cohort: - `starting_mrr`: sum of `starting_mrr` - `expansion_mrr`: sum of `expansion_mrr` - `contraction_mrr`: sum of `contraction_mrr` - `churned_mrr`: sum of `churned_mrr` Calculate `nrr_percentage` in the outer query as: `ROUND(((starting_mrr + expansion_mrr - contraction_mrr - churned_mrr) * 100.0) / starting_mrr, 2)`. Return: 1. `cohort_year` 2. `starting_mrr` 3. `ending_mrr`: `(starting_mrr + expansion_mrr - contraction_mrr - churned_mrr)` 4. `nrr_percentage` Order by `cohort_year` ASC.

Why Technical Interviewers Ask This

SaaS venture capital and finance teams evaluate company health through Net Revenue Retention (NRR), calculating whether expansion from existing accounts outpaces contraction and churn.

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 business analytics constructs.

Canonical Modern SQL Solution

PostgreSQL 16 / ANSI SQL
WITH totals AS (
        SELECT 
          cohort_year,
          SUM(starting_mrr) AS starting_mrr,
          SUM(expansion_mrr) AS expansion_mrr,
          SUM(contraction_mrr) AS contraction_mrr,
          SUM(churned_mrr) AS churned_mrr
        FROM cohort_annual_revenue
        WHERE cohort_year = 2023
        GROUP BY cohort_year
      )
      SELECT 
        cohort_year,
        starting_mrr,
        (starting_mrr + expansion_mrr - contraction_mrr - churned_mrr) AS ending_mrr,
        ROUND(((starting_mrr + expansion_mrr - contraction_mrr - churned_mrr) * 100.0) / starting_mrr, 2) AS nrr_percentage
      FROM totals
      ORDER BY cohort_year ASC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Business Analytics. It meets the target criteria: Cohort ending MRR and Net Revenue Retention rate percentage.

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 Business Analytics 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.

More FAANG SQL Interview Questions

View all questions