The Proving Ground

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
L3
Asked in
ESPN

Problem

Ahead of picking one framework to standardize on for 2026, an analyst is comparing the models in ml_models trained that year, counting the ones with no training timestamp too, since those came in through this year's registry migration. Show each framework's average accuracy, highest first.

One team, one framework. Let the accuracy numbers decide.

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 4 of 4 rows
frameworkavg_accuracy
xgboost0.822
pytorch0.81525
tensorflow0.784
sklearn0.742

Worked solution and explanation

What this really tests

Strip off the ML costume and this is a per-framework average over a one-year slice, with two traps in it. First, framework is entered by hand: 'PyTorch' and 'pytorch' are one framework, and so are 'TensorFlow' and 'tensorflow'. Second, undated models count toward the year, and a NULL train_at fails every comparison you can write against it. On this data every undated model carries the 'PyTorch' label. A year filter alone quietly removes all of them, and the pytorch average comes out wrong while everything else looks fine.


Building it up

Step 1: Define the slice

Keep a model when the year of train_at is 2026 or when train_at is missing. The two halves of the OR cover different rows: the year test admits the dated models, and train_at IS NULL admits the migration rows the year test can never see.

Step 2: Fold the framework label

Wrap the label in LOWER(framework). The undated 'PyTorch' rows now land in the same bucket as the dated 'pytorch' rows, and the two TensorFlow spellings merge, so each framework comes out as one row.

Step 3: Group and average

GROUP BY LOWER(framework) with AVG(accuracy) AS avg_accuracy. AVG skips NULL accuracy, so an unscored model simply does not count, rather than being averaged in as zero.

Step 4: Order highest first

ORDER BY avg_accuracy DESC puts the strongest framework on top, which is the order the statement asks for.

The solution

Framework averages over the year's models
SELECT LOWER(framework) AS framework,
       AVG(accuracy) AS avg_accuracy
FROM ml_models
WHERE strftime('%Y', train_at) = '2026'
   OR train_at IS NULL
GROUP BY LOWER(framework)
ORDER BY avg_accuracy DESC

Common follow-up questions