Average Deal Size and Contract Totals by Customer Revenue Segment

Quick 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.

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.

Salesforce logo
Salesforce
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

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.

Input Table(s)

customers

ColumnTypeMeaning
customer_idINTUnique customer identifier
customer_nameVARCHAR(100)Customer name, never NULL
annual_revenueDECIMAL(15,2)Customer's annual revenue, never NULL
industryVARCHAR(50)Industry label, may be NULL

Primary key: customer_id.

contracts

ColumnTypeMeaning
contract_idINTUnique contract identifier
customer_idINTCustomer who signed the contract
contract_valueDECIMAL(15,2)Value of the contract, never NULL
start_dateDATEContract start date, never NULL
end_dateDATEContract 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_idcustomer_nameannual_revenueindustry
1Acme Bakery250000.00Food
2Brightline Labs1000000.00Biotech
3Cobalt Logistics4500000.00Logistics
4Delta Systems10000000.00Software
5Evergreen Bank85000000.00Finance
6Fable Studio600000.00Media

Input contracts:

contract_idcustomer_idcontract_valuestart_dateend_date
101112000.002024-01-012024-12-31
10218000.002025-01-012025-12-31
103250000.002024-03-012025-02-28
104375000.002024-06-012025-05-31
1054120000.002024-02-012025-01-31
1065400000.002024-01-012026-12-31
1075250000.002025-01-012025-12-31

Expected output:

SegmentCustomerCountAvgDealSizeTotalContractValue
SMB110000.0020000.00
Mid-Market381666.67245000.00
Enterprise1325000.00650000.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.