+ Start a Discussion
Santoshi K VSantoshi K V 

SOQL-OFFSET

Hi Team

In SOQL using OFFSET is it possible to skip records in the middle? What is the exact use of OFFSET, only to skip initial records? If so,is there any limit to the number of records which we can skip ?

Will there be any scenario, where we have to skip records from middle, if so how to do that?

Thanks in advance
PriyaPriya (Salesforce Developers) 

Hey Santoshi,

We can use OFFSET keyword in SOQL to specify the starting row from the result returned by the query. For example if there are 50 records then, if we specify offset as 20 in the query then it would return record 21 to 50, it will skip first 20 records.

In the following example the query will return rows 11 through 110, and will skip first 10 rows

 

SELECT Name, AccountNumber, Type
FROM Account
WHERE Type = 'Customer - Direct'
ORDER BY Name
LIMIT 100
OFFSET 10
 



Note :- We cannot skip the records from middle. 

For more reference, check this link :- 
https://www.biswajeetsamal.com/blog/using-offset-in-soql/#:~:text=We%20can%20use%20OFFSET%20keyword,will%20skip%20first%2020%20records.

Please mark it as the best answer if it helps you. 

Regards,

Priya Ranjan

mukesh guptamukesh gupta
Hi Santoshi,

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.

SOQL concept:-
 
SELECT fieldList
FROM objectType
[WHERE conditionExpression] 
ORDER BY fieldOrderByList
LIMIT numberOfRowsToReturn
OFFSET numberOfRowsToSkip

 Offset Considerations:

1.The maximum offset is 2,000 rows. Requesting an offset greater than 2,000 will result in aNUMBER_OUTSIDE_VALID_RANGE error.

2.OFFSET cannot be used as a sub-query in the WHERE clause, even if the parent query uses LIMIT 1.

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh