# Top Accuracy Model

> The single best-performing model.

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

Domain: SQL · Difficulty: medium · Seniority: L3

## Problem

Which model in the ML registry has the highest accuracy? Show the model name and accuracy.

## Worked solution and explanation

### Why this problem exists in real interviews

The `ml_models` table is the foundation for this filtering to the top rows after aggregation problem. It tests whether you can compose a CTE or subquery that aggregates before ranking, then filter to the desired slice.

---

### Break down the requirements

#### Step 1: Aggregate per model_id

`GROUP BY model_id` with the appropriate aggregate function produces one summary row per group from the `ml_models` table.

#### Step 2: Rank the results

`ORDER BY` the aggregate descending with `LIMIT` to surface the top entries.

---

### The solution

**Select model with highest accuracy from ml_models**

```sql
SELECT
    model_id,
    SUM(accuracy) AS total_accuracy
FROM ml_models
GROUP BY model_id
ORDER BY total_accuracy DESC
LIMIT 10
```

> **Cost Analysis**
>
> The GROUP BY reduces the 3K-row `ml_models` table to the number of distinct `model_id` values. A covering index on `(model_id, accuracy)` enables an index-only aggregate scan.

> **Interviewers Watch For**
>
> Interviewers verify you aggregate before sorting. Sorting raw rows gives per-row values, not group totals. The correct grain is one row per `model_id`.

> **Common Pitfall**
>
> Using the wrong aggregate function. `SUM` gives totals, `COUNT` gives volume, `AVG` gives rates. Read the prompt to determine which metric is needed.

---

## Common follow-up questions

- If two models share the exact same top accuracy, does LIMIT 1 arbitrarily pick one? How would you return both? _(Tests tie handling; WHERE accuracy = (SELECT MAX(accuracy)...) returns all ties.)_
- Would an index on accuracy DESC speed this query, and what type of scan does it become? _(Tests index-assisted top-1 retrieval via index-only scan.)_
- If accuracy values are stored to different decimal precisions (0.95 vs 0.9500), does that affect ordering? _(Tests numeric precision understanding; trailing zeros do not affect numeric comparison.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/top_accuracy_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.