# Top 2 Rate-Limited Clients

> Two clients are hitting the rate limit harder than anyone.

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

Domain: SQL · Difficulty: medium · Seniority: L3

## Problem

The API gateway team is cracking down on abusive traffic patterns. Find the two clients generating the most blocked requests, where a request counts as blocked when the blocked value is greater than 0. Show each client and their blocked count, sorted from most blocked to least.

## Worked solution and explanation

### Why this problem exists in real interviews

Querying `rate_limits` for top 2 rate-limited clients requires filtering to the top rows after aggregation. Interviewers watch for whether the candidate aggregates first or tries to rank raw rows, which is the most common mistake.

---

### Break down the requirements

#### Step 1: Apply filters

Use a `WHERE` clause to narrow the data to the relevant subset before aggregation.

#### Step 2: Aggregate per client

`GROUP BY client` with the appropriate aggregate function produces one summary row per group from the `rate_limits` table.

#### Step 3: Rank the results

`ORDER BY` the aggregate descending with `LIMIT` to surface the top entries.

---

### The solution

**Sum blocked requests per client from rate_limits and take top 2**

```sql
SELECT
    client,
    SUM(checked) AS total_checked
FROM rate_limits
GROUP BY client
ORDER BY total_checked DESC
LIMIT 2
```

> **Cost Analysis**
>
> The GROUP BY reduces the 3M-row `rate_limits` table to the number of distinct `client` values. A covering index on `(client, checked)` enables an index-only aggregate scan.

> **Interviewers Watch For**
>
> Interviewers verify you aggregate before sorting. Sorting raw rows gives per-row values, not group totals. The correct grain is one row per `client`.

> **Common Pitfall**
>
> Using the wrong aggregate function. `SUM` gives totals, `COUNT` gives volume, `AVG` gives rates. Read the prompt to determine which metric is needed.

---

## Common follow-up questions

- If blocked can be negative (representing credits or reversals), does your SUM still reflect actual blocked volume? _(Tests defensive aggregation; the candidate might need to filter for blocked > 0 rows only.)_
- What if two clients tie for second place? Does LIMIT 2 still show both? _(Tests awareness that LIMIT truncates ties; DENSE_RANK or FETCH WITH TIES is needed for tie inclusion.)_
- How would you extend this to show each client's most-blocked endpoint alongside their total? _(Tests ability to add a correlated subquery or window function for per-group detail.)_

## Related

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