Ranking functions in SQL are essential tools for performing advanced row-level calculations. They assign ranks, numbers, or relative positions to rows within a result set, based on specified criteria.
Ranking functions are window functions that compute a rank or position for each row within a partition of data. Unlike aggregate functions, which collapse data into single results, ranking functions retain row-level details, making them ideal for scenarios such as sorting, categorization, and pagination.
Assigns a unique number to each row, starting at 1 for the first row within each partition.
Assigns ranks to rows. Ties (rows with the same value) receive the same rank, and subsequent ranks are skipped.
Similar to RANK
, but ranks are not skipped after ties.
Divides the result set into n
equal parts (tiles) and assigns a tile number to each row.
All ranking functions share the following general syntax:
function_name()
OVER (
[PARTITION BY column_name]
[ORDER BY column_name]
)
function_name
: The ranking function (ROW_NUMBER
, RANK
, DENSE_RANK
, NTILE
).OVER
: Indicates the function operates on a window of rows.PARTITION BY
: Divides the data into subsets (optional).ORDER BY
: Specifies the order of rows within each partition.The PARTITION BY
clause divides the data into independent subsets. Each subset is ranked separately.
The ORDER BY
clause defines the order of rows within each partition, affecting the ranking computation.
SELECT EmployeeID, Name, Salary,
ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RowNumber
FROM Employees;
EmployeeID | Name | Salary | RowNumber |
---|---|---|---|
101 | Alice | 80000 | 1 |
102 | Bob | 75000 | 2 |
103 | Charlie | 70000 | 3 |
The ROW_NUMBER
function assigns unique row numbers based on salary in descending order.
SELECT EmployeeID, Name, Salary,
RANK() OVER (ORDER BY Salary DESC) AS Rank
FROM Employees;
EmployeeID | Name | Salary | Rank |
---|---|---|---|
101 | Alice | 80000 | 1 |
102 | Bob | 75000 | 2 |
103 | Charlie | 70000 | 3 |
The RANK
function assigns ranks to rows. If two rows have the same value, they share the same rank, and the next rank is skipped.
SELECT EmployeeID, Name, Salary,
DENSE_RANK() OVER (ORDER BY Salary DESC) AS DenseRank
FROM Employees;
EmployeeID | Name | Salary | DenseRank |
---|---|---|---|
101 | Alice | 80000 | 1 |
102 | Bob | 75000 | 2 |
103 | Charlie | 70000 | 3 |
The DENSE_RANK
function assigns ranks without skipping numbers after ties.
SELECT EmployeeID, Sales,
SUM(Sales) OVER (ORDER BY EmployeeID) AS RunningTotal
FROM sales;
EmployeeID | Name | Salary | TileNumber |
---|---|---|---|
101 | Alice | 80000 | 1 |
102 | Bob | 75000 | 1 |
103 | Charlie | 70000 | 2 |
The NTILE
function divides the rows into two tiles (groups). Rows are distributed evenly among the tiles.
ROW_NUMBER
to implement pagination in web applications.RANK
or DENSE_RANK
to display rankings with ties.NTILE
to split data into equal-sized groups.PARTITION BY
and ORDER BY
for better performance.NTILE
carefully, as uneven data distributions may lead to skewed group sizes.Ranking functions in SQL provide a powerful way to analyze and organize data. By mastering their syntax and usage, you can solve complex ranking and grouping problems with ease. Their flexibility and efficiency make them indispensable for modern data analysis. Happy Coding!❤️