0
0
SQLquery~20 mins

RANK and DENSE_RANK difference in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rank Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Difference in output between RANK() and DENSE_RANK()

Consider the following table Scores with columns Player and Points:

Player | Points
-------|-------
Alice  | 100
Bob    | 90
Carol  | 90
Dave   | 80

What is the output of the following query?

SELECT Player, Points, RANK() OVER (ORDER BY Points DESC) AS RankPos
FROM Scores;
SQL
CREATE TABLE Scores (Player VARCHAR(10), Points INT);
INSERT INTO Scores VALUES
('Alice', 100), ('Bob', 90), ('Carol', 90), ('Dave', 80);

SELECT Player, Points, RANK() OVER (ORDER BY Points DESC) AS RankPos
FROM Scores;
A
Alice 100 1
Bob 90 3
Carol 90 3
Dave 80 4
B
Alice 100 1
Bob 90 2
Carol 90 2
Dave 80 4
C
Alice 100 1
Bob 90 2
Carol 90 2
Dave 80 3
D
Alice 100 1
Bob 90 2
Carol 90 3
Dave 80 4
Attempts:
2 left
💡 Hint

RANK() assigns the same rank to ties but skips ranks after ties.

query_result
intermediate
2:00remaining
Output of DENSE_RANK() with ties

Using the same Scores table, what is the output of this query?

SELECT Player, Points, DENSE_RANK() OVER (ORDER BY Points DESC) AS DenseRankPos
FROM Scores;
SQL
SELECT Player, Points, DENSE_RANK() OVER (ORDER BY Points DESC) AS DenseRankPos
FROM Scores;
A
Alice 100 1
Bob 90 2
Carol 90 3
Dave 80 4
B
Alice 100 1
Bob 90 3
Carol 90 3
Dave 80 4
C
Alice 100 1
Bob 90 2
Carol 90 2
Dave 80 3
D
Alice 100 1
Bob 90 2
Carol 90 2
Dave 80 4
Attempts:
2 left
💡 Hint

DENSE_RANK() assigns the same rank to ties but does not skip ranks after ties.

🧠 Conceptual
advanced
1:30remaining
Understanding rank gaps with RANK()

Why does the RANK() function sometimes skip rank numbers after ties?

ABecause RANK() counts the number of tied rows and skips ranks accordingly to reflect the position in the ordered list.
BBecause RANK() always assigns consecutive ranks without gaps regardless of ties.
CBecause RANK() assigns random ranks to tied rows causing gaps.
DBecause RANK() ignores ties and assigns unique ranks to each row.
Attempts:
2 left
💡 Hint

Think about how RANK() reflects the position of rows in the sorted order.

🧠 Conceptual
advanced
1:30remaining
When to use DENSE_RANK() instead of RANK()

Which scenario best describes when to use DENSE_RANK() instead of RANK()?

AWhen you want to assign ranks without gaps even if there are ties, such as ranking products by sales where consecutive ranks are needed.
BWhen you want to skip ranks after ties to reflect the actual position in the list.
CWhen you want to assign random ranks to rows.
DWhen you want to ignore ties and assign unique ranks.
Attempts:
2 left
💡 Hint

Consider if you want ranks to be consecutive numbers or to reflect gaps.

📝 Syntax
expert
2:00remaining
Identify the syntax error in RANK() usage

Which of the following SQL queries will cause a syntax error?

SELECT Player, Points, RANK(ORDER BY Points DESC) AS RankPos FROM Scores;
ASELECT Player, Points, RANK() OVER (PARTITION BY Player ORDER BY Points DESC) AS RankPos FROM Scores;
BSELECT Player, Points, RANK() OVER (ORDER BY Points DESC) AS RankPos FROM Scores;
CSELECT Player, Points, RANK() OVER (ORDER BY Points DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS RankPos FROM Scores;
DSELECT Player, Points, RANK(ORDER BY Points DESC) AS RankPos FROM Scores;
Attempts:
2 left
💡 Hint

Check the correct syntax for window functions like RANK().