I need to calculate second highest or 4th highest salary etc.. what is the way to calculate it.
select salary__c from Staff_Details__c order by salary__c desc
I am getting the salary in desc order but then how to get second.
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.Sign up to join this community
You can get a specific record with LIMIT X OFFSET Y. To get the second-highest Salary, for example:
select salary__c from Staff_Details__c order by salary__c desc nulls last limit 1 offset 1
Note that offset is 0-based, so the second-highest is 1, while fourth-highest would be 3. There's also a limit of OFFSET 2000, so you'll need to come up with alternative methods if that's what you need.