I know SOQL can only retrieve data.

Now my question is, WITHOUT WRITING APEX, given that a query have 4 results. I know I can filter the results to 1 by limiting row and pretty sure this could be the 1st record.

Can I have a syntax that will filter down the results for the query to show the 2nd record or 3rd?

Appreciate your response on this.

1 Answer 1


You can use the offset clause for this - this will skip the specified number of rows in the result. If you also supply a limit 1, this will be applied after the offset and return you the single row you desire. Looking at the debug logs, offset applies a default ordering by id, so you may need to supply your own ordering.

The following code first pulls back 10 accounts ordered by id, then the third and fourth in the list:

List<Account> accs=[select id, name from account order by id limit 10];
System.debug('Accs = ' + accs);
accs=[select id, name from account limit 1 offset 2];
System.debug('Accs = ' + accs);
accs=[select id, name from account limit 1 offset 3];
System.debug('Accs = ' + accs);

Anonymous execution was successful.

08:32:39.024 (24324207)|USER_DEBUG|[2]|DEBUG|Accs = (Account:{Name=GenePoint, Id=0018000000Q9v1QAAR}, Account:{Name=United Oil & Gas, UK, Id=0018000000Q9v1RAAR}, Account:{Name=United Oil & Gas, Singapore, Id=0018000000Q9v1SAAR}, Account:{Name=Edge Communications, Id=0018000000Q9v1TAAR}, Account:{Name=Burlington Textiles Corp of America, Id=0018000000Q9v1UAAR}, Account:{Name=Pyramid Construction Inc., Id=0018000000Q9v1VAAR}, Account:{Name=Dickenson plc, Id=0018000000Q9v1WAAR}, Account:{Name=Grand Hotels & Resorts Ltd, Id=0018000000Q9v1XAAR}, Account:{Name=Express Logistics and Transport, Id=0018000000Q9v1YAAR}, Account:{Name=University of Arizona, Id=0018000000Q9v1ZAAR})

08:32:39.027 (27277422)|USER_DEBUG|[4]|DEBUG|Accs = (Account:{Name=United Oil & Gas, Singapore, Id=0018000000Q9v1SAAR})

08:32:39.029 (29455227)|USER_DEBUG|[6]|DEBUG|Accs = (Account:{Name=Edge Communications, Id=0018000000Q9v1TAAR})


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .