What is ROW_NUMBER in SQL?
ROW_NUMBER is a SQL window function used to assign a unique sequential number to each row in a result set.
In simple words:
ROW_NUMBER generates a unique row number for every row based on sorting conditions.
Why ROW_NUMBER is Important
Enterprise applications frequently require:
- Ranking systems
- Pagination
- Duplicate removal
- Top-N queries
- Report generation
ROW_NUMBER helps:
- Assign unique sequence numbers
- Order rows systematically
- Perform advanced analytics
- Implement pagination efficiently
Simple Real-Life Example
Think about:
- Students standing in exam ranking order
Each student gets:
- Unique position number
Example
1 → Naresh 2 → Rahul 3 → Arjun 4 → David
ROW_NUMBER Internal Architecture
Select Rows
|
v
Sort Rows
|
v
Assign Sequential Numbers
|
v
Return Rows with Row Numbers
ROW_NUMBER Syntax
SELECT column_name,
ROW_NUMBER() OVER (
ORDER BY column_name
) AS row_num
FROM table_name;
Employees Table Example
| Employee | Salary |
|---|---|
| Naresh | 90000 |
| Rahul | 70000 |
| Arjun | 85000 |
| David | 60000 |
ROW_NUMBER Query
SELECT employee_name,
salary,
ROW_NUMBER() OVER (
ORDER BY salary DESC
) AS row_num
FROM employees;
Result
| Employee | Salary | ROW_NUMBER |
|---|---|---|
| Naresh | 90000 | 1 |
| Arjun | 85000 | 2 |
| Rahul | 70000 | 3 |
| David | 60000 | 4 |
How ROW_NUMBER Works
Step 1
Rows sorted using:
ORDER BY salary DESC
Step 2
Sequential numbers assigned:
1, 2, 3, 4
Important Point
ROW_NUMBER always assigns:
- Unique numbers
even if duplicate values exist.
Duplicate Value Example
| Employee | Salary |
|---|---|
| Naresh | 90000 |
| Rahul | 70000 |
| Arjun | 70000 |
ROW_NUMBER Result
| Employee | Salary | ROW_NUMBER |
|---|---|---|
| Naresh | 90000 | 1 |
| Rahul | 70000 | 2 |
| Arjun | 70000 | 3 |
Important Observation
Even though Rahul and Arjun have same salary:
- Different row numbers assigned
ROW_NUMBER Query Flow
Read Rows
|
v
Sort Rows
|
v
Assign Sequential Number
|
v
Return Final Result
PARTITION BY with ROW_NUMBER
PARTITION BY divides:
- Rows into groups
Department-wise Ranking Example
SELECT employee_name,
department,
salary,
ROW_NUMBER() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS dept_rank
FROM employees;
What Happens?
ROW_NUMBER restarts:
- For every department
Example Result
| Employee | Department | Salary | Dept Rank |
|---|---|---|---|
| Naresh | IT | 90000 | 1 |
| Rahul | IT | 70000 | 2 |
| Arjun | HR | 85000 | 1 |
Difference Between ROW_NUMBER, RANK, and DENSE_RANK
| Function | Duplicate Handling | Skips Numbers |
|---|---|---|
| ROW_NUMBER | Always unique | No |
| RANK | Same rank for duplicates | Yes |
| DENSE_RANK | Same rank for duplicates | No |
Example Comparison
Marks:
95 90 90 85
ROW_NUMBER Result
1 2 3 4
RANK Result
1 2 2 4
DENSE_RANK Result
1 2 2 3
Common Use Cases of ROW_NUMBER
- Pagination
- Top-N records
- Duplicate removal
- Latest record selection
1. Pagination Example
SELECT *
FROM (
SELECT employee_name,
salary,
ROW_NUMBER() OVER (
ORDER BY salary DESC
) AS row_num
FROM employees
) temp
WHERE row_num BETWEEN 1 AND 10;
Purpose
Retrieve:
- First 10 rows
2. Duplicate Removal Example
WITH duplicate_rows AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY email
ORDER BY id
) AS row_num
FROM users
)
DELETE FROM duplicate_rows
WHERE row_num > 1;
What Happens?
Keeps:
- First occurrence
Deletes:
- Duplicate rows
3. Latest Record Selection
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY order_date DESC
) AS row_num
FROM orders
) latest_orders
WHERE row_num = 1;
Purpose
Retrieve:
- Latest order for each customer
ROW_NUMBER vs LIMIT
| Feature | ROW_NUMBER | LIMIT |
|---|---|---|
| Supports Partitioning | Yes | No |
| Advanced Analytics | Yes | No |
| Pagination | Advanced | Basic |
Real-Time Banking Example
Banking systems use ROW_NUMBER for:
- Top customer ranking
- Latest transaction tracking
- Fraud analysis
Example
Find latest transaction per customer.
Real-Time E-Commerce Example
E-commerce platforms use ROW_NUMBER for:
- Top-selling products
- Order tracking
- Customer analytics
Example
Retrieve latest order per customer.
Real-Time Learning Platform Example
Learning platforms use ROW_NUMBER for:
- Student rankings
- Leaderboard systems
- Top performers
Example
Find top 3 students per course.
ROW_NUMBER in Microservices
Microservices use ROW_NUMBER for:
- Analytics APIs
- Dashboard reporting
- Pagination services
Advantages of ROW_NUMBER
- Unique row numbering
- Supports advanced analytics
- Useful for pagination
- Efficient duplicate handling
Disadvantages of ROW_NUMBER
- Requires sorting operations
- May impact performance on large datasets
Performance Consideration
ROW_NUMBER requires:
- Sorting
- Partitioning
which may:
- Increase execution cost
Best Practices
- Use indexes on ORDER BY columns
- Optimize partition sizes
- Use ROW_NUMBER for pagination carefully
- Avoid unnecessary sorting
Common Interview Mistake
Many developers think:
- ROW_NUMBER behaves like RANK
Reality
ROW_NUMBER always assigns:
- Unique sequential numbers
even for duplicate values.
Related Learning Topics
- What are Window Functions in SQL?
- Difference Between RANK and DENSE_RANK
- What is PARTITION BY in SQL?
- What is GROUP BY in SQL?
- MySQL Performance Optimization
Professional Interview Answer
ROW_NUMBER is a SQL window function that assigns a unique sequential number to each row in a result set based on specified sorting conditions. It is commonly used with the OVER clause along with ORDER BY and optionally PARTITION BY clauses. Unlike RANK and DENSE_RANK, ROW_NUMBER always generates unique numbers even when duplicate values exist. ROW_NUMBER is widely used for pagination, duplicate removal, top-N queries, latest record retrieval, and advanced analytics in enterprise applications such as banking systems, e-commerce platforms, reporting dashboards, and microservices-based analytical APIs.
Why Interviewers Like This Answer
- Clearly explains sequential numbering
- Includes OVER and PARTITION BY concepts
- Shows window function expertise
- Provides enterprise-level examples
- Explains ROW_NUMBER vs RANK differences
Frequently Asked Questions
What is ROW_NUMBER in SQL?
ROW_NUMBER is a window function that assigns unique sequential numbers to rows.
Does ROW_NUMBER handle duplicates?
Yes, but it still assigns unique row numbers to duplicate rows.
Can ROW_NUMBER use PARTITION BY?
Yes, PARTITION BY restarts numbering for each group.
What is ROW_NUMBER commonly used for?
Pagination, duplicate removal, latest record selection, and ranking systems.
What is the difference between ROW_NUMBER and RANK?
ROW_NUMBER always generates unique numbers, while RANK assigns same rank to duplicates.