Average Deal Size and Contract Totals by Customer Revenue Segment
Company: Salesforce
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Each customer falls into a revenue segment based on `annual_revenue`:
- `SMB`: `annual_revenue` below 1,000,000.
- `Mid-Market`: `annual_revenue` from 1,000,000 to 10,000,000, both ends included.
- `Enterprise`: `annual_revenue` above 10,000,000.
For each segment, return the number of distinct customers with contracts, the average contract value (the average deal size) and the total contract value.
```hint One label, used twice
Compute the segment label in one place, such as a CTE, so the grouping and the custom sort order read the same expression.
```
### Input Table(s)
`customers`
| Column | Type | Meaning |
|---|---|---|
| customer_id | INT | Unique customer identifier |
| customer_name | VARCHAR(100) | Customer name, never NULL |
| annual_revenue | DECIMAL(15,2) | Customer's annual revenue, never NULL |
| industry | VARCHAR(50) | Industry label, may be NULL |
Primary key: `customer_id`.
`contracts`
| Column | Type | Meaning |
|---|---|---|
| contract_id | INT | Unique contract identifier |
| customer_id | INT | Customer who signed the contract |
| contract_value | DECIMAL(15,2) | Value of the contract, never NULL |
| start_date | DATE | Contract start date, never NULL |
| end_date | DATE | Contract end date, never NULL |
Primary key: `contract_id`. Foreign key: `contracts.customer_id` references `customers.customer_id`.
### Output Contract
- Columns, in this order and with this exact capitalization: `Segment`, `CustomerCount`, `AvgDealSize`, `TotalContractValue`.
- `CustomerCount` is the number of distinct customers in the segment that have at least one contract.
- `AvgDealSize` is the mean `contract_value` over the segment's contracts, rounded to 2 decimal places.
- `TotalContractValue` is the sum of `contract_value` over the segment's contracts, rounded to 2 decimal places.
- One row per segment that has at least one contract.
- Rows ordered `SMB`, then `Mid-Market`, then `Enterprise`. Each segment appears at most once, so this order is unique.
### Example
Input `customers`:
| customer_id | customer_name | annual_revenue | industry |
|---|---|---|---|
| 1 | Acme Bakery | 250000.00 | Food |
| 2 | Brightline Labs | 1000000.00 | Biotech |
| 3 | Cobalt Logistics | 4500000.00 | Logistics |
| 4 | Delta Systems | 10000000.00 | Software |
| 5 | Evergreen Bank | 85000000.00 | Finance |
| 6 | Fable Studio | 600000.00 | Media |
Input `contracts`:
| contract_id | customer_id | contract_value | start_date | end_date |
|---|---|---|---|---|
| 101 | 1 | 12000.00 | 2024-01-01 | 2024-12-31 |
| 102 | 1 | 8000.00 | 2025-01-01 | 2025-12-31 |
| 103 | 2 | 50000.00 | 2024-03-01 | 2025-02-28 |
| 104 | 3 | 75000.00 | 2024-06-01 | 2025-05-31 |
| 105 | 4 | 120000.00 | 2024-02-01 | 2025-01-31 |
| 106 | 5 | 400000.00 | 2024-01-01 | 2026-12-31 |
| 107 | 5 | 250000.00 | 2025-01-01 | 2025-12-31 |
Expected output:
| Segment | CustomerCount | AvgDealSize | TotalContractValue |
|---|---|---|---|
| SMB | 1 | 10000.00 | 20000.00 |
| Mid-Market | 3 | 81666.67 | 245000.00 |
| Enterprise | 1 | 325000.00 | 650000.00 |
Acme Bakery is the only SMB customer with contracts. Fable Studio is also SMB but has none, so it is not counted. Brightline Labs and Delta Systems sit exactly on the boundaries and belong to Mid-Market, whose three contracts average 245000 / 3 = 81666.666..., rounded to 81666.67. Evergreen Bank's two contracts give the Enterprise row.
### Constraints and Clarifications
- `customers` has at most 10,000 rows and `contracts` at most 100,000 rows.
- `annual_revenue` and `contract_value` are always positive and never NULL.
- Every contract belongs to exactly one existing customer.
- Customers without contracts are not counted, and a segment none of whose customers has a contract is left out of the result.
- `AvgDealSize` weights every contract equally, so a customer with several contracts contributes each of them.
- Contract dates do not filter anything. Every contract in the table counts.
Overview: Write a SQL query that sorts customers into revenue segments and reports the customer count, average contract value and total contract value for each segment. It tests CASE-based bucketing with inclusive boundaries, distinct counting across a join, rounding and a custom sort order.