Practice Free
MediumAggregations

MRR Breakdown by Subscription Tier

Frequently Asked By:AppleGoogleAmazon
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

Aggregate the `customer_accounts` table for all **active** accounts. Calculate: 1. `tier` 2. `total_mrr`: Sum of `mrr` 3. `active_customers`: Count of accounts in that tier 4. `avg_seats`: Average `seat_count` rounded to 1 decimal place Filter to keep only tiers with **at least 2** active customers using a `HAVING` clause. Order the results by `total_mrr` in **descending** order.

Why Technical Interviewers Ask This

The Chief Revenue Officer needs a summary of active revenue distribution across customer tiers. Tiers with fewer than 2 active accounts should be filtered out to protect customer confidentiality.

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 
        tier,
        SUM(mrr) AS total_mrr,
        COUNT(*) AS active_customers,
        ROUND(AVG(seat_count), 1) AS avg_seats
      FROM customer_accounts
      WHERE status = 'active'
      GROUP BY tier
      HAVING COUNT(*) >= 2
      ORDER BY total_mrr DESC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Aggregations. It meets the target criteria: Aggregate table showing tier, total_mrr, active_customers, and avg_seats for tiers with >= 2 active accounts.

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 Apple 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