The Elite

An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
easy
Seniority
L3
Asked in
Vesta Innovations

Problem

The ML team is deciding whether to retrain the highest-accuracy models or leave them alone. Accuracy is stored as a decimal between 0 and 1. Compute the average accuracy across all models whose accuracy, when expressed as a percentage, falls between 91 and 100 inclusive.

The top-scoring models. What's their average?

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 1 of 1 rows
avg_accuracy
0.9500000000000001

Worked solution and explanation

What this really is

This is a single-column range filter wearing an ML-evaluation costume. The only real skill being probed: can you apply a range check to a metric that is stored on one scale but described on another? Accuracy lives as a 0-to-1 decimal, but the ask is phrased in percentage points, 91 to 100. The candidates who stumble compare the raw decimal against 91 and 100, so nothing ever clears the bar and the query returns NULL. Scale first, then filter, then average. Miss the scaling and you confidently hand back an empty result.


Building the query

Step 1: Filter to the high-accuracy band

Accuracy is a 0-to-1 decimal, so multiply it by 100 to put it on the percentage scale the prompt speaks in, then keep only the rows where that value sits in the 91-to-100 band. BETWEEN is inclusive on both ends, which matches the 'inclusive' wording exactly. NULL accuracy rows fail this predicate silently, so they drop out here with no extra handling.

Step 2: Average the survivors

Apply AVG(accuracy) over the surviving rows. Note you average the original decimal accuracy, not the scaled value; the scaling was only a means to express the filter. AVG ignores any NULLs by definition, so the average is over genuine measured models.

Step 3: Name the result

Alias the aggregate as avg_accuracy so the single scalar comes back under a stable, readable name instead of a raw expression string.


The solution

Average accuracy of the top band
SELECT AVG(accuracy) AS avg_accuracy
FROM ml_models
WHERE accuracy * 100 BETWEEN 91 AND 100

Common follow-up questions