What SOSL does that SOQL can't
SOSL (Salesforce Object Search Language) performs full-text search across multiple objects and multiple fields simultaneously in a single query, whereas SOQL always targets one primary object at a time. A SOSL query like FIND 'Acme' IN ALL FIELDS RETURNING Account(Id, Name), Contact(Id, LastName), Lead(Id, Company) searches the search index (not the raw table) for the term 'Acme' across Accounts, Contacts, and Leads at once, returning up to 2,000 total records across all returned sObject types.
Cricket analogy: A commentator's archive search for 'Kohli' pulling up match reports, player interviews, and highlight-reel titles all at once — across totally different content types — is exactly what SOSL does across Account, Contact, and Lead simultaneously.
String searchTerm = 'Acme';
List<List<SObject>> searchResults = [
FIND :searchTerm IN ALL FIELDS
RETURNING
Account(Id, Name, Industry WHERE Industry = 'Technology'),
Contact(Id, FirstName, LastName, Email),
Lead(Id, Company, Status)
LIMIT 200
];
List<Account> foundAccounts = (List<Account>) searchResults[0];
List<Contact> foundContacts = (List<Contact>) searchResults[1];
List<Lead> foundLeads = (List<Lead>) searchResults[2];
System.debug('Matched ' + foundAccounts.size() + ' accounts');IN clauses, wildcards, and result structure
SOSL lets you scope the search field set with IN NAME FIELDS, IN EMAIL FIELDS, IN PHONE FIELDS, or IN ALL FIELDS, and supports wildcards — an asterisk (*) for zero-or-more characters and a question mark (?) for exactly one character — inside the search term, e.g. FIND 'John*' to match Johnson or Johnston. Crucially, the query returns a List<List<SObject>>, a list of lists where each inner list corresponds to one of the sObject types named in RETURNING, in the same order they were listed, which trips up beginners who expect a flat result.
Cricket analogy: A stats database search scoped 'search only player names' versus 'search everything including commentary transcripts' mirrors choosing IN NAME FIELDS versus IN ALL FIELDS in SOSL.
SOSL is significantly better than SOQL LIKE '%term%' for text search because it uses Salesforce's dedicated search index, which supports relevance ranking and tokenized matching, whereas a SOQL LIKE query with a leading wildcard forces a slow, non-indexed scan across the field on every matching object.
SOSL is the right tool specifically for open-ended, user-typed search boxes — like a global 'search everything' bar — while SOQL remains correct for precise, structured, known-field queries such as WHERE AccountId = :id. A common architectural pattern combines both: use SOSL first to find candidate record Ids matching a loose text search, then run a targeted SOQL query with WHERE Id IN :candidateIds to fetch additional related fields not exposed conveniently through SOSL's RETURNING clause.
Cricket analogy: A broadcaster first does a loose search across all commentary archives for 'hat-trick' (SOSL-like), then pulls the exact scorecards for just those matching matches (SOQL-like) to get precise stats.
SOSL results are limited to 2,000 records total across all returned sObject types by default (raise-able per-object with a LIMIT clause inside RETURNING, up to that same ceiling), and it can take a few minutes for newly inserted or updated records to appear in the search index, so a record just inserted in the same transaction will not be found by a SOSL search run immediately afterward.
- SOSL performs full-text search across multiple objects and fields in a single query; SOQL always targets one primary object.
- SOSL syntax starts with FIND 'term' IN <field-scope> FIELDS RETURNING Object1(fields), Object2(fields), ...
- Field scope options are IN ALL FIELDS, IN NAME FIELDS, IN EMAIL FIELDS, and IN PHONE FIELDS.
- Wildcards: * matches zero or more characters, ? matches exactly one character.
- SOSL queries return a List<List<SObject>>, with one inner list per object type listed in RETURNING, in the same order.
- SOSL uses the search index for fast, relevance-ranked matching, unlike a non-indexed SOQL LIKE '%term%' scan.
- SOSL results are capped at 2,000 total records, and newly inserted records may take a few minutes to appear in the search index.
Practice what you learned
1. What is the fundamental difference between SOSL and SOQL in terms of scope?
2. What data structure does a SOSL query return in Apex?
3. Which wildcard character in SOSL matches exactly one character?
4. Why is SOSL generally faster than a SOQL query using LIKE '%term%' for text search?
5. Why might a record inserted earlier in the same transaction not appear in a SOSL search run immediately afterward?
Was this page helpful?
You May Also Like
SOQL Queries
Learn Salesforce Object Query Language (SOQL) syntax for retrieving records, including relationship queries, aggregate functions, and governor-limit-aware query design.
sObjects and DML
Understand Apex's sObject data model and the insert, update, upsert, and delete DML operations used to change Salesforce records, including bulk patterns and exception handling.
Lists, Sets, and Maps
Master Apex's three core collection types — ordered Lists, unique Sets, and key-value Maps — and when to use each for bulk-safe, governor-limit-friendly code.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics