Rank and Dense_Rank in SQL Tutorial W3Schools Guide

SQL, a standard language for managing and manipulating relational databases, offers a variety of functions for ranking and grouping data. Among these are the RANK() and DENSE_RANK() functions, which are particularly useful for ordering the results of a query based on specific criteria. In this article, we will delve into the functionalities, differences, and use cases of RANK() and DENSE_RANK() with examples, to help you navigate through the endless possibilities that these functions offer.

SQL DENSE_RANK() Explained with Examples | Beginner SQL Window Function Tutorial
SQL DENSE_RANK() Explained with Examples | Beginner SQL Window Function Tutorial

Before we dive into the specifics of RANK() and DENSE_RANK(), let's establish a basic understanding of how these functions work. Both functions are used to rank the data based on one or more columns. However, they handle ties differently, which we will explore further in the following sections.

SQL RANK() Explained with Examples | Beginner SQL Window Function Tutorial
SQL RANK() Explained with Examples | Beginner SQL Window Function Tutorial

RANK() Function

The RANK() function in SQL assigns a unique rank to each row within the result set, including ties. It means that if there are duplicate values in the column used for ranking, the function will assign the same rank to those duplicate values and then skip the next rank(s) to maintain a unique sequence.

SQL Window Functions Explained with Examples | Beginner SQL Tutorial
SQL Window Functions Explained with Examples | Beginner SQL Tutorial

For instance, consider a table 'Students' with the following data:

NameScore
Alice90
Bob90
Charlie80
David80
Eve70
Sql Cheat Sheet, Learn Sql, Learn Web Development, Excel Tutorials, Sql Server, Career Options, Data Analyst, Data Scientist, Data Analysis
Sql Cheat Sheet, Learn Sql, Learn Web Development, Excel Tutorials, Sql Server, Career Options, Data Analyst, Data Scientist, Data Analysis

Ranking Scores

Let's first rank the scores using the RANK() function. The query would look like the following:

SELECT Name, Score, RANK() OVER (ORDER BY Score DESC) AS Rankstood
FROM Students;

In this result set, both Alice and Bob will have a rank of 1 due to their tie in scores. Charlie and David will have a rank of 3, skipping the 2nd rank because of the tie at the top:

a notepad with some writing on it that says, what is row - number?
a notepad with some writing on it that says, what is row - number?
NameScoreRank
Alice901
Bob901
Charlie803
David803
Eve705

DENSE_RANK() Function

On the other hand, the DENSE_RANK() function also assigns unique ranks to each row within the result set, but unlike RANK(), it does not skip ranks when there are ties. It maintains a continuous sequence of integer values, regardless of any duplicate values in the column used for ranking. Let's apply the DENSE_RANK() function to the same 'Students' table:

SQL in 60 days
SQL in 60 days

Ranking Scores Densely

The query would look like the following:

7 SQL Projects for Beginners | Build a Real Data Portfolio
7 SQL Projects for Beginners | Build a Real Data Portfolio
a table with the names and numbers for different types of items in front of it
a table with the names and numbers for different types of items in front of it
SQL Basics Every Beginner Must Know | Database, Table, Row & Column Explained
SQL Basics Every Beginner Must Know | Database, Table, Row & Column Explained
SQL Table Structure Explained for Beginners πŸ“Š
SQL Table Structure Explained for Beginners πŸ“Š
#sql #sqlfordataanalysis #dataanalytics #learnsql #datascience #sqltips #database | Ruth Y.
#sql #sqlfordataanalysis #dataanalytics #learnsql #datascience #sqltips #database | Ruth Y.
Learn SQL Fast πŸš€ | Beginner SQL Commands Cheat Sheet for Data Analysts
Learn SQL Fast πŸš€ | Beginner SQL Commands Cheat Sheet for Data Analysts
SQL Nested subquery - w3resource
SQL Nested subquery - w3resource
SQL Window Functions Explained πŸ“Š
SQL Window Functions Explained πŸ“Š
an image of a table with numbers and tables for each piece in the game, which is
an image of a table with numbers and tables for each piece in the game, which is
167K views Β· 467 reactions | Brought to you by programmingvalley  #SQL #database | Programming Valley
167K views Β· 467 reactions | Brought to you by programmingvalley #SQL #database | Programming Valley
What Is a Database in SQL? | SQL Database for Beginners
What Is a Database in SQL? | SQL Database for Beginners
SQL Mindmap for Beginners & Pros πŸ“Š | Master Queries, Joins, DDL, DML Fast
SQL Mindmap for Beginners & Pros πŸ“Š | Master Queries, Joins, DDL, DML Fast
Level Up Coding on LinkedIn: SQL, NoSQL, or something else β€” How do you decide which database? The…
Level Up Coding on LinkedIn: SQL, NoSQL, or something else β€” How do you decide which database? The…
SQL Clauses Guide (WHERE, GROUP BY, ORDER BY)
SQL Clauses Guide (WHERE, GROUP BY, ORDER BY)
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL JOINS Made SIMPLE πŸ§ πŸ”΅ Blue Visual Guide for Beginners
SQL JOINS Made SIMPLE πŸ§ πŸ”΅ Blue Visual Guide for Beginners
SQL Joins Cheat Sheet Every Developer Should Save
SQL Joins Cheat Sheet Every Developer Should Save
2.4K views Β· 102 reactions | 🎯 SQL Cheatsheet – Everything You Need at a Glance Master SQL fundamentals with this clean and powerful SQL Cheatsheet πŸš€ Perfect for beginners, developers, data analysts, and inte | Codewithprashantt
2.4K views Β· 102 reactions | 🎯 SQL Cheatsheet – Everything You Need at a Glance Master SQL fundamentals with this clean and powerful SQL Cheatsheet πŸš€ Perfect for beginners, developers, data analysts, and inte | Codewithprashantt
Learn SQL just by investing 30 minutes a day within 30 Days
Learn SQL just by investing 30 minutes a day within 30 Days

SELECT Name, Score, DENSE_RANK() OVER (ORDER BY Score DESC) AS DenseRank
FROM Students;

In this result set, although Alice and Bob have the same score, they will have consecutive ranks: 1 and 2, respectively. Charlie and David still get ranks 3 and 4:

NameScoreDenseRank
Alice901
Bob902
Charlie803
David804
Eve705

Now that we have explored the differences between RANK() and DENSE_RANK() functions, you can choose the function that best suits your needs, depending on whether you want to account for ties in your ranking or not.

The ability to rank and order data is a powerful feature in SQL and can help transform your raw data into meaningful and actionable insights. Consider exploring the other ranking functions, such as ROW_NUMBER() and NTILE(), to gain a full understanding of SQL's capabilities. Happy data analysis!

Related Articles

W3schools Sql Remove Row W3 Xslt W3 Sql Procedures Difference Between Mysql And Sql Server Management Studio Trigger Use In Sql Ms Sql Server Management Studio Query How To Use Pivot In Sql Create Table Sql W3schools Trigger Sql Server W3schools Types Of Sql Transactions