# Zero-Retry Job Ratio by Priority

> No retries needed. First try success rate.

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

Domain: SQL · Difficulty: hard · Seniority: L4

## Problem

The batch processing system tracks retry counts per job. Calculate the ratio of jobs that completed without any retries to total jobs, broken down by priority level. A zero-retry job has retries equal to 0. Show the priority, count of zero-retry jobs, total jobs for that priority, and the ratio, from lowest ratio to highest.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests conditional aggregation with ratio computation per group. Interviewers check whether you can compute a subset count relative to a total count within each GROUP BY partition.

---

### Break down the requirements

#### Step 1: Group by priority level

`GROUP BY priority` partitions the 500K jobs into 5 priority levels.

#### Step 2: Count zero-retry jobs and total jobs

`SUM(CASE WHEN retries = 0 THEN 1 ELSE 0 END)` counts zero-retry jobs. `COUNT(*)` counts all jobs per priority.

#### Step 3: Compute and sort the ratio

Divide zero-retry count by total count for the ratio. `ORDER BY ratio ASC` shows lowest ratio first.

---

### The solution

**Conditional aggregation for subset ratio per group**

```sql
SELECT
    priority,
    SUM(CASE WHEN retries = 0 THEN 1 ELSE 0 END) AS zero_retry_count,
    COUNT(*) AS total_jobs,
    SUM(CASE WHEN retries = 0 THEN 1 ELSE 0 END) * 1.0 / COUNT(*) AS ratio
FROM batch_jobs
GROUP BY priority
ORDER BY ratio ASC
```

> **Cost Analysis**
>
> Full scan of 500K rows, grouped into 5 priorities. The CASE expression evaluates per row. Trivially fast with no joins or subqueries needed.

> **Interviewers Watch For**
>
> Using `CASE WHEN retries = 0` inside an aggregate rather than a separate subquery. This is more efficient and idiomatic than filtering and counting in separate passes.

> **Common Pitfall**
>
> Integer division. `SUM(...) / COUNT(*)` truncates to 0 for most groups. Multiply by `1.0` to force decimal division.

---

## Common follow-up questions

- How would you also show the average retry count per priority? _(Add AVG(retries) to the SELECT.)_
- What if you needed to exclude jobs that are still running (status = 'running')? _(Add WHERE status != 'running' to filter before aggregation.)_
- How would you find priorities where more than 80% of jobs required retries? _(Add HAVING to filter on the ratio: HAVING ratio < 0.2 (zero-retry ratio below 20%).)_
- What if retries could be NULL for some jobs? _(NULL != 0 in SQL, so NULL-retry jobs would not be counted as zero-retry. Use COALESCE(retries, 0) if NULL means zero.)_

## Related

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