# Largest Single Cloud Cost

> One line item. The biggest bill of all.

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

Domain: SQL · Difficulty: medium · Seniority: L3

## Problem

What is the single largest cloud cost entry in the table? Show the service name and the amount.

## Worked solution and explanation

### Why this problem exists in real interviews

This focuses on top-N selection and date extraction within cloud_costs, specifically around the provider column. Interviewers present it in mid-level screens because the edge cases around NULL values and boundary conditions reveal depth of understanding.

> **Trick to Solving**
>
> Read the prompt carefully for implicit constraints. The phrase structure hints at the grain of the output: what each row represents.
> 
> 1. Identify the output grain from the prompt (one row per what?)
> 2. Work backward from the desired output columns
> 3. Build the query inside-out: innermost subquery first, then layer on filters and aggregates

---

### Break down the requirements

#### Step 1: Read from `cloud_costs`

The query targets `cloud_costs` with 7 columns. Identify which columns are needed for the output.

#### Step 2: Order and limit the output

Sort by the target metric and apply `LIMIT` to return the requested number of rows. Ensure the sort is deterministic to produce reproducible results.

#### Step 3: Return the result set

Select the required columns with any necessary aliasing or formatting.

---

### The solution

**Direct sort for single maximum row**

```sql
SELECT cost_id, provider, svc_name, region, amount, acct_id, bill_date
FROM cloud_costs
ORDER BY amount DESC
LIMIT 1
```

> **Cost Analysis**
>
> The query scans 15M rows from `cloud_costs`.

> **Interviewers Watch For**
>
> Candidates who verbalize their approach before typing, naming the output columns and expected row count, consistently perform better.

> **Common Pitfall**
>
> Returning more columns than the prompt asks for can trigger a "wrong schema" failure in automated grading. Match the output specification exactly.

---

## Common follow-up questions

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

## Related

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