Home > Blockchain >  Finding the nearest salary to the max salary in SQL
Finding the nearest salary to the max salary in SQL

Time:07-28

Input: Salary: 200, 300, 100 with respected id 1, 2, 3
Output: 200 [Here 200 is the closest value of the 300]

I have tried in this way:

 select ABS(max(salary)) from Employee order by salary limit 1;

After running this query it returns the only max value which is 300. If you can complete the query or suggest how to complete then you are most welcome.

CodePudding user response:

You could use this query this way.

SELECT salary FROM Employee ORDER BY salary DESC LIMIT 1,1

This query will fetch second most big salary. This would work!

CodePudding user response:

select salary from Employee order by salary desc limit 1 offset 1;

This should retrieve the second highest salary. It orders the salaries in descinding order, then neglect the first result by offset 1, then limit 1 limits the result to one record.

  • Related