Reviewed by Aditya Kumar · Last reviewed 2026-03-24
To find the second highest salary in each department using PySpark, the most robust and idiomatic approach leverages window functions. Specifically, you partition the data by department, order it by…
This medium-level Spark/Big Data question appears frequently in data engineering interviews at companies like Altimetrik, Infosys. While less common, it tests deeper understanding that distinguishes strong candidates. Mastering the underlying concepts (partition, spark, sql) will help you answer variations of this question confidently.
Break this problem into components. Identify the core trade-offs involved, then walk the interviewer through your reasoning step by step. Demonstrate awareness of edge cases and production considerations - this is what separates good answers from great ones. The expert answer includes a code example that demonstrates the implementation pattern.
To find the second highest salary in each department using PySpark, the most robust and idiomatic approach leverages window functions. Specifically, you partition the data by department, order it by salary in descending order, and then apply dense_rank() to assign ranks within each department group, finally filtering for rank 2.
DENSE_RANKThe Window.partitionBy("department") clause logically groups rows by department. This operation necessitates a data shuffle across the Spark cluster, where all records for a given department are moved to the same executor partition. Within each department partition, orderBy(F.desc("salary")) sorts the employees by salary from highest to lowest.
F.dense_rank().over(windowSpec) then assigns a rank to each employee within their department. DENSE_RANK is crucial here because it assigns consecutive ranks without gaps, even if there are ties. For example, if two employees share the highest salary (rank 1), the next distinct salary will correctly receive rank 2. This ensures you truly find the second highest salary, not just the second row after ordering. The final step, filter(F.col("rank") == 2), isolates the employees holding the second highest salary in their respective departments.
from pyspark.sql.window import Window
from pyspark.sql import functions as F
# Assume 'df' is your DataFrame with 'department' and 'salary' columns
windowSpec = Window.partitionBy("department").orderBy(F.desc("salary"))
ranked_df = df.withColumn("rank", F.dense_rank().over(windowSpec))
result_df = ranked_df.filter(F.col("rank") == 2).select("department", "salary")
The partitionBy operation is a full data shuffle, which can be computationally expensive due to network I/O and serialization/deserialization across Spark executors. For very large datasets, especially with highly skewed department sizes (e.g., one department with millions of employees and others with hundreds), this can lead to data skew. Skew causes a few Spark tasks to process disproportionately more data, becoming bottlenecks and slowing down the entire job.
To mitigate this:
* Data Layout: If department is a frequent partitioning key, consider physically partitioning your data lake tables (e.g., Parquet or Delta Lake) by department. This can reduce shuffle costs for subsequent queries. For systems like Databricks Delta Lake or Snowflake, Z-ORDER or clustering on department can also improve performance by co-locating related data.
Pre-filtering: If you only need to analyze a subset of departments, filter the DataFrame before* applying the window function to reduce the volume of data being shuffled.
Discuss the importance of understanding the Spark execution plan (DAG, stages, tasks) and how shuffles impact performance. Briefly mention that while other methods like self-joins or UDFs exist, window functions are generally the most performant and idiomatic for this type of problem in Spark.
Red Flag: Using ROW_NUMBER when ties exist—you'll miss valid second-highest. Pro-Move: 'I use DENSE_RANK and add a tiebreaker (e.g., emp_id) in orderBy for deterministic results; I partition the source table by department for efficient windows.'
Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.
According to DataEngPrep.tech, this is one of the most frequently asked Spark/Big Data interview questions, reported at 2 companies. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.