# Health Checks per Service

> Some services get checked constantly.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

Before onboarding a new monitoring vendor, the SRE team needs to quantify current coverage by showing how many health checks have been recorded per service.

## Worked solution and explanation

### Why this problem exists in real interviews

The core skill being tested is grouped aggregation over `svc_health`. Candidates must decide how `svc_name`, `status`, `latency` interact before choosing a join strategy or aggregation level.

---

### Break down the requirements

#### Step 1: Group by `svc_name`

`GROUP BY svc_name` produces one row per distinct value.

#### Step 2: Compute the count

`COUNT(*)` calculates the requested metric per group.

---

### The solution

**Group-aggregate for health checks service**

```sql
SELECT svc_name, COUNT(*) AS result
FROM svc_health
GROUP BY svc_name
ORDER BY result DESC
```

> **Cost Analysis**
>
> Single-pass hash aggregate. An index on `svc_name` helps if the table is large.

> **Interviewers Watch For**
>
> The interviewer checks that you use the correct aggregate function (COUNT) based on the prompt.

> **Common Pitfall**
>
> Selecting a column not in GROUP BY and not in an aggregate is a syntax error in strict SQL mode.

---

## Common follow-up questions

- The `latency` column in `svc_health` has roughly 1% 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 GROUP BY aggregates `check_id` from `svc_health`. If two groups have the same aggregate value, how is the output ordered, and is that deterministic? _(Tests awareness that ORDER BY on a non-unique value produces non-deterministic row order without a tiebreaker.)_
- `check_id` in `svc_health` has ~18M 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 the business definition of `status` changed mid-quarter (e.g., a status value was renamed), how would you handle historical consistency? _(Tests awareness of slowly changing dimensions and backward-compatible query design.)_

## Related

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