What are the key differences between SOQL and SOSL, and when do you choose one over the other?
Answer
| SOQL | SOSL | |
|---|---|---|
| Purpose | Query records of one object (plus related) | Text search across many objects and fields |
| Returns | List of sObjects | List of lists of sObjects |
| Search | Field-based WHERE filters | Full-text search index, word matching |
| Limits | 100 queries, 50,000 rows | 20 SOSL queries, 2,000 rows each |
| Use when | You know the object and need filtered/related data, aggregates | You don't know which object/field holds the text, or need fuzzy text search |
apex
List<List<SObject>> r = [FIND 'Acme*' IN ALL FIELDS RETURNING Account(Id, Name), Contact(Id, Name)];
soqlsoslfundamentals