Latest Migration Output per Author

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
L4

Problem

The release manager wants the latest migration each author shipped. For every author in the migrations table, find their most recent migration by applied date, breaking ties in favor of the largest migration ID. Return the author, version, and applied timestamp, sorted alphabetically by author.

Each author's most recent migration output.

Tables

migrations

Columns: migr_id INTEGER, version TEXT, status TEXT, applied TEXT, rollback TEXT, dur_ms INTEGER, author TEXT, db_name TEXT

migrations: 10 of 200 rows
migr_idversionstatusappliedrollbackdur_msauthordb_name
123002_add_userspending2026-02-02 01:00:00DROP TABLE IF EXISTS tbl_141bobstaging_main
146003_add_ordersfailed2026-03-03 02:00:00DROP TABLE IF EXISTS tbl_272charlieanalytics
169004_indexesrolled_back2026-04-04 03:00:00DROP TABLE IF EXISTS tbl_3103danareporting
192005_add_productsApplied2026-05-05 04:00:00DROP TABLE IF EXISTS tbl_4134migration-botarchive
215006_add_sessionsPENDINGNULLDROP TABLE IF EXISTS tbl_5NULLAliceprod_main
238007_add_eventsapplied2026-07-07 06:00:00DROP TABLE IF EXISTS tbl_6196alicestaging_main
261008_add_searchpending2026-08-08 07:00:00NULL227bobanalytics
284v009_paymentsfailed2026-09-09 08:00:00DROP TABLE IF EXISTS tbl_8258charliereporting
307V010_analyticsrolled_back2026-01-10 09:00:00DROP TABLE IF EXISTS tbl_9289danaarchive
330001_initAppliedNULLDROP TABLE IF EXISTS tbl_10NULLmigration-botprod_main

Expected output

First 5 of 6 rows
authorversionapplied
AliceV010_analytics2026-09-06 17:00:00
alice005_add_products2026-07-25 00:00:00
charliev009_payments2026-09-09 08:00:00
dana002_add_users2026-07-24 03:00:00
migration-bot003_add_orders2026-08-25 04:00:00

Worked solution and explanation

What this really is

Strip off the release-management costume and this is the latest-row-per-group problem, the most common interview shape in all of SQL. You are not aggregating anything: you want the WHOLE migration row that happens to be the newest one for each author. That distinction is the entire test. Everyone reaches for GROUP BY author, but the moment you do, you can keep the MAX of applied and you LOSE the version that belongs to it. An aggregate collapses the group; it cannot hand you the other columns from the winning row.

One table. The grain is one row per migration; you want the newest row per author value.
  • migrations: migr_id int PK, version text, status text, applied timestamp, author text, dur_ms int

The trap

Aggregate then self-join (fragile)
GROUP BY author to get MAX(applied), then join back to migrations on (author, applied) to fetch version. Works until a tie on applied returns two rows, double-counting the author, with no clean way to break the tie inside the join.
Rank then filter (clean)
Number the rows within each author by recency, then keep number 1. The tie-break lives inside the ORDER BY, so a duplicate timestamp resolves deterministically and the author still appears exactly once.

The build

Step 1: Number rows within each author by recency

ROW_NUMBER() OVER (PARTITION BY author ORDER BY applied DESC, migr_id DESC). PARTITION BY author restarts the count for every author, so each author's newest migration gets rn = 1. Note authors are case-sensitive here: Alice and alice partition separately, which is exactly what the data intends.

Step 2: Make the tie-break deterministic

The prompt says break ties by the largest migration ID, so the second ORDER BY key is migr_id DESC. Without it, two migrations sharing an applied timestamp would get rn 1 and 2 in an undefined order, and your output would flip between runs. This is the difference between a query that passes once and one you would trust in production.

Step 3: Keep the winners and sort the result

Wrap the numbered set in a subquery (window functions cannot live in a WHERE clause directly) and filter rn = 1. Then ORDER BY author for the alphabetical output the release manager asked for. Do not confuse the inner recency ordering with this outer presentation ordering; they are two different sorts doing two different jobs.

Latest migration per author
SELECT author, version, applied
FROM (
  SELECT author, version, applied, migr_id,
         ROW_NUMBER() OVER (PARTITION BY author ORDER BY applied DESC, migr_id DESC) AS rn
  FROM migrations
) t
WHERE rn = 1
ORDER BY author

One pass over migrations, one window, no join, no fan-out.

Common follow-up questions