# Service Roll Call

> The mesh is sprawling. Find out exactly how many services are actually running.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The SRE team wants a quick count of how many unique services are being monitored by health checks.

## Worked solution and explanation

### Why this problem exists in real interviews

This is the simplest COUNT(DISTINCT) query. It screens for whether you know how to count unique values in a column.

---

### Break down the requirements

#### Step 1: Count unique services

`COUNT(DISTINCT svc_name)` returns the number of unique service names in the table.

---

### The solution

**Simple distinct count**

```sql
SELECT COUNT(DISTINCT svc_name) AS unique_services
FROM svc_health
```

> **Cost Analysis**
>
> Scan of 15M rows with hash-based distinct counting. Single-row output. The hash set for distinct service names is small (thousands at most).

> **Interviewers Watch For**
>
> Whether the candidate uses COUNT(DISTINCT) vs a subquery with SELECT DISTINCT. Both work; COUNT(DISTINCT) is more concise.

> **Common Pitfall**
>
> Using COUNT(svc_name) without DISTINCT counts total non-NULL values (15M), not unique services. The DISTINCT keyword is essential.

---

## Common follow-up questions

- What is the difference between COUNT(DISTINCT svc_name) and COUNT(svc_name)? _(DISTINCT counts unique values; without it, counts all non-NULL occurrences.)_
- How would you approximate the distinct count for very large tables? _(Tests HyperLogLog: APPROX_COUNT_DISTINCT in some databases.)_
- What if svc_name has NULLs? _(COUNT(DISTINCT) ignores NULLs. Tests NULL awareness.)_

## Related

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