Ranking

 1. Second Highest Salary Without LIMIT or TOP

SELECT MAX(salary) AS second_highest_salary

FROM employees

WHERE salary < (SELECT MAX(salary) FROM employees);


2  nth Highest Salary -


SELECT salary AS nth_salary FROM 

(  SELECT salary,rank() OVER (ORDER BY salary DESC) AS rn    FROM employees )

WHERE rn = 3


3. Department Wise nth Highest Salary -

select department_id, salary, rnk from 

(SELECT department_id, salary, rank() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rnk  FROM employees) 

WHERE rnk = 3;


No comments:

Post a Comment

About This Blog

SQL and PL/SQL are essential for database management, enabling efficient data retrieval, manipulation, and transaction control. SQL (Structu...