How do you use a window function with DENSE_RANK to rank salaries within each department and filter for rank 2?
💡 Model Answer
DENSE_RANK is a window function that assigns the same rank to rows with identical values and skips no numbers when ties occur. In the query you partition by department_id so each department gets its own ranking, order by salary descending so the highest salary gets rank 1, and then you filter for rnk = 2 to get the second distinct salary. The key difference from ROW_NUMBER is that if two employees share the top salary, both get rank 1 and the next distinct salary is rank 2, not 3. This is useful when you need the second highest distinct salary per department.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500