CUME_DIST()

SQL · Window Functions

Theory

CUME_DIST() is the cumulative distribution: the fraction of rows in the partition whose ORDER BY value is less than or equal to the current row's value. The formula is (number of rows with value <= this value) / (rows in partition). The result is always greater than 0 and at most 1, and the last value always gets exactly 1.

For salaries 45000, 52000, 60000, 60000, 75000 the results are 0.2, 0.4, 0.8, 0.8 and 1. Both 60000 rows get 0.8 because four of the five rows are at or below 60000; ties count all their peers. Compare that with PERCENT_RANK on the same data (0, 0.25, 0.5, 0.5, 1): PERCENT_RANK asks 'what share of the others are strictly below me', while CUME_DIST asks 'what share of everyone is at or below me'. A value of 0.8 reads naturally as 'this salary is at the 80th percentile'.

Why we use it

Questions like 'what fraction of orders are under ₹2000' or 'is this salary in the top 20%' are about distribution. CUME_DIST gives that share directly on every row, so you can filter or label rows by percentile.

When to use it

Use CUME_DIST for 'at or below' percentile statements and to keep tied values together. Use PERCENT_RANK for a 0-based relative rank, and NTILE when you need fixed equal-sized buckets.

How it works

  1. Rows sorted by the window ORDER BY
  2. Count rows in the partition (N)
  3. Count rows with value <= current value, ties included
  4. Divide that count by N
  5. Value in (0, 1] for each row; the last value gets 1

Syntax

CUME_DIST() OVER (ORDER BY col)

Example

SELECT name, salary,
  CUME_DIST() OVER (ORDER BY salary) AS cume_dist
FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic