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)?

Advanced2 Minutes600 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