Top Deployed Model
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
- Forbes
Problem
In the ml_models registry, only a status of exactly 'deployed' in lowercase marks a live model; 'Deployed' and every other spelling are separate states. For the ML platform team's production review, return the name, accuracy and framework of the single most accurate live model.
The best-performing model in production.
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
| mdl_name | accuracy | framework |
|---|---|---|
| churn_pred | 0.97 | PyTorch |
Worked solution and explanation
What this is really asking
This is a filter-then-top-one with a casing trap built into the filter. Anyone can sort ml_models by accuracy and take the first row. What separates candidates is treating 'deployed' as an exact, case-sensitive value. The registry also holds 'Deployed', and one of those rows scores 0.98, above every live model. Fold the casing with LOWER(status) and you report that 0.98 sentiment model as the production leader when it was never live.
Step 1: Keep only exact 'deployed'
WHERE status = 'deployed' compares the stored string as written. Do not wrap the column in LOWER() or switch to ILIKE: the statement already told you 'Deployed' is a separate state, so the cleanup reflex changes which row wins.
Step 2: Sort by accuracy, best first, take one
ORDER BY accuracy DESC then LIMIT 1. NULLs sort as the smallest value here, so the unscored models fall to the bottom of a descending sort and never land on top.
Step 3: Return the 3 requested columns
mdl_name, accuracy, framework. Leave out model_id and version: the column names are the output contract.
The solution
SELECT
mdl_name,
accuracy,
framework
FROM ml_models
WHERE status = 'deployed'
ORDER BY accuracy DESC
LIMIT 1;SELECT
mdl_name,
accuracy,
framework
FROM ml_models
WHERE LOWER(status) = 'deployed'
ORDER BY accuracy DESC
LIMIT 1;LOWER(status) merges 'Deployed' into the live set and returns the 0.98 sentiment model, which was never live.
Common follow-up questions
Common follow-up questions
- What if 2 different deployed models tie on accuracy? (`LIMIT 1` picks one arbitrarily. Use `RANK() OVER (ORDER BY accuracy DESC)` and keep rank 1.)
- How would you return the top model per framework? (`ROW_NUMBER() OVER (PARTITION BY framework ORDER BY accuracy DESC)`, then filter to 1.)
- What if the team later decides 'Deployed' should count as live? (Then `LOWER(status) = 'deployed'` becomes correct; the business rule decides, and the data only reflects it.)