RANK vs DENSE_RANK vs ROW_NUMBER: how ties are ranked

ROW_NUMBER(), RANK() and DENSE_RANK() are SQL window functions that number rows in the order you give them. They only differ when two rows tie: ROW_NUMBER() still gives each row its own number, RANK() gives tied rows the same number and then skips ahead (1, 2, 2, 4), and DENSE_RANK() gives them the same number without skipping (1, 2, 2, 3). PySpark has the same three functions with the same behaviour.

The difference in one table

Take five students with scores 95, 90, 90, 85 and 80, ordered from highest to lowest. ROW_NUMBER returns 1, 2, 3, 4, 5: the two 90s get different numbers. RANK returns 1, 2, 2, 4, 5: both 90s are 2, and the next row is 4 because two rows already took the second place. DENSE_RANK returns 1, 2, 2, 3, 4: both 90s are 2 and the next row is 3, so the numbers stay consecutive. When there are no ties, all three return the same numbers.

ROW_NUMBER: one number per row

ROW_NUMBER never repeats a number inside a partition, so it is the function for picking exactly one row: the latest order per customer, the first login per day, or one copy of each duplicate. With ties, which tied row gets the lower number is not defined, and it can change between runs. Add a tie-breaker to the ORDER BY, such as ORDER BY score DESC, student, whenever the result must be repeatable.

RANK: ties share a place, then a gap

RANK is the numbering used in a race: two runners who tie for second are both second, and the next runner is fourth. Use it when the position itself matters, for example a leaderboard that should show 1, 2, 2, 4. Because places are skipped, a filter such as WHERE rnk = 3 can return no rows at all.

DENSE_RANK: ties share a place, no gap

DENSE_RANK numbers the distinct values: every different score gets the next number. That makes it the right choice for questions about distinct values, such as the second- or third-highest salary. WHERE dense_rnk = 3 finds the third-highest distinct value even when the top values are tied, which RANK cannot guarantee.

Which one to use for top N

The three functions give different row counts for the same "top 3". In the scores example, row_num <= 3 returns exactly three rows. rnk <= 3 also returns three rows (95, 90, 90). dense_rnk <= 3 returns four rows, because it keeps everyone with one of the top three distinct scores (95, 90, 90, 85). If the question asks for exactly N rows, use ROW_NUMBER with a tie-breaker. If tied rows must not be cut off, use RANK or DENSE_RANK, and check which of the two the question means. In an interview, asking how ties should be handled is a good first step.

The same functions in PySpark

PySpark has F.row_number(), F.rank() and F.dense_rank() in pyspark.sql.functions, used with a window: w = Window.partitionBy("dept").orderBy(F.col("salary").desc()), then df.withColumn("rnk", F.rank().over(w)). They behave exactly like the SQL versions, and spark.sql() accepts the SQL form unchanged. See the PySpark window functions guide for frames, lag() and lead().

NULLs and partitions

Rows with NULL in the ORDER BY column are tied with each other, so RANK and DENSE_RANK give them the same number. Where they appear depends on the engine: Spark sorts NULLs first in ascending order and last in descending order, as do MySQL and SQLite, while PostgreSQL does the opposite. In PySpark you can choose explicitly with desc_nulls_first() or asc_nulls_last(). With PARTITION BY, numbering restarts at 1 in every partition, so each department or region gets its own ranking.

ROW_NUMBER, RANK and DENSE_RANK compared
FunctionTied rows getGap after a tieScores 95, 90, 90, 85Typical use
ROW_NUMBER()Different numbersNo1, 2, 3, 4One row per group, deduplication
RANK()The same numberYes1, 2, 2, 4Leaderboards, competition ranking
DENSE_RANK()The same numberNo1, 2, 2, 3Nth-highest distinct value

Example (SQL)

SELECT student, score,
       ROW_NUMBER() OVER (ORDER BY score DESC, student) AS row_num,
       RANK()       OVER (ORDER BY score DESC)          AS rnk,
       DENSE_RANK() OVER (ORDER BY score DESC)          AS dense_rnk
FROM scores
ORDER BY score DESC, student;

Ben and Chen both scored 90. ROW_NUMBER gives them 2 and 3 (the student name breaks the tie), RANK gives both 2 and skips to 4, and DENSE_RANK gives both 2 and continues with 3.

Run this example in the free online PySpark compiler

Expected output

student  score  row_num  rnk  dense_rnk
Asha     95     1        1    1
Ben      90     2        2    2
Chen     90     3        2    2
Dev      85     4        4    3
Eli      80     5        5    4

Frequently asked questions

What is the difference between RANK and DENSE_RANK?

Both give tied rows the same number. RANK then skips numbers (1, 2, 2, 4), while DENSE_RANK continues without a gap (1, 2, 2, 3).

What is the difference between ROW_NUMBER and RANK?

ROW_NUMBER gives every row a different number even when values tie. RANK gives tied rows the same number and leaves a gap after them.

Which function finds the second-highest salary?

DENSE_RANK: filter on dense_rnk = 2 to get the second-highest distinct salary, even when several people share the highest one.

Do RANK and DENSE_RANK work the same in PySpark?

Yes. F.rank() and F.dense_rank() over a Window give the same results as the SQL functions, and spark.sql() runs the SQL form directly.

Why does ROW_NUMBER return different results each time?

When rows tie on every ORDER BY column, their order is not defined. Add a unique tie-breaker column to the ORDER BY to make it repeatable.

Practice challenges

Open the free PySpark compiler · Data Engineering challenges · Data Engineering jobs

Follow the free PySpark tutorial