Top Framework by Deployments

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L4
Asked in
Netflix

Problem

Which three models in our ml_models registry have the most entries logged with a status of exactly 'deployed', and what framework does each run on? Only that lowercase spelling marks a live deployment, so 'Deployed' rows count nowhere, not even in the total. Give each model's deployed count and its percentage of all deployed entries, with alphabetically earlier model names (then frameworks) winning any tie at the cutoff.

The framework most often deployed.

Tables

ml_models

Columns: model_id INTEGER, mdl_name TEXT, version TEXT, accuracy REAL, status TEXT, train_at TEXT, framework TEXT

ml_models: 10 of 200 rows
model_idmdl_nameversionaccuracystatustrain_atframework
123rec_enginev1.10.72training2026-02-02 01:00:00TensorFlow
146churn_predv2.00.89archived2026-03-03 02:00:00XGBoost
169spam_filter3.00.61failed2026-04-04 03:00:00sklearn
192img_classifyv2.1-beta0.78validating2026-05-05 04:00:00pytorch
215sentiment1.0.00.95Deployed2026-06-06 05:00:00tensorflow
238demand_fcstv1.00.67deployedNULLPyTorch
261price_optv1.10.84Training2026-08-08 07:00:00TensorFlow
284fraud_detectv2.0NULLarchived2026-09-09 08:00:00XGBoost
307rec_engine3.00.73failed2026-01-10 09:00:00sklearn
330churn_predv2.1-beta0.9validating2026-02-11 10:00:00pytorch

Expected output

First 3 of 3 rows
mdl_nameframeworkdeploy_countdeploy_share_pct
churn_predPyTorch825
demand_fcstPyTorch825
fraud_detectPyTorch825

Worked solution and explanation

What this really is

This is a capped leaderboard glued to a fleet-wide denominator, dressed up as a deployment report. Anyone can count deployed rows per model and keep three. The two places candidates lose it: the cutoff, where a tie at third place makes LIMIT 3 pick a winner at random unless the sort says who wins, and the denominator, which has to be every deployed entry in ml_models, not the three leaders added together. Get the second one wrong and the shares sum to 100 and every number is inflated.

Step 1: Filter to exact 'deployed' first

WHERE status = 'deployed' runs before any counting. The registry also holds 'Deployed' and 'DEPLOYED'; those are not live deployments here, so an exact match is the rule, not a case-folded one.

Step 2: Count at the (mdl_name, framework) grain

Every deployed row is one entry, repeat versions included, so this is COUNT(*), not a unique count. Grouping by both columns is what lets the output carry the framework without guessing which one a model runs on.

Step 3: Make the cutoff deterministic

Inside top_models, sort by deploy_count DESC, then mdl_name ASC, then framework ASC, and LIMIT 3. The trailing keys only matter when counts tie at the boundary, and that is exactly when you need them.

Step 4: Divide by the whole fleet

The scalar subquery (SELECT COUNT(*) FROM ml_models WHERE status = 'deployed') counts every deployed entry once, independent of the three rows it sits beside. Multiplying by 100.0 both scales to a percentage and dodges integer division.

Top three deployed models with their share
WITH deployed_counts AS (
  SELECT mdl_name, framework, COUNT(*) AS deploy_count
  FROM ml_models
  WHERE status = 'deployed'
  GROUP BY mdl_name, framework
),
top_models AS (
  SELECT mdl_name, framework, deploy_count
  FROM deployed_counts
  ORDER BY deploy_count DESC, mdl_name ASC, framework ASC
  LIMIT 3
)
SELECT
  mdl_name,
  framework,
  deploy_count,
  100.0 * deploy_count / (SELECT COUNT(*) FROM ml_models WHERE status = 'deployed') AS deploy_share_pct
FROM top_models
Denominator from the leaders
SUM(deploy_count) OVER () or a sum over top_models divides by the three leaders' entries. The shares always add to 100, which looks tidy and answers nothing.
Denominator from the fleet
A scalar subquery over ml_models filtered to 'deployed' counts every live entry. The three shares add to well under 100, which is the actual concentration story.

Common follow-up questions