Top Frameworks by Accuracy

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
Forbes

Problem

Our model registry ml_models records every model we've trained, its framework, and its accuracy, but models that were never evaluated have no accuracy on file. We want a leaderboard of the 3 frameworks with the highest average accuracy across their scored models, each shown with its position on the board and how its average stacks up against the fleet-wide average over all scored models. Treat framework names that differ only in capitalization as one tool, and order the board best first.

A point separates the leaders, and the blanks decide who they are.

Tables

ml_models

Columns: model_id INTEGER, mdl_name TEXT, version TEXT, accuracy REAL, status TEXT, train_at TIMESTAMP, 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
frameworkavg_accuracyaccuracy_ranklift_over_fleet
pytorch0.819599999999999910.029031818181818103
xgboost0.813846153846153920.023277972027972105
tensorflow0.78060606060606063-0.009962121212121144

Worked solution and explanation

What this is really testing

Strip the ML veneer and this is a per-group average leaderboard with 2 data landmines and a fleet baseline bolted on. The top 2 frameworks sit barely a point apart, so any sloppiness in the mean reorders the board. Landmine one: framework was typed by hand, so 'TensorFlow' and 'tensorflow' are one tool spelled 2 ways, and grouping on the raw column splits it. Landmine two: some models were never evaluated and carry a null accuracy. Because the mean is written out as SUM(accuracy) / COUNT(*), those unscored rows stay in the denominator and quietly drag a framework down; leave them in and the top 3 flips from pytorch, xgboost, tensorflow to tensorflow, sklearn, xgboost.


Build it in 4 moves

Step 1: Collapse the casing first

The framework column holds hand-typed names. GROUP BY LOWER(framework) folds every casing variant into one group; grouping on the raw column scatters one tool's models across 2 rows that each average only part of them.

Step 2: Average only the scored models

A null accuracy means the model was never evaluated. With the mean spelled out as SUM(accuracy) / COUNT(*), a null row still lands in COUNT(*) and pulls the average down. WHERE accuracy IS NOT NULL drops those rows, and the same filter has to sit inside the fleet baseline subquery, or the comparison runs against a corrupted denominator.

Step 3: Stamp each framework with its position

DENSE_RANK() OVER (ORDER BY SUM(accuracy) / COUNT(*) DESC) runs over every framework group after the GROUP BY and before the LIMIT, so the 3 survivors come back numbered 1, 2, 3.

Step 4: Measure against the fleet, then keep the best 3

The scalar subquery (SELECT SUM(accuracy) / COUNT(*) FROM ml_models WHERE accuracy IS NOT NULL) is the fleet average over scored models; subtracting it gives lift_over_fleet. ORDER BY avg_accuracy DESC puts the strongest first and LIMIT 3 keeps the leaders. The averages are distinct around the cut, so the third slot needs no tiebreak key.


The solution

Framework leaderboard, best 3 with position and fleet lift
SELECT LOWER(framework) AS framework,
       SUM(accuracy) / COUNT(*) AS avg_accuracy,
       DENSE_RANK() OVER (ORDER BY SUM(accuracy) / COUNT(*) DESC) AS accuracy_rank,
       SUM(accuracy) / COUNT(*) - (SELECT SUM(accuracy) / COUNT(*) FROM ml_models WHERE accuracy IS NOT NULL) AS lift_over_fleet
FROM ml_models
WHERE accuracy IS NOT NULL
GROUP BY LOWER(framework)
ORDER BY avg_accuracy DESC
LIMIT 3
Mean over every row
Without the filter, COUNT(*) counts unscored models too, so pytorch and xgboost sink to roughly 0.62 and the board reorders to tensorflow, sklearn, xgboost: the wrong top 3.
Mean over scored rows
With WHERE accuracy IS NOT NULL, the denominator matches AVG(accuracy). Pytorch (0.82) and xgboost (0.81) hold the top 2 and the board is correct.

Common follow-up questions