RANK()

SQL · Window Functions

Theory

RANK() numbers rows by the window ORDER BY, but rows with equal values share the same rank, and the numbers after a tie are skipped. If Asha and Priya both earn 60000 and only Meera earns more, they are both rank 2, and the next person is rank 4, not 3. There is no rank 3 because two people occupy positions 2 and 3.

This is the 'sports ranking' style: if two runners tie for second, nobody is third. The rank value always equals 1 plus the number of rows that come strictly before this row. With PARTITION BY, ranking restarts in each group. Filtering WHERE rnk <= 3 can return more than 3 rows when there are ties, which is often exactly what you want for a fair 'top 3'. The query's ORDER BY direction matters: ORDER BY salary DESC ranks the highest salary as 1.

Why we use it

When ties are genuine, giving tied rows different numbers (as ROW_NUMBER does) is unfair and misleading. RANK treats equal values equally, and the gaps make it clear how many rows are ahead of you.

When to use it

Use RANK for leaderboards, exam results or sales contests where ties should share a place and gaps are expected. Use DENSE_RANK if you need consecutive numbers (like 'second highest salary'), or ROW_NUMBER if you need exactly one row per position.

How it works

  1. Partition is sorted by the window ORDER BY
  2. First row gets rank 1
  3. Same value as previous row? Yes: Same rank. No: Rank = row position (1 + rows before).
  4. Gaps appear after every tie
  5. Rank column added to each row

Syntax

RANK() OVER (ORDER BY col)

Example

SELECT name, salary,
  RANK() OVER (ORDER BY salary DESC) AS rnk
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