I have list of Account ids and when pass the account list to SOQL, what will order of the record return by salesforce? It will be same order where list of id is present ?

exmaple :

List<ID> accountIds=new List<ID>{'id1','id2','id3'};
List<Account> accountList=[SELECT Id,Name FROM WHERE Id in:accountIds];

What will be the order of the accountList i get ? Any article on this ?


1 Answer 1


As per this answer, there's no guaranteed ordering without an ORDER BY clause. However, empirically it appears that objects are (at least in the observed cases) ordered by Id (i.e. adding ORDER BY Id retains the same order) which is aligned with ordering by created date/time. The results are definitely not returned in the order specified in an IN array of IDs.

  • While there is no deterministic order without using ORDER BY, Salesforce does at least guarantee that the order of results will be the same every time a given query is executed. Having trouble finding supporting documentation, but I know it's out there somewhere.
    – Derek F
    Commented Mar 31, 2020 at 11:26
  • this is why I use AutoNumber fields on objects with ORDER BY so I can have predictable results in testmethods
    – cropredy
    Commented Apr 1, 2020 at 4:59

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