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 now60 Days of Data Days! Live and on-demand sessions, challenges, study groups and more! And it's all FREE!. Join now. 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.
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 |