Skip to main content
PYSPARK • LESSON 99

Self Join for Hierarchical Reporting

How do we join an employee table to itself to resolve employee-to-manager reporting hierarchies?

Advanced2 Minutes530 XP
🤔 THE QUESTION

How do we join an employee table to itself to resolve employee-to-manager reporting hierarchies?

💡 WHAT IS IT?

Aliasing the same DataFrame with different names (emp and mgr) and joining manager_id to employee_id.

🎯 WHAT IS IT USED FOR?

Organizational hierarchy modeling, recursive bill-of-materials, and multi-hop referral tracking.

💻 EXAMPLE
from pyspark.sql.functions import col

hierarchy_df = employees.alias("emp").join(
    employees.alias("mgr"),
    col("emp.manager_id") == col("mgr.employee_id"),
    "left"
).select(
    col("emp.name").alias("employee"),
    col("mgr.name").alias("manager")
)

🎯 Mission Objectives

Practice typing production-grade PySpark code for Self Join for Hierarchical Reporting.

  • Alias DataFrame instances with alias()
  • Join employee manager_id to manager employee_id
  • Select resolved organizational hierarchy