Last Chance to Join Data Science Interview MasterClass (this week) 🚀 | Just 2 spots remaining...

PRO SQL Modules

SQL Basics
1h 20m • 4 lessons
  1. SQL SELECT
  2. LIMIT
  3. ORDER BY
  4. DISTINCT
Data Filtering
1h 20m • 4 lessons
  1. WHERE
  2. LIKE
  3. IN
  4. BETWEEN
Aggregations
40m • 2 lessons
  1. GROUP BY
  2. HAVING
Multiple Tables
1h 40m • 5 lessons
  1. INNER JOIN
  2. LEFT & RIGHT JOIN
  3. FULL OUTER JOIN
  4. SELF JOIN
  5. INTERSECT & EXCEPT
Query Restructuring
40m • 2 lessons
  1. Subquery
  2. Common Table Expressions (CTE)
Data Transformation
1h 40m • 5 lessons
  1. CASE
  2. String
  3. Date Manipulation
  4. Math Operations
  5. COALESCE
Analytical SQL
2h • 6 lessons
  1. Ranking - ROW_NUMBER(), RANK(), DENSE_RANK()
  2. Running Totals
  3. LAG and LEAD
  4. FIRST_VALUE, LAST_VALUE, Nth_VALUE
  5. PARTITION BY
  6. SQL BETWEEN and PRECEDING
Unlock Premium
FREE

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

Course Author Daniel Lee
Instructor

Dan Lee

Learn SQL for free with interactive exercises designed to help aspiring Data Engineers, Data Analysts, and Data Scientists. Learn the basics and advanced SQL concepts by solving real-world problems from MANGO (Meta, Amazon, Netflix, Google, OpenAI)

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

1SELECT column1, column2,  
2       function() OVER (PARTITION BY column ORDER BY column) AS rank  
3FROM 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

1SELECT post_id, user_id, likes,  
2       ROW_NUMBER() OVER (ORDER BY likes DESC) AS rank  
3FROM 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()

1SELECT post_id, user_id, likes,  
2       RANK() OVER (ORDER BY likes DESC) AS rank  
3FROM 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()

1SELECT post_id, user_id, likes,  
2       DENSE_RANK() OVER (ORDER BY likes DESC) AS rank  
3FROM 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.

Upgrade to Pro

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.

Upgrade to Pro
👉 Found this lesson helpful?