First and Last Peak Accuracy Dates

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L3
Asked in
Vesta Innovations

Problem

Every training run lands in ml_models with its accuracy, and we want to know when our best-ever accuracy was first reached and whether any run has matched it since. Show the earliest and latest training dates among the runs that hit that top score.

Peak accuracy. When it first hit and when it last did.

Tables

ml_models

Columns: model_id INTEGER, mdl_name TEXT, version TEXT, accuracy REAL, status TEXT, train_at TEXT, 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 1 of 1 rows
first_datelast_date
2023-01-10 13:00:002026-02-27 10:00:00

Worked solution and explanation

This is a filter-by-extremum question wearing a model-registry costume. Anyone can write MAX(accuracy). The trap is writing MIN(train_at) and MAX(train_at) right beside it and trusting that the dates belong to the peak runs. They don't: every aggregate in a flat SELECT scans all 200 rows on its own, so the dates you get back are the whole registry's range, reaching into 2022, and you tell the team the peak was hit years before it was. The other shortcut, sorting by accuracy and keeping the top row, returns one date and silently drops the half of the question that matters: whether the peak was ever matched again.

Two passes over one table

Step 1: Fix the peak once

The peak is one number over every run, not one per mdl_name: (SELECT MAX(accuracy) FROM ml_models). Nothing in it refers to the outer row, so the engine computes it a single time and treats it as a constant.

Step 2: Keep only the runs that hit it

Compare accuracy to that constant in WHERE. On this data four runs share the top score of 0.99, two trained in 2023 and two in 2026, and all four have to survive the filter: that spread is exactly what 'matched it since' is asking about.

Step 3: Read the span off the survivors

Now MIN(train_at) and MAX(train_at) only see the peak runs. train_at is 'YYYY-MM-DD HH:MM:SS' text, so string order is time order: the minimum is the first time the peak was reached, the maximum the latest time it was matched.

Peak first, then the span
SELECT MIN(train_at) AS first_date, MAX(train_at) AS last_date FROM ml_models WHERE accuracy = (SELECT MAX(accuracy) FROM ml_models)

The subquery fixes the peak; the outer MIN and MAX read dates from the peak runs only.

The flat pass that looks right
SELECT MAX(accuracy) AS peak_accuracy,
       MIN(train_at) AS first_date,
       MAX(train_at) AS last_date
FROM ml_models

Three independent aggregates: the dates come from every run in ml_models, not the peak runs.

Common follow-up questions