I have not been able to find exact answer on the developer pages, but from coding I am seeing that the only data type returned from a SOQL is a List. Is this correct?

I am encountering this because I was wanting to have the returned objects from the SOQL put inside of a Set instead of a List because the Set class has some remove methods that the List class does not. (i.e. removeAll()).

Is there a way to have the returned SOQL put into a Set, or does the returned SOQL always have to be initially put into a List? I can take the List and then create a set from it, but is there a way to skip the two step process?


1 Answer 1


SOQL can only return a single record, a list of records, or an Integer (if a COUNT method is used). As for what you're asking, you can create and populate other containers without explicitly instantiating a List container:

Map<ID, Contact> contactMap = new Map<ID, Contact>([SELECT Id FROM Contact]);

Set<Contact> contactSet = new Set<Contact>([SELECT Id FROM Contact]);

EDIT: As @SamuelDeRycke notes, a List of the SObject AggregateResult is returned if aggregate functions are used - worth noting since the syntax of the query is slightly different.

  • Don't forget about Aggregated Results for Group By Oct 12, 2015 at 15:22

You must log in to answer this question.

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