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
| 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
| 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
SELECT AVG(accuracy) AS avg_accuracy
FROM ml_models
WHERE accuracy * 100 BETWEEN 91 AND 100Common follow-up questions
- The accuracy column has roughly 2 percent NULLs. Where do those rows go in your query, and how would the answer change if NULL accuracy were treated as 0? (Tests whether the candidate understands that both the range predicate and AVG already exclude NULLs, and how forcing them to zero would drag the average down.)
- A model has accuracy exactly 0.91. Does it make the cut, and how would you rewrite the filter if the band were meant to be strictly above 91? (Tests understanding of boundary inclusivity and how BETWEEN differs from strict comparisons.)
- The team now wants this average broken out by framework instead of one overall number. How does the query change? (Tests whether the candidate can extend a single scalar into a grouped breakdown while preserving the same filter.)
- If accuracy were indexed, would accuracy * 100 BETWEEN 91 AND 100 use that index, and how might you rewrite the predicate so it can? (Tests awareness of index usability: wrapping a column in an expression can prevent an index on accuracy from being used.)