This is best Fabric, Power BI, SQL and AI community event. How do we know? The last event sold out! Save €200 with code FABCMTY200.
Register nowThe Fabric community is now in read-only for platform upgrade. Learn more
Hi All
I need help with a SQL problem as desc below
Find the second highest salary from an Employee table without using LIMIT, TOP, or window functions.
Solved! Go to Solution.
Hi @powerbidev123 ,
You can use a subquery to find the maximum salary that is lower than the overall maximum salary:
SELECT MAX(salary) AS second_highest_salary
FROM Employee
WHERE salary < (
SELECT MAX(salary)
FROM Employee
);
The inner query finds the highest salary. The outer query then finds the highest salary below that value, which gives you the second-highest distinct salary.
This also handles duplicate highest salaries correctly.
Hi @powerbidev123 ,
You can use a subquery to find the maximum salary that is lower than the overall maximum salary:
SELECT MAX(salary) AS second_highest_salary
FROM Employee
WHERE salary < (
SELECT MAX(salary)
FROM Employee
);
The inner query finds the highest salary. The outer query then finds the highest salary below that value, which gives you the second-highest distinct salary.
This also handles duplicate highest salaries correctly.