# Mid-Range Cost Allocations

> Not the cheapest. Not the priciest. The middle.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The FinOps team is auditing mid-tier cost allocations (amounts between 500 and 1,000 inclusive) and needs each entry displayed as a team-service label alongside the amount and region, ordered by amount.

## Worked solution and explanation

### Why this problem exists in real interviews

Working against cost_allocs, this problem tests query construction on the team_name and svc_name columns. Interviewers use it as a fundamentals check because a subtle mis-grouping or filter placement changes the output without raising an error.

---

### Break down the requirements

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

Use `BETWEEN` in the `WHERE` clause to select the target range. This is both readable and optimizable by the query planner.

#### Step 2: 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

**Concatenation label with BETWEEN filter**

```sql
SELECT team_name || '-' || svc_name AS team_service_label, amount, region
FROM cost_allocs
WHERE amount BETWEEN 500 AND 1000
ORDER BY amount
```

> **Cost Analysis**
>
> The query scans 10M rows from `cost_allocs`.

> **Interviewers Watch For**
>
> Interviewers expect you to articulate why you chose a specific join type and what happens to unmatched rows.

> **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

- If cost_allocs.alloc_id could contain unexpected NULL values, how would your query behave? _(Tests NULL awareness even when the schema does not currently allow NULLs in alloc_id.)_
- How would you verify that your aggregation on cost_allocs.alloc_id is not double-counting due to duplicate rows? _(Tests data quality awareness and deduplication strategies.)_
- With millions of distinct values in cost_allocs.alloc_id, what index strategy would you use to keep this query performant? _(Tests indexing knowledge specific to high-cardinality columns like alloc_id.)_

## Related

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