Write a solution to find the second highest salary from the Employee table. If there is no second highest salary, return null.
This is my code:
select ifnull(x.salary,NULL) as SecondHighestSalary
from(
select *, dense_rank() over( order by salary) as rnk
from Employee)x
where x.rnk =2
It is passing all test case but not able to pass only 1 test case.
Employee:
id | salary |
---|---|
1 | 100 |
Output:
SecondHighestSalary |
---|
Expected output:
SecondHighestSalary |
---|
null |
Still don’t know why Isnull
is not working.
Can someone please tell me why 1 test case is failing?
2
Answers
try:
You can use a good, old subquery with
MAX
rather than a window function. This will returnNULL
if no second highest salary exists:See this sample fiddle