function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ramesh babu 114ramesh babu 114 

. Describe OFFSET and give example and why we use?

. Describe OFFSET and give example and why we use?
Akhil TandonAkhil Tandon
Let's assume you have 10 contact records Contact 1 to Contact 10.
If you fire a query [select id, FirstName, LastName from Contact order by FirstName] the results will show you 10 contact records.
If you fire a query [select id, FirstName, LastName from Contact order by FirstName OFFSET 5] the results will show you only 5 contact records. FIRST five contact records will be skipped due to the command OFFSET 5.

OFFSET is generally used to implement pagination of large set of records. If you have 500 contacts and you want to show 20 records at a time then you will create a query where OFFSET counter will change based on which page you want to view.

[select id, FirstName, LastName from Contact order by FirstName LIMIT 20 OFFSET 0] ==> Page 1
[select id, FirstName, LastName from Contact order by FirstName LIMIT 20 OFFSET 20] ==> Page 2
[select id, FirstName, LastName from Contact order by FirstName LIMIT 20 OFFSET 40] ==> Page 3

OFFSET is available for SOQL and SOSL queries.
More detail is given in below mentioned links.

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_offset.htm (https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_offset.htm)
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_offset.htm
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Ramesh,
  • When expecting many records in a query’s results, you can display the results in multiple pages by using the OFFSET clause on a SOQL query. For example, you can use OFFSET to display records 51–75 and then jump to displaying records 301–350. Using OFFSET is an efficient way to handle large results sets.
  • Use OFFSET to specify the starting row offset into the result set returned by your query. Because the offset calculation is done on the server and only the result subset is returned, using OFFSET is more efficient than retrieving the full result set and then filtering the results locally. OFFSET is available in API version 24.0 and later.
Please refer the below link for reference. hope it helps.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar