PERCENT_RANK()

SQL · Window Functions

Theory

PERCENT_RANK() shows where a row stands within its partition as a number from 0 to 1. The formula is (rank - 1) / (rows in partition - 1), where rank is the same value RANK() would give. The first row in the ORDER BY always gets 0 and the last distinct value gets 1 (unless it is tied).

With five salaries 45000, 52000, 60000, 60000, 75000 ordered ascending, the ranks are 1, 2, 3, 3, 5, so PERCENT_RANK is 0, 0.25, 0.5, 0.5 and 1. Read 0.5 as 'half of the other rows are below me'. Tied rows get the same value because they share a RANK. With only one row in a partition, the result is 0. Multiply by 100 for a percentile-style score, and remember the direction: ORDER BY salary DESC would give the top earner 0.

Why we use it

Raw ranks are hard to compare across groups of different sizes: rank 5 out of 6 and rank 5 out of 500 mean very different things. PERCENT_RANK scales every rank to 0-1 so you can compare positions fairly.

When to use it

Use PERCENT_RANK to express relative standing, such as 'in the top 10% of sellers' (value >= 0.9 with ascending order). Use CUME_DIST if you want 'share of rows at or below me'; use NTILE if you want fixed buckets.

How it works

  1. Rows sorted by the window ORDER BY
  2. Compute RANK() for the current row
  3. Count rows in the partition (N)
  4. Is N equal to 1? Yes: Return 0. No: Return (rank - 1) / (N - 1).
  5. Value between 0 and 1 for each row

Syntax

PERCENT_RANK() OVER (ORDER BY col)

Example

SELECT name, salary,
  PERCENT_RANK() OVER (ORDER BY salary) AS pct_rank
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