Output

EmployeeSalaryRow_Number
Amit800001
Rahul750002
Priya750003
Neha700004

Key Point

Even though Rahul and Priya have the same salary, they receive different row numbers.

When to Use

Use ROW_NUMBER() when every row must have a unique position.

Examples:

Agar hume har row ko ek unique number dena ho, chahe values same hi kyu na ho, tab hum ROW_NUMBER() ka use karte hain. Maan lo do employees ki salary same hai, phir bhi ROW_NUMBER() un dono ko alag-alag numberdega. Is function ka use tab kiya jata hai jab hume har record ki unique position chahiye hoti hai, jaise pagination, duplicate records remove karna ya top records nikalna.

2. RANK()

Definition

RANK() gives the same rank to duplicate values, but skips the next rank.

Syntax

SELECT
Employee,
Salary,
RANK() OVER (ORDER BY Salary DESC) AS Salary_Rank

FROM Employees;

Output

EmployeeSalaryRank
Amit800001
Rahul750002
Priya750002
Neha700004

Notice that Rank 3 is skipped.

Key Point

Duplicate values receive the same rank.

The next rank is skipped.

Agar hume kisi data ko highest se lowest ya lowest se highest order me arrange karke uski position nikalni ho, tab hum RANK() function ka use karte hain.Agar do records ki value same ho, to dono ko same rank milti hai aur uske baad wali rank automatically skip ho jati hai.


3. DENSE_RANK()

Definition

DENSE_RANK() also gives the same rank to duplicate values, but it never skips the next rank.

Syntax

SELECT
Employee,
Salary,
DENSE_RANK() OVER (ORDER BY Salary DESC) AS Dense_Rank

FROM Employees;

Output

Employee Salary Dense Rank
Amit 80000 1
Rahul 75000 2
Priya 75000 2
Neha 70000 3

Unlike RANK(), there is no missing rank.

Agar hume kisi data ki ranking nikalni ho aur duplicate values ko same rank deni ho, lekin uske baad wali ranking ko skip na karna ho, tab hum DENSE_RANK() ka use karte hain.Maan lo kisi company me employees ko salary ke basis par rank deni hai. Agar do employees ki salary same hai, to dono ko same rank milegi. Lekin RANK() ki tarah next rank skip nahi hogi.Isliye jab hume continuous ranking chahiye hoti hai, tab DENSE_RANK() sabse best choice hoti hai.


Comparison Table

FunctionDuplicate ValuesRank SkippedUnique Numbers
ROW_NUMBER()❌ No❌ No✅ Yes
RANK()✅ Yes✅ Yes❌ No
DENSE_RANK()✅ Yes❌ No❌ No

Real Business Scenarios

🏥 Hospital Analytics

Find the highest-paid doctors in each department.

🏦 Financial Fraud Analytics

Rank customers based on total fraudulent transaction amount.

Customers with equal fraud amounts may receive the same rank using RANK() or DENSE_RANK().


🍽️ Zomato Analytics

Rank restaurants by rating within each city.

If two restaurants have the same rating:


Common Mistake

Many beginners think these three functions produce the same output.

They only behave differently when duplicate values exist.

If all values are unique, all three functions return identical rankings.


Interview Question

Q1. What is the difference between RANK() and DENSE_RANK()?

Answer:


Q2. Which ranking function always generates unique numbers?

Answer:

ROW_NUMBER()


Q3. Which function is commonly used for pagination?

Answer:

ROW_NUMBER()


Quick Revision

Need Function
Unique numbering ROW_NUMBER()
Same rank with skipped numbers RANK()
Same rank without skipped numbers DENSE_RANK()

Conclusion

Although ROW_NUMBER(), RANK(), and DENSE_RANK() are all ranking functions, each serves a different purpose.

Understanding these differences will help you write better SQL queries and answer interview questions with confidence.


Ranks on the basic of Window function
Salary
80000
│
├── Amit
│
│ ROW_NUMBER() = 1
│ RANK() = 1
│ DENSE_RANK() = 1
75000
│
├── Rahul
│
│ ROW_NUMBER() = 2
│ RANK() = 2
│ DENSE_RANK() = 2
├── Priya
│
│ ROW_NUMBER() = 3
│ RANK() = 2
│ DENSE_RANK() = 2
70000
│
├── Neha
│
│ ROW_NUMBER() = 4
│ RANK() = 4
│ DENSE_RANK() = 3
👤
Jatin Kumar
Data Analyst & Educator

Python, SQL, Power BI aur Excel mein practical tutorials likhta hoon — taaki data analytics seekhna aasan ho. Portfolio: jatinanalytics.co.in

Share:

💬 Comments (0)

Spam/links allowed nahi hain — respectful comments welcome!

Loading comments...

Was this article helpful?