# First Build per Repository

> Every repo had a first build.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The CI/CD team is mapping out when each repository first started running builds. Show each repo alongside its earliest build date, ordered chronologically then alphabetically by repo name.

## Worked solution and explanation

### Why this problem exists in real interviews

Interviewers use the `ci_builds` table here to probe row numbering within partitions combined with nested subqueries. The columns `repo_name`, `branch`, `status` force candidates to reason about the correct grain before writing any aggregation.

---

### Break down the requirements

#### Step 1: Partition by `repo_name`

`PARTITION BY repo_name` creates groups. Within each group, `ORDER BY built_at 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 build repository**

```sql
SELECT *
FROM (
    SELECT *,
           ROW_NUMBER() OVER (PARTITION BY repo_name ORDER BY built_at ASC) AS rnk
    FROM ci_builds
) ranked
WHERE rnk = 1
ORDER BY repo_name
```

> **Cost Analysis**
>
> Window function sorts within each `repo_name` partition. An index on `(repo_name, built_at)` 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(built_at) gives the value but not the other columns. ROW_NUMBER gives the full row.

---

## Common follow-up questions

- The `dur_secs` column in `ci_builds` has roughly 2% 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.)_
- `build_id` in `ci_builds` has ~2M distinct values. What index strategy keeps your query from doing a full table scan? _(Tests whether the candidate can design indexes for high-cardinality columns and understands selectivity.)_
- If `build_id` in `ci_builds` contained negative values, would your query still produce correct results? _(Tests whether the candidate validated assumptions about the domain of numeric columns.)_

## Related

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