Practice Free
MediumAggregations

Churned MRR Concentration by Industry Sector

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

Group records in `saas_contracts` by `industry_sector`. Calculate: 1. `industry_sector` 2. `churned_logos`: Count of accounts where `contract_status = 'churned'` 3. `lost_mrr`: Sum of `mrr_value` for churned accounts 4. `avg_lost_mrr`: Average `mrr_value` of churned accounts, rounded to 2 decimal places Filter records with `WHERE contract_status = 'churned'`. Use `HAVING` to only include industries where `SUM(mrr_value) >= 3000.00`. Order results by `lost_mrr` descending.

Why Technical Interviewers Ask This

The board of directors wants an audit of lost monthly recurring revenue (MRR) due to customer cancellations. They need to identify industry verticals that experienced at least $3,000 in lost MRR.

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 SQL
SELECT 
        industry_sector,
        COUNT(*) AS churned_logos,
        SUM(mrr_value) AS lost_mrr,
        ROUND(AVG(mrr_value), 2) AS avg_lost_mrr
      FROM saas_contracts
      WHERE contract_status = 'churned'
      GROUP BY industry_sector
      HAVING SUM(mrr_value) >= 3000.00
      ORDER BY lost_mrr DESC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Aggregations. It meets the target criteria: Summary of lost MRR and churned accounts per industry sector with lost MRR >= $3,000.

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.

More FAANG SQL Interview Questions

View all questions