🔥 *Find Nth Highest Salary in SQL Server using DENSE_RANK() | Interview Question 💯*
🚀 Struggling with SQL interview questions? This is one of the *MOST ASKED queries* in interviews! Learn how to easily find the *Nth highest salary* using `DENSE_RANK()` in MS SQL Server 💻
💡 *What is DENSE_RANK()?*
👉 `DENSE_RANK()` is a powerful window function that assigns ranking *without skipping numbers* even if there are duplicate salaries.
📊 Example:
10000 → Rank 1
9000 → Rank 2
9000 → Rank 2
8000 → Rank 3
🧠 *SQL Query to Find Nth Highest Salary*
SELECT Salary
FROM (
SELECT Salary,
DENSE_RANK() OVER (ORDER BY Salary DESC) AS SalaryRank
FROM Employees
) AS ranked
WHERE SalaryRank = 3;
🎯 *How it Works (Step-by-Step)*
✅ Step 1: `ORDER BY Salary DESC` → Highest salary comes first
✅ Step 2: `DENSE_RANK()` → Assigns same rank to duplicate salaries
✅ Step 3: Subquery → Stores ranked data
✅ Step 4: Outer query → Filters required rank (Nth highest)
🔥 *Why Use DENSE_RANK()?*
✔ Handles duplicate salaries correctly
✔ No gaps in ranking (1,2,2,3…)
✔ Perfect for real-world data
📌 *Pro Tip for Interviews*
👉 Use `DENSE_RANK()` for *distinct ranking*
👉 Use `ROW_NUMBER()` when duplicates don’t matter
💬 *Comment below* if you want:
👉 Employee name with nth highest salary
👉 Difference between RANK(), DENSE_RANK(), ROW_NUMBER()
🔔 Like | Share | Subscribe for more SQL & Tech content 🚀
#SQL #SQLServer #Database #InterviewPreparation #Coding #LearnSQL #Tech #Programming
sql interview questions, nth highest salary sql, dense_rank sql server, sql server window functions, sql queries for practice, sql interview query examples, how to find nth highest salary, sql ranking functions, dense rank vs rank vs row_number, advanced sql queries, sql server tutorial for beginners, top sql interview questions 2026, sql coding questions and answers, real world sql examples, learn sql fast