Practice Free
MediumWindow Functions

Dense Ranking of Workspaces by License Seat Saturation

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

For each workspace in `saas_workspaces`, calculate the seat utilization percentage as `ROUND(active_seats * 100.0 / allocated_seats, 1)`. Assign `saturation_rank` using `DENSE_RANK()` partitioned by `plan_tier` and ordered by utilization percentage descending. Return: 1. `workspace_id` 2. `org_name` 3. `plan_tier` 4. `utilization_pct` 5. `saturation_rank` Order results by `plan_tier` ascending, then by `saturation_rank` ascending, then by `workspace_id` ascending.

Why Technical Interviewers Ask This

Customer Success teams identify enterprise accounts ripe for expansion by ranking workspaces within each subscription tier based on their active user seat utilization percentage.

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 window functions constructs.

Canonical Modern SQL Solution

PostgreSQL 16 / ANSI SQL
WITH ranked_workspaces AS (
        SELECT 
          workspace_id,
          org_name,
          plan_tier,
          ROUND(active_seats * 100.0 / allocated_seats, 1) AS utilization_pct,
          DENSE_RANK() OVER (
            PARTITION BY plan_tier
            ORDER BY ROUND(active_seats * 100.0 / allocated_seats, 1) DESC
          ) AS saturation_rank
        FROM saas_workspaces
      )
      SELECT 
        workspace_id,
        org_name,
        plan_tier,
        utilization_pct,
        saturation_rank
      FROM ranked_workspaces
      ORDER BY plan_tier ASC, saturation_rank ASC, workspace_id ASC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Window Functions. It meets the target criteria: Dense rank of SaaS accounts by license saturation within tier.

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 Window Functions 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