-
DAYS
-
HOURS
-
MINUTES
-
SECONDS

Free Beginners SQL Course!

Working with ROW_NUMBER, RANK, and DENSE_RANK

·

·

Introduction

Window functions are an essential tool for data analysis, particularly in the SQL language. They allow you to perform complex calculations, like running totals, moving averages, and ranking values, on a set of data, without the need for subqueries or self-joins. In this article, we’ll explore three popular ranking functions in SQL: ROW_NUMBER(), RANK(), and DENSE_RANK().

ROW_NUMBER()

The ROW_NUMBER() function is a window function that assigns a unique number to each row in the result set. It’s the simplest of the three ranking functions, as it generates a sequence of integers, starting at 1, for each row within a defined partition. The partition is defined by the “partition by” clause, which allows you to divide the result set into groups, such as by date, department, or region. The order of the rows within each partition is determined by the “order by” clause.

For example, the following SQL query assigns a unique row number to each team, based on the number of points they’ve scored, within their conference:

select Conference, Team, Points,
row_number() over(partition by Conference order by Points desc) RowNum
from Teams

If you’re up to learning more, check out our SQL Percentile Rank Puzzle

RANK()

The RANK() function is similar to the ROW_NUMBER() function, but it assigns the same rank to two or more rows if they have the same value. In other words, if two teams have the same number of points, they’ll both receive the same rank, and the next rank will be skipped. The result is that some ranks will be skipped, and there will be gaps in the sequence of ranks.

For example, the following SQL query assigns a rank to each team, based on the number of points they’ve scored, within their conference:

select Conference, Team, Points,
    rank() over(partition by Conference order by Points desc) Ranking
from Teams

DENSE_RANK()

The DENSE_RANK() function is similar to the RANK() function, but it assigns the same rank to two or more rows if they have the same value, and there are no gaps in the sequence of ranks. In other words, if two teams have the same number of points, they’ll both receive the same rank, and the next rank will be assigned to the next team, regardless of their points.

For example, the following SQL query assigns a dense rank to each team, based on the number of points they’ve scored, within their conference:

select Conference, Team, Points,
    dense_rank() over(partition by Conference order by Points desc) DenseRanking
from Teams

Conclusion

In conclusion, ROW_NUMBER(), RANK(), and DENSE_RANK() are all window functions in SQL that are used to add a sequential number to the rows of a result set, based on the sorting criteria specified. ROW_NUMBER() is used to simply add a unique, incremental number to each row, regardless of duplicates. RANK() and DENSE_RANK() are used to assign a rank to each row, based on the values of one or more columns, with RANK() assigning unique values, and DENSE_RANK() assigning the same rank to duplicates and filling in the gaps in the sequence. By using these functions, you can add meaningful numbers to your data, making it easier to compare and analyze the results.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More from the blog


MySQL PostgreSQL SQLite SQL Server