ML Engineer MasterClass (April) | 6 seats left

Ranking - ROW_NUMBER(), RANK(), DENSE_RANK()

Ranking - ROW_NUMBER(), RANK(), DENSE_RANK()

Lesson Objectives

By the end of this lesson, you will:

  • Learn how to use ROW_NUMBER() to assign unique ranks
  • Use RANK() and DENSE_RANK() to handle ties
  • Write SQL queries to analyze Meta post engagement using ranking functions

🎬 Scenario: Ranking Meta Posts by Engagement

Your manager at Meta, Jordan, wants to see which posts have received the most likes and how they rank against others.

Can you generate a report ranking posts based on likes while handling ties correctly?

Your task is to use window functions (ROW_NUMBER(), RANK(), and DENSE_RANK()) to generate a ranking report. Use the meta_posts table to solve the question.

post_iduser_idpost_contentpost_datelikessharespost_status
11“Had a great day at the beach!”“2024-01-10T14:30:00”12045“Public”
22“Launching our new product!”“2024-01-12T09:20:00”25090“Friends Only”
33“Just finished a 10K run!”“2024-01-15T18:10:00”8030“Public”
44“Hosting a webinar next week.”“2024-01-18T20:45:00”5025“Private”
55“Traveling to Japan soon!”“2024-01-20T10:05:00”18075“Public”
61“Excited for the weekend!”“2024-01-25T16:15:00”9540“Friends Only”
72“Need recommendations for a new laptop.”“2024-01-27T12:40:00”7020“Public”
83“Learning SQL window functions!”“2024-01-29T08:55:00”11050“Public”
94“Working on a new AI model.”“2024-02-01T14:00:00”200100“Friends Only”
105“Book recommendations?”“2024-02-05T11:30:00”6515“Public”

1. Understanding Window Functions for Ranking

Window functions allow you to assign ranks while keeping the full dataset intact.

FunctionWhat It DoesHandles Ties?Gaps in Rank?
ROW_NUMBER()Assigns a unique rank to each row❌ No✅ Yes
RANK()Assigns the same rank to tied values✅ Yes✅ Yes
DENSE_RANK()Assigns the same rank to ties but avoids gaps✅ Yes❌ No

Syntax for Ranking Functions

SQL
SELECT column1, column2,  
       function() OVER (PARTITION BY column ORDER BY column) AS rank  
FROM table_name;
  • PARTITION BY groups rankings within each category.
  • ORDER BY determines ranking criteria.

2. Using ROW_NUMBER() for Unique Ranking

Jordan wants to assign a unique rank to each post based on the number of likes.

Example Query: Assigning Unique Ranks

SQL
SELECT post_id, user_id, likes,  
       ROW_NUMBER() OVER (ORDER BY likes DESC) AS rank  
FROM meta_posts;
post_iduser_idlikesrank
222501
942002
551803
111204
831105
61956
33807
72708
105659
445010

What’s Happening?

  • ROW_NUMBER() assigns a unique rank to each post.
  • If two posts have the same likes, their ranks still differ.

3. Using RANK() to Handle Ties

Jordan now wants to assign the same rank to tied values while keeping gaps in ranking.

Example Query: Handling Ties with RANK()

SQL
SELECT post_id, user_id, likes,  
       RANK() OVER (ORDER BY likes DESC) AS rank  
FROM meta_posts;
post_iduser_idlikesrank
222501
942002
551803
111204
831105
61956
33807
72708
105659
445010

What’s Happening?

  • RANK() assigns the same rank to tied values.
  • Gaps appear in the ranking (i.e., after rank 3, it jumps to rank 5).

4. Using DENSE_RANK() to Handle Ties Without Gaps

Jordan prefers a ranking without gaps when ties occur.

Example Query: Handling Ties with DENSE_RANK()

SQL
SELECT post_id, user_id, likes,  
       DENSE_RANK() OVER (ORDER BY likes DESC) AS rank  
FROM meta_posts;
post_iduser_idlikesrank
222501
942002
551803
111204
831105
61956
33807
72708
105659
445010

What’s Happening?

  • DENSE_RANK() assigns the same rank to ties but avoids gaps.

✍️ SQL Exercises

Exercise 1: Assigning Unique Ranks to Posts

Jordan wants to assign unique ranks to each post based on the number of likes.

Filter condition:

  • Retrieve post ID, user ID, likes, and rank.
  • Assign unique ranks using ROW_NUMBER().
  • Sort by likes in descending order.
post_iduser_idlikesrank
222501
942002
551803
111204
831105
61956
33807
72708
105659
445010

Write an SQL query to return the requested data.


Exercise 2: Ranking Posts While Handling Ties

Jordan also wants to assign the same rank to posts with equal likes but without gaps.

Filter condition:

  • Retrieve post ID, user ID, likes, and rank.
  • Assign rank using DENSE_RANK().
  • Sort by likes in descending order.
post_iduser_idlikesrank
222501
942002
551803
111204
831105
61956
33807
72708
105659
445010

Write an SQL query to return the requested data.