Skip to main content
PYSPARK โ€ข LESSON 109

Ranking with Gaps using rank()

How do we rank employee salaries within each department, leaving gaps for tied values (e.g. 1, 2, 2, 4)?

Advancedโฑ 2 Minutesโญ 600 XP
๐Ÿค” THE QUESTION

How do we rank employee salaries within each department, leaving gaps for tied values (e.g. 1, 2, 2, 4)?

๐Ÿ’ก WHAT IS IT?

rank().over(windowSpec) computes standard ranks with gap jumps after tied values.

๐ŸŽฏ WHAT IS IT USED FOR?

Competition leaderboards, salary tiering, and performance rank audits.

๐Ÿ’ป EXAMPLE
from pyspark.sql.window import Window
from pyspark.sql.functions import col, rank

window_spec = Window.partitionBy("department").orderBy(col("salary").desc())
df = df.withColumn("rank", rank().over(window_spec))

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Ranking with Gaps using rank().

  • โœ“ Import rank function
  • โœ“ Calculate competition rank with ties
  • โœ“ Append departmental salary ranks