# Latest Version Per Service

> The latest version deployed. Each service.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The release dashboard needs to reflect the current state of every service. Show each service alongside the latest version that was deployed to it.

## Worked solution and explanation

### Why this problem exists in real interviews

The deploy_logs table contains svc_name and version values that must be processed with row numbering. This appears as a fundamentals check to probe whether you reason about the correct aggregation grain before writing any window or GROUP BY clause.

> **Trick to Solving**
>
> Keeping the most recent row per group is a classic `ROW_NUMBER` pattern.
> 
> 1. `ROW_NUMBER() OVER (PARTITION BY group_col ORDER BY ts DESC)` assigns 1 to the latest row
> 2. Wrap in a subquery or CTE
> 3. Filter to `rn = 1`

---

### Break down the requirements

#### Step 1: Filter to the target rows

Apply the `WHERE` filter to restrict the working set before aggregation. Filtering early reduces the number of rows that downstream operations process.

#### Step 2: Assign row numbers for deduplication

`ROW_NUMBER() OVER (PARTITION BY ... ORDER BY ... DESC)` tags each row within its group. The outer query filters to `rn = 1` to keep only the target row.

#### Step 3: Order the final output

Apply `ORDER BY` as specified to produce the expected row sequence. When tied values exist, add a secondary sort column for determinism.

---

### The solution

**ROW_NUMBER for latest version per service**

```sql
SELECT svc_name, version
FROM (
    SELECT svc_name, version,
        ROW_NUMBER() OVER (PARTITION BY svc_name ORDER BY deploy_at DESC) AS rn
    FROM deploy_logs
) sub
WHERE rn = 1
```

> **Cost Analysis**
>
> The query scans 500K rows from `deploy_logs`.

> **Interviewers Watch For**
>
> Interviewers expect you to articulate why you chose a specific join type and what happens to unmatched rows. Explaining why `ROW_NUMBER` is preferred over `DISTINCT` for deduplication shows you understand the difference between collapsing and selecting.

> **Common Pitfall**
>
> Forgetting that a JOIN can multiply rows when the relationship is one-to-many. Always check whether the join key is unique on at least one side.

---

## Common follow-up questions

- What happens to your result if deploy_logs.dur_secs contains NULLs for some rows? _(Tests whether the candidate accounts for NULL behavior in aggregates and comparisons on dur_secs.)_
- If two rows in deploy_logs have identical values in the ORDER BY columns, how does your ranking handle the tie? _(Tests understanding of RANK vs DENSE_RANK vs ROW_NUMBER tie-breaking behavior.)_
- With millions of distinct values in deploy_logs.log_id, what index strategy would you use to keep this query performant? _(Tests indexing knowledge specific to high-cardinality columns like log_id.)_

## Related

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