Average Accuracy by Framework

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
ESPN

Problem

The framework accuracy comparison has lost the ML platform team's trust: it lists PyTorch and pytorch as different frameworks, counts the 3.0 models and shows releases under their raw labels, because version in ml_models carries a 'v' prefix, a '-beta' suffix or an extra '.0' inconsistently. Show each framework's average accuracy for each release from 1.0 through 2.1 inclusive, with the framework in lowercase, the release written plainly so 'v2.1-beta' reads as '2.1' and '1.0.0' as '1.0', and the average rounded to 2 decimal places.

Not all frameworks deliver the same accuracy.

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 5 of 5 rows
frameworkrelease_versionavg_accuracy
pytorch1.00.8
pytorch2.10.84
tensorflow1.00.78
tensorflow1.10.78
xgboost2.00.81

Worked solution and explanation

What this really is

This is a data-cleaning problem wearing a dashboard costume. The average is the easy part. The real test is whether you notice that version is a label, and a messy one: 'v1.1', '1.0.0', '3.0' and 'v2.1-beta' all live in one text column. The comparison has to report each model under the release it shipped as and keep only 1.0 through 2.1. Clean only the 'v' and the output still shows '1.0.0' and '2.1-beta' as releases of their own. Filter the raw text and every prefixed release drops out, because 'v1.1' sorts after '2.1'.

The quieter trap is the framework name. The same framework was logged as 'PyTorch' and 'pytorch', 'TensorFlow' and 'tensorflow'. Group on framework as stored and the comparison shows 4 names for 2 frameworks. Nothing errors; it just looks like more data.

Step 1: Clean the label once

Strip the 'v' prefix, the '-beta' suffix and the extra '.0' with 3 nested REPLACE calls, inside a derived table, so the cleaned release_version is written once and read by the filter, the grouping and the output. 'v2.1-beta' becomes '2.1' and '1.0.0' becomes '1.0'. Each edit shows up in the result: skip one and a release appears under a label the team never shipped.

Step 2: Cast the clean label for the window

CAST(release_version AS REAL) BETWEEN 1.0 AND 2.1 compares numbers, inclusive on both ends: it keeps the 2.1 beta and drops '3.0'. The cast is safe only because the label is clean first. A strict engine refuses CAST('2.1-beta' AS REAL) outright, and a lenient one quietly reads whatever leading digits it finds.

Step 3: Group on one spelling of each framework

Lowercase framework in the same derived table and group on it together with release_version, so 'PyTorch' and 'pytorch' both report as 'pytorch', one row per release.

Step 4: Average and round

ROUND(AVG(accuracy), 2) gives the 2-place average. AVG skips the models whose accuracy is missing, which is the right reading: an unscored model has no accuracy to contribute.

Average accuracy per framework and release, 1.0 through 2.1
SELECT framework,
       release_version,
       ROUND(AVG(accuracy), 2) AS avg_accuracy
FROM (
    SELECT LOWER(framework) AS framework,
           REPLACE(REPLACE(REPLACE(version, 'v', ''), '-beta', ''), '.0.0', '.0') AS release_version,
           accuracy
    FROM ml_models
) AS model_releases
WHERE CAST(release_version AS REAL) BETWEEN 1.0 AND 2.1
GROUP BY framework, release_version
Filtering the label as stored
version BETWEEN '1.0' AND '2.1' compares text: every 'v'-prefixed release sorts above '2.1' and falls out, and the rows that survive still carry labels like '1.0.0'.
Cleaning, then casting
CAST(release_version AS REAL) BETWEEN 1.0 AND 2.1 compares values on a label that is already clean, so 'v1.1' is 1.1, the 2.1 beta is in and '3.0' is out.

Common follow-up questions