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
| model_id | mdl_name | version | accuracy | status | train_at | framework |
|---|---|---|---|---|---|---|
| 123 | rec_engine | v1.1 | 0.72 | training | 2026-02-02 01:00:00 | TensorFlow |
| 146 | churn_pred | v2.0 | 0.89 | archived | 2026-03-03 02:00:00 | XGBoost |
| 169 | spam_filter | 3.0 | 0.61 | failed | 2026-04-04 03:00:00 | sklearn |
| 192 | img_classify | v2.1-beta | 0.78 | validating | 2026-05-05 04:00:00 | pytorch |
| 215 | sentiment | 1.0.0 | 0.95 | Deployed | 2026-06-06 05:00:00 | tensorflow |
| 238 | demand_fcst | v1.0 | 0.67 | deployed | NULL | PyTorch |
| 261 | price_opt | v1.1 | 0.84 | Training | 2026-08-08 07:00:00 | TensorFlow |
| 284 | fraud_detect | v2.0 | NULL | archived | 2026-09-09 08:00:00 | XGBoost |
| 307 | rec_engine | 3.0 | 0.73 | failed | 2026-01-10 09:00:00 | sklearn |
| 330 | churn_pred | v2.1-beta | 0.9 | validating | 2026-02-11 10:00:00 | pytorch |
Expected output
| framework | avg_accuracy | accuracy_rank | lift_over_fleet |
|---|---|---|---|
| pytorch | 0.8195999999999999 | 1 | 0.029031818181818103 |
| xgboost | 0.8138461538461539 | 2 | 0.023277972027972105 |
| tensorflow | 0.7806060606060606 | 3 | -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
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 matchesAVG(accuracy). Pytorch (0.82) and xgboost (0.81) hold the top 2 and the board is correct.
Common follow-up questions
- How would you also report how many scored models back each framework's average, so a framework with 2 models isn't ranked beside one with 200? (Tests adding `COUNT(*)` to the grouped query and reasoning about sample-size reliability.)
- If leadership only trusts frameworks with at least 20 scored models, where does that threshold go? (Tests `HAVING` versus `WHERE` placement on a post-aggregation count.)
- If 2 frameworks genuinely tie for third by average, what do `DENSE_RANK()` and `LIMIT 3` return, and how would you make that stable across runs? (Tests tie semantics under a window rank and when a tiebreak key becomes necessary.)