What is the difference between SOQL and SOSL?
1What it is
SOQL queries one object (and its related records) with exact filters - like SQL's SELECT. SOSL searches text across many objects at once using the search index.
2Why it matters
They answer different questions. When you know where the data is, SOQL is precise. When a user types "acme" and it could be an account, a contact or a lead, SOSL finds all of them in one call.
3Example
// SOQL: one object, exact filters
List<Account> big = [SELECT Name FROM Account WHERE AnnualRevenue > 1000000];
// SOSL: a word, across several objects
List<List<SObject>> found = [FIND 'Acme*' IN NAME FIELDS
RETURNING Account(Id, Name), Contact(Id, Name, Email), Lead(Id, Company)];
List<Account> accounts = (List<Account>) found[0];| SOQL | SOSL | |
|---|---|---|
| Searches | One object and its relationships | Many objects at once |
| Matching | Exact filters on fields | Text search, with wildcards |
| Per transaction | 100 queries (200 async), 50,000 rows | 20 searches, 2,000 records each |
4In short - say this in the interview
SOQL queries one object with exact filters, like SQL. SOSL searches a word across many objects through the search index. SOQL when you know where the data is, SOSL when you only know the word.
Using SOSL to fetch records by Id or exact field values. That is SOQL's job, and SOSL results depend on the search index, which updates with a short delay.
- Why might a newly created record not appear in SOSL straight away?
- What does IN NAME FIELDS change?