Compute Each Customer's Spend Percentile with PERCENT_RANK and CUME_DIST

Quick Overview

Write a SQL query that gives every customer a percent rank and a cumulative distribution value based on total spend across the whole table. It tests the PERCENT_RANK and CUME_DIST window functions, how ties share values, and rounding window results in PostgreSQL.

Compute Each Customer's Spend Percentile with PERCENT_RANK and CUME_DIST

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

For every customer, report where their total spend sits in the distribution of all customers. Return `customer_id`, `total_spend`, `percent_rank` (from `PERCENT_RANK()`) and `cume_dist` (from `CUME_DIST()`), both computed over the whole table ordered by `total_spend`. ```hint Check the return type In PostgreSQL, look at what type these window functions return before you pass the result to `ROUND` with a number of decimal places. ``` ### Input Table(s) `customer_spend` | Column | Type | Meaning | |---|---|---| | customer_id | INT | Customer identifier | | total_spend | DECIMAL(10,2) | The customer's total spend (never `NULL`) | Key: `customer_id` is the primary key. ### Output Contract Return these columns in this order, one row per customer: | Column | Rule | |---|---| | customer_id | The customer identifier | | total_spend | The customer's total spend, unchanged | | percent_rank | (rank - 1) / (total rows - 1), where rank is the standard `RANK()` by `total_spend` ascending; a value in [0, 1]; rounded to 2 decimals | | cume_dist | (rows with `total_spend` less than or equal to this row's) / (total rows); a value in (0, 1]; rounded to 2 decimals | Customers with equal `total_spend` get the same `percent_rank` and the same `cume_dist`. Sort by `total_spend` ascending, then by `customer_id` ascending. ### Example `customer_spend` | customer_id | total_spend | |---|---| | 1 | 100.00 | | 2 | 100.00 | | 3 | 300.00 | | 4 | 300.00 | Expected result: | customer_id | total_spend | percent_rank | cume_dist | |---|---|---|---| | 1 | 100.00 | 0.00 | 0.50 | | 2 | 100.00 | 0.00 | 0.50 | | 3 | 300.00 | 0.67 | 1.00 | | 4 | 300.00 | 0.67 | 1.00 | The two 100.00 customers share rank 1, so their percent rank is (1 - 1) / 3 = 0, and 2 of the 4 rows are at or below 100.00, giving a cumulative distribution of 0.50. The two 300.00 customers share rank 3, so their percent rank is (3 - 1) / 3, which rounds to 0.67, and all 4 rows are at or below 300.00. ### Constraints and Clarifications - The table has 1 to 10,000 rows. - `customer_id` is unique and never `NULL`. - `total_spend` is a non-negative decimal and never `NULL`. - With a single row, `percent_rank` is 0 and `cume_dist` is 1. - The ranking covers the whole table; there is no grouping.

Overview: Write a SQL query that gives every customer a percent rank and a cumulative distribution value based on total spend across the whole table. It tests the PERCENT_RANK and CUME_DIST window functions, how ties share values, and rounding window results in PostgreSQL.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

For every customer, report where their total spend sits in the distribution of all customers. Return customer_id, total_spend, percent_rank (from PERCENT_RANK()) and cume_dist (from CUME_DIST()), both computed over the whole table ordered by total_spend.

Input Table(s)

customer_spend

ColumnTypeMeaning
customer_idINTCustomer identifier
total_spendDECIMAL(10,2)The customer's total spend (never NULL)

Key: customer_id is the primary key.

Output Contract

Return these columns in this order, one row per customer:

ColumnRule
customer_idThe customer identifier
total_spendThe customer's total spend, unchanged
percent_rank(rank - 1) / (total rows - 1), where rank is the standard RANK() by total_spend ascending; a value in [0, 1]; rounded to 2 decimals
cume_dist(rows with total_spend less than or equal to this row's) / (total rows); a value in (0, 1]; rounded to 2 decimals

Customers with equal total_spend get the same percent_rank and the same cume_dist.

Sort by total_spend ascending, then by customer_id ascending.

Example

customer_spend

customer_idtotal_spend
1100.00
2100.00
3300.00
4300.00

Expected result:

customer_idtotal_spendpercent_rankcume_dist
1100.000.000.50
2100.000.000.50
3300.000.671.00
4300.000.671.00

The two 100.00 customers share rank 1, so their percent rank is (1 - 1) / 3 = 0, and 2 of the 4 rows are at or below 100.00, giving a cumulative distribution of 0.50. The two 300.00 customers share rank 3, so their percent rank is (3 - 1) / 3, which rounds to 0.67, and all 4 rows are at or below 300.00.

Constraints and Clarifications

  • The table has 1 to 10,000 rows.
  • customer_id is unique and never NULL .
  • total_spend is a non-negative decimal and never NULL .
  • With a single row, percent_rank is 0 and cume_dist is 1.
  • The ranking covers the whole table; there is no grouping.