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
| 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 | release_version | avg_accuracy |
|---|---|---|
| pytorch | 1.0 | 0.8 |
| pytorch | 2.1 | 0.84 |
| tensorflow | 1.0 | 0.78 |
| tensorflow | 1.1 | 0.78 |
| xgboost | 2.0 | 0.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.
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.1compares 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
- A new label like 'v2.0-rc1' shows up. What does this query do with it, and what should it do? (Tests whether the candidate sees that the cleanup only knows the suffixes it was told about, so the cast breaks on the next one.)
- How would you compare '1.10' against '1.9' correctly? (Tests awareness that casting a dotted version to a single number breaks multi-part semver ordering.)
- The team now wants only shipped models, and `status` holds both 'deployed' and 'Deployed'. What changes? (Tests whether the candidate applies the same case-normalisation instinct to a filter column.)