+ Start a Discussion
santhosh konathala 8santhosh konathala 8 

when we need to use limit and offset keyword in apex code?

Best Answer chosen by santhosh konathala 8
JyothsnaJyothsna (Salesforce Developers) 
Hi Santhosh,


LIMIT is an optional clause that can be added to a SELECT statement of an SOQL query to specify the maximum number of rows to return.

For Example
SELECT Name
FROM Account
WHERE Industry = 'Media' LIMIT 125
This query returns the first 125 Account records whose Industry is Media.
You can use LIMIT with count() as the fieldList to count up to the maximum specified.


OFFSETclause on an SOQL query. For example, you can use OFFSET to display records 51 to 75 and then jump to displaying records 301 to 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.

As an example, if an SOQL query normally returned 50 rows, you could use OFFSET 10 in your query to skip the first 10 rows:
 
SELECT Name
FROM Merchandise__c
WHERE Price__c > 5.0
ORDER BY Name
LIMIT 100
OFFSET 10

The result set for the preceding example would be a subset of the full result set, returning rows 11 through 50 of the full set.


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.


Hope this helps you!
Best Regards,
Jyothsna

All Answers

JyothsnaJyothsna (Salesforce Developers) 
Hi Santhosh,


LIMIT is an optional clause that can be added to a SELECT statement of an SOQL query to specify the maximum number of rows to return.

For Example
SELECT Name
FROM Account
WHERE Industry = 'Media' LIMIT 125
This query returns the first 125 Account records whose Industry is Media.
You can use LIMIT with count() as the fieldList to count up to the maximum specified.


OFFSETclause on an SOQL query. For example, you can use OFFSET to display records 51 to 75 and then jump to displaying records 301 to 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.

As an example, if an SOQL query normally returned 50 rows, you could use OFFSET 10 in your query to skip the first 10 rows:
 
SELECT Name
FROM Merchandise__c
WHERE Price__c > 5.0
ORDER BY Name
LIMIT 100
OFFSET 10

The result set for the preceding example would be a subset of the full result set, returning rows 11 through 50 of the full set.


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.


Hope this helps you!
Best Regards,
Jyothsna
This was selected as the best answer
Carl johnson 21Carl johnson 21
The part I find interesting is the more or less complete separation of comments here, keep it up for more updates about this related blog. Visit our website gtacheatcode for latest game’s cheats code and click here for 2020 Sims 4 Vampire Cheats (https://gtacheatcode.com/sims-4-vampire-cheats/)