📎 Referral Code: —
🎁 Referral Earnings ₹0 →
📊 Dashboard Sign In
🎁 ₹0
Your Referral Earnings → Tap to view
Buy
🛒
Buy Course
Videos
📘
Basic Videos
🚀
Project Related Videos
🐍
Old Videos Style — will be removed soon
📚
Buy Book
After Course Completion
📒
My Interview Tracker — HR calls & follow-ups
📈
My Performance — Track your progress
🎯
Interview Prep — Resume & questions for a job
🏢
Company Interview Questions — with answers
🤖
Central Command — your HQ bot
💼
Jobs — AWS Data Engineer openings
Personal Training
🎬
Live Classes
🎯
My 1:1 Dashboard
🗺️
Course Roadmap
🎤
Mock Interview
🧑‍💼
HR Discussion
🗓️
Slot Booking
📅
My Booked Slots
Passive Income
🎁
Refer & Earn — ₹1,000 per referral
🤝
Browse Job Support
💰
Become a Supporter
🔗
Browse Referrals
📢
Post a Referral
🎓
Course Marketplace — Other Trainers
🧑‍🏫
Teach a Course
📊
My Courses & Earnings
🎬
Reels
AI Tools
🌐
Project Explanation Agent
👤
My Profile
🏢
Recruiter Board
🛟
Support Works

← LTIMindtree interview questions

table is salary
  • col -emop_id, dept_id, salary
  • using window function find the second highest salary?

🏢 LTIMindtree 🏷️ PySpark #2 of 4

📝 Answer

1. Interviewer expects

The interviewer checks whether you know window functions (partitionBy, orderBy) and whether you handle duplicate salaries (ties) correctly. Also clarify: second highest per department or for the whole company?

Telugu note: same salary ఇద్దరికి ఉంటే ఎలా handle చేస్తారో చూస్తారు.

2. Why / when it happens

In real payroll data, many employees share the same salary. The three ranking functions treat ties differently:

emp_idsalaryrow_numberrankdense_rank
190000111
280000222
380000322
470000443
  • row_number drops one of the tied employees.
  • rank leaves gaps (1, 2, 2, 4).
  • dense_rank has no gaps (1, 2, 2, 3).

3. Solution

  1. Partition by dept_id.
  2. Order by salary descending.
  3. Apply dense_rank().
  4. Filter rnk == 2.

Telugu note: dense_rank same salary కి same rank ఇస్తుంది, so second highest ఎప్పుడూ correct.

4. Fix in code

from pyspark.sql import SparkSession, Window
from pyspark.sql.functions import col, dense_rank
spark = SparkSession.builder.getOrCreate()
data = [(1,10,90000),(2,10,80000),(3,10,80000),
        (4,10,70000),(5,20,60000),(6,20,55000),
        (7,20,50000)]
cols = ["emp_id", "dept_id", "salary"]
df = spark.createDataFrame(data, cols)
w = (Window.partitionBy("dept_id")
     .orderBy(col("salary").desc()))
res = (df.withColumn("rnk", dense_rank().over(w))
       .filter(col("rnk") == 2))
res.orderBy("dept_id", "emp_id").show()

Output:

emp_iddept_idsalaryrnk
210800002
310800002
620550002

Spark SQL version:

SELECT emp_id, dept_id, salary
FROM (
  SELECT *,
         DENSE_RANK() OVER (
           PARTITION BY dept_id
           ORDER BY salary DESC) AS rnk
  FROM salary
) t
WHERE rnk = 2;

For the whole company, remove partitionBy / PARTITION BY. Window functions cannot be used directly in WHERE, so we use a subquery.

5. Your answer

"First I'd confirm whether they want it per department or for the whole company. For per department, I create a window partitioned by dept_id and ordered by salary descending, apply dense_rank, and filter rank 2. I use dense_rank because in our payroll data two employees often had the same salary; row_number would drop one and rank leaves gaps. For the whole company, I just remove the partitionBy."