Choosing Between SOQL and SOSL
By the end of this lesson, you'll be able to:
- Decide when to use SOQL versus SOSL for a given problem
- Explain the performance implications of each
Prerequisites: SOSL Basics
When SOQL wins
You know the object, need relationship traversal (parent/child), need aggregates, or need exact filtering on any field type — SOQL is more precise and typically faster when you know your target in advance.
When SOSL wins
Searching free text across several unrelated objects at once (like a global search box), when you don't know which object holds the match, or when you want fuzzy/tokenized text matching rather than exact equality — SOSL's search index is built for exactly this, where SOQL's LIKE '%term%' would be slow and can't span multiple objects in one query.
Same intent, two different tools
-- SOQL: precise, single-object, exact match
SELECT Id FROM Contact WHERE Email = 'ada@example.com'
-- SOSL: fuzzy, multi-object, text search
FIND {ada} IN ALL FIELDS RETURNING Contact, Lead, Account
Same rough intent — find something related to 'ada' — but a completely different tool depending on whether you know exactly what and where you're looking for.
Exercise
A support agent needs a single search box that looks up a customer's name across Accounts, Contacts, and Cases at once. Which query language fits, and why?
Show hint
Think about how many objects are involved and whether the exact match location is known in advance.
Choosing Between SOQL and SOSL — Quick Check
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
SOQL is the right tool when you know which object you need and want precise, structured results; SOSL is the right tool when you're searching text across multiple objects and don't know in advance where a match will be.