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
| 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
| model_id | mdl_name | accuracy |
|---|---|---|
| 951 | sentiment | 0.99 |
| 1986 | churn_pred | 0.99 |
| 10002437 | sentiment | 0.99 |
| 10002482 | churn_pred | 0.99 |
| 767 | sentiment | 0.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.
SELECT model_id, mdl_name, accuracy
FROM ml_models
ORDER BY accuracy DESC, model_id ASC
LIMIT 10SELECT model_id, mdl_name, accuracy
FROM ml_models
ORDER BY accuracy DESC
LIMIT 10With one key, which 2 of the four 0.97 models fill slots 9 and 10 is up to the engine.
Common follow-up questions
- What if you needed the top model per `framework`? (Tests `PARTITION BY framework` with `ROW_NUMBER()` instead of a global `LIMIT`.)
- Should tied models share a position, so that every model at 0.97 appears? (Tests `DENSE_RANK()` versus `ROW_NUMBER()` and a result size that varies.)
- How would you exclude NULL `accuracy` rows outright rather than rely on how NULLs sort? (Tests `WHERE accuracy IS NOT NULL` before the sort.)