Top 10 Model Accuracies

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
ESPN

Problem

We keep a registry of ML models and want a leaderboard of the 10 most accurate, most accurate first, showing each model's name and its accuracy. Where accuracy ties, the model that was registered first ranks ahead.

When the scores tie at the top, someone still has to be first.

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 10 rows
model_idmdl_nameaccuracy
951sentiment0.99
1986churn_pred0.99
10002437sentiment0.99
10002482churn_pred0.99
767sentiment0.98

Worked solution and explanation

What this is really testing

This is a deterministic top-N dressed up as a model leaderboard. Anyone can write ORDER BY accuracy DESC LIMIT 10. The trap is that accuracy repeats: 4 models share 0.99, 4 share 0.98, and 4 more share 0.97, so the tenth slot lands in the middle of the 0.97 group and only 2 of those 4 make the cut. Sort on accuracy alone and the engine picks those 2 by whatever order it read the rows in. Your leaderboard can then change between runs and miss the expected rows.

Step 1: Sort by accuracy from highest down

The leaderboard reads best first, so accuracy DESC is the primary key. Keep model_id in the output next to mdl_name. Names repeat across versions (2 'sentiment' entries sit at 0.99), and the id is the only way to tell them apart.

Step 2: Break ties with model_id ASC

The statement says the model registered first wins a tie. Ids are issued in registration order, so the lower model_id goes first. model_id is unique, so both keys together leave no tie anywhere and the cutoff is well defined.

Step 3: Cut with LIMIT 10 last

LIMIT takes the first 10 rows of the order above it. With a total order, the same 10 come back every run: the 8 models at 0.99 and 0.98, then models 583 and 1618 from the 0.97 group.

Top 10 by accuracy with a stable tiebreak
SELECT model_id, mdl_name, accuracy
FROM ml_models
ORDER BY accuracy DESC, model_id ASC
LIMIT 10
The version that looks right
SELECT model_id, mdl_name, accuracy
FROM ml_models
ORDER BY accuracy DESC
LIMIT 10

With one key, which 2 of the four 0.97 models fill slots 9 and 10 is up to the engine.

Common follow-up questions