Professional Documents
Culture Documents
Search Write
Lori Lu · Follow
4 min read · Mar 30, 2022
85
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 1/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
Struggle to answer this question? Well, you have to read this blog!
Let’s go back to the real world and learn the difference from a common retail
analytics use case. We’ll run some queries in Byzer Notebook so you can
easily compare them to each other.
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 2/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
In this blog, let’s run some SQL queries in Byzer Notebook to find the Top 3
Best-Selling Products of this season for a retailer.
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 3/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
set sales='''
product,product_price,items_sold
a, 44.12, 6547
b, 100, 547
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 4/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
c, 12.47, 48
d, 12, 3254
e, 100, 547
f, 12, 3254
g, 12, 3254
h, 7.77, 147
''';
-- convert JSON string to a table
as sales;
However, the results might surprise you as you run the query with RANK() :
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 5/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
Let’s run the following query that ranks the rows by revenue using the three
ranking functions described above:
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 6/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 7/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
The RANK() function creates a ranking of the rows based on the provided
columns. It starts with assigning “1” to the first row in the order and then
gives higher numbers to rows lower in the order. If rows have the same
value, they’re ranked the same. However, the next spot is shifted accordingly.
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 8/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
For example, if two rows are 2th (have the same rank), the next row will be
4th (i.e., 3rd doesn’t exist).
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 9/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
have the same rank, the rank of the next row will be one plus the previous
number. For example, if two rows are 2rd, the next row will be 3rd.
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 10/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
So DENSE_RANK() is the right ranking function to show top results in this use
case.
SELECT
RANK() OVER(ORDER BY product_price * items_sold DESC) AS rank,
DENSE_RANK() OVER(ORDER BY product_price * items_sold DESC) AS
dense_rank,
ROW_NUMBER() OVER(ORDER BY product_price * items_sold DESC) AS
row_number,
product,
product_price * items_sold AS revenue
FROM sales
AS sales_rank;
What’s Byzer?
Go to this blog — Byzer 101, if you want to install Byzer on your laptop.
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 11/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
Want to see how Byzer how seamlessly Byzer Notebook integrates with SQL
and Python? Go try this tutorial:
Leaving feedback:
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 12/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
438 Followers
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 13/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
40 196
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 14/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
8 min read · May 27, 2022 3 min read · Jan 13, 2022
51 1 33
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 15/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
1.5 Years of Spark Knowledge in 8 Ten Habits that will get you ahead
Tips of 99% of People
My learnings from Databricks customer Improve your life and get ahead of your peers
engagements in 10 simple steps
8 min read · Dec 24, 2023 9 min read · Nov 18, 2023
Lists
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 16/18
1/4/24, 5:57 AM What’s the difference? — RANK() vs.DENSE_RANK() vs.ROW_NUMBER() | by Lori Lu | Medium
What I learned after one year of Deep Dive into Apache Parquet:
building a Data Platform from… Efficient Data Storage for Analytics
My key learnings on building a Data platform, In today’s digital age, the amount of data
from the tech side to the business side being generated is growing at an…
9 min read · Nov 14, 2023 9 min read · Aug 27, 2023
2.5K 30 155 1
97 1
Help Status About Careers Blog Privacy Terms Text to speech Teams
https://medium.com/@LoriLu/whats-the-difference-rank-vs-dense-rank-vs-row-number-3aca5ecfb928 18/18