# First Migration Record

> The very first migration. Where it all began.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

Pull the very first migration ever applied to the schema, identified by the smallest migration ID. Show all available fields: migration ID, version, status, applied date, rollback info, duration, author, and database name.

## Worked solution and explanation

### Why this problem exists in real interviews

The `migrations` schema makes this a clean test of row numbering within partitions combined with nested subqueries. Columns like `version`, `status`, `applied` introduce enough ambiguity that only candidates who clarify assumptions produce correct results.

---

### Break down the requirements

#### Step 1: Partition by `db_name`

`PARTITION BY db_name` creates groups. Within each group, `ORDER BY applied ASC` determines the ranking.

#### Step 2: Filter to rank 1

`WHERE rnk = 1` in the outer query selects the target row per group.

---

### The solution

**Row-number for first migration record**

```sql
SELECT *
FROM (
    SELECT *,
           ROW_NUMBER() OVER (PARTITION BY db_name ORDER BY applied ASC) AS rnk
    FROM migrations
) ranked
WHERE rnk = 1
ORDER BY db_name
```

> **Cost Analysis**
>
> Window function sorts within each `db_name` partition. An index on `(db_name, applied)` avoids a full sort.

> **Interviewers Watch For**
>
> The interviewer checks whether you use ROW_NUMBER (one row) vs. RANK/DENSE_RANK (ties) based on the prompt requirements.

> **Common Pitfall**
>
> Using GROUP BY with MIN(applied) gives the value but not the other columns. ROW_NUMBER gives the full row.

---

## Common follow-up questions

- The `rollback` column in `migrations` has roughly 70% NULLs. How does your query handle those rows, and would the result change if NULLs were replaced with zeros? _(Tests whether the candidate understands how NULLs propagate through aggregation functions and whether their WHERE/JOIN conditions implicitly filter them out.)_
- Your window function uses a default frame. What is the implicit frame, and would switching to ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW change anything? _(Tests knowledge of default window frames (RANGE vs ROWS) and when the distinction matters.)_
- The `version` column in `migrations` has a zipf distribution, meaning a few values dominate. How does that skew affect your query plan and parallelism? _(Tests understanding of data skew: the optimizer may choose a bad plan when histogram statistics are stale.)_
- How would you modify this query to run incrementally as new rows arrive in `migrations` each hour? _(Tests whether the candidate can think about incremental computation vs full recomputation.)_

## Related

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