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 three 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.

Top three frameworks by accuracy.

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
frameworkavg_accuracyaccuracy_ranklift_over_fleet
pytorch0.819599999999999910.029031818181818103
xgboost0.813846153846153920.023277972027972105
tensorflow0.78060606060606063-0.009962121212121144

Worked solution and explanation

What this is really testing


Build it in four moves

Step 1: Collapse the casing first

The framework column holds hand-typed names, so 'TensorFlow' and 'tensorflow' are the same tool spelled two ways. GROUP BY LOWER(framework) folds every casing variant into one group. Grouping on the raw column scatters one framework's models across several rows and understates its true model count.

Step 2: Average only the scored models

Some models carry a null accuracy: they were never evaluated. Because the mean is spelled out as SUM(accuracy) / COUNT(*), a null row would remain in COUNT(*) and inflate the denominator, pulling the average down. WHERE accuracy IS NOT NULL drops those rows so the denominator counts only scored models. The same filter has to appear inside the fleet-wide baseline subquery, or the comparison is measured against a corrupted denominator.

Step 3: Stamp each framework with its position

The board needs a position number per framework, so DENSE_RANK() OVER (ORDER BY SUM(accuracy) / COUNT(*) DESC) stamps each framework with its standing. The window runs over every framework group before the top slice is taken, so the surviving three come back numbered 1, 2, 3.

Step 4: Rank against the fleet, then take the best three

A scalar subquery, SELECT SUM(accuracy) / COUNT(*) FROM ml_models WHERE accuracy IS NOT NULL, gives the fleet-wide average across all scored models; subtracting it from each framework's average shows how far above or below the house each leader sits. ORDER BY avg_accuracy DESC with framework as a deterministic tiebreak puts the strongest first, and LIMIT 3 keeps the top three.


The solution

Framework leaderboard, best three 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, framework
LIMIT 3
SUM(accuracy) / COUNT(*), no null filter
COUNT(*) counts unscored models too, so pytorch and xgboost crater to roughly 0.62 and the board reorders to tensorflow, sklearn, xgboost: the wrong top three.
SUM(accuracy) / COUNT(*) WHERE accuracy IS NOT NULL
The denominator counts only scored models, matching AVG(accuracy). pytorch (0.82) and xgboost (0.81) hold the top two and the board is honest.

Common follow-up questions