# Top Deployed Model

> The best-performing model in production.

Canonical URL: <https://datadriven.io/problems/top_deployed_model>

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

Which deployed model has the highest accuracy? Show its name, accuracy, and framework.

## Worked solution and explanation

### What this is really asking

status = 'deployed' is the only filter that matters. Of 2,000 ml_models rows, return one: the deployed row with the highest accuracy, columns mdl_name, accuracy, framework.

---

### Break down the requirements

#### Step 1: Filter to deployed

WHERE status = 'deployed'. Other values like training or archived drop out; exact string match, case sensitive on most engines.

#### Step 2: Rank by accuracy

ORDER BY accuracy DESC plus LIMIT 1. No tiebreak rule was given.

#### Step 3: Return three columns

mdl_name, accuracy, framework. Not model_id, not version.

---

### The solution

**TOP DEPLOYED MODEL**

```sql
SELECT
  mdl_name,
  accuracy,
  framework
FROM ml_models
WHERE status = 'deployed'
ORDER BY accuracy DESC
LIMIT 1;
```

> **Cost Analysis**
>
> 2,000 rows means a sequential scan is fine. A composite index on (status, accuracy DESC) lets the engine skip to the top deployed row, but the win is negligible at this size.

> **Interviewers Watch For**
>
> Filtering in WHERE not HAVING, returning exactly the requested columns, and recognizing ORDER BY DESC plus LIMIT 1 as the top-of-one idiom.

> **Common Pitfall**
>
> Putting status = 'deployed' in HAVING. HAVING runs after GROUP BY; with no aggregation it belongs in WHERE so the optimizer prunes rows early.

> **The False Start**
>
> First instinct is WHERE accuracy = MAX(accuracy) AND status = 'deployed'. Aggregates are not allowed in WHERE, and the MAX would not even be scoped to deployed. Pivot to ORDER BY accuracy DESC LIMIT 1.

---

### COMMON FOLLOW-UP QUESTIONS

## Common follow-up questions

- What if two deployed models tie on accuracy? _(LIMIT 1 picks one arbitrarily. Use RANK() OVER (ORDER BY accuracy DESC) and keep rnk = 1.)_
- How would you return the top model per framework? _(ROW_NUMBER() OVER (PARTITION BY framework ORDER BY accuracy DESC), filter rn = 1.)_
- What if accuracy can be NULL? _(NULLs sort differently per engine. Add NULLS LAST or WHERE accuracy IS NOT NULL.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/top_deployed_model)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.