For Salesforce Admins and Developers, SOQL (Salesforce Object Query Language) and SOSL (Salesforce Object Search Language) are essential tools. They help you search, filter, and manage data across the platform, powering automation, reporting, and custom development. Whether you’re building Apex code, crafting dynamic dashboards, or just exploring data relationships, a deep understanding of these query languages gives you a strong edge.
This guide walks you through key concepts, advanced techniques, and best practices to help you use SOQL and SOSL with confidence.
Why SOQL and SOSL Matter
Both SOQL and SOSL enable powerful interactions with your Salesforce data:
-
Retrieve specific records based on flexible criteria
-
Power dynamic Apex logic, batch classes, and triggers
-
Search across multiple objects in a single query
-
Build efficient and scalable reports
-
Optimize performance by narrowing query scope
SOQL Basics: What You Need to Know
SOQL is similar to SQL but object-oriented. You use it to fetch specific fields from standard or custom Salesforce objects. It’s perfect when you know where to look and what to retrieve.
Example:
SELECT FirstName, LastName, Email FROM Contact WHERE LastName = 'Smith'
This query returns a list of contacts whose last name is Smith.
Key Components of SOQL
-
SELECT: Fields to retrieve
-
FROM: Object to query
-
WHERE: Conditions for filtering
-
ORDER BY: Sort results
-
LIMIT: Cap the number of returned records
Example:
SELECT Name, Industry FROM Account WHERE Industry = 'Technology' ORDER BY Name LIMIT 10
SOSL Overview: Search Across Objects
SOSL allows you to search text, email, phone, and name fields across multiple objects in one go. Unlike SOQL, it doesn’t require object relationships.
Example:
FIND 'Acme' IN ALL FIELDS RETURNING Account(Name), Contact(FirstName, LastName)
This searches both Account and Contact objects for records that match the word “Acme.”
Benefits of SOSL
-
Fuzzy keyword search
-
Cross-object search
-
Useful for global search interfaces
Advanced SOQL Techniques
Aggregation
Use aggregate functions like COUNT()
, SUM()
, or AVG()
for grouped summaries.
Example:
SELECT Account.Name, COUNT(Id) FROM Contact GROUP BY Account.Name
This counts the number of Contacts per Account.
Relationship Queries
SOQL supports two directions:
-
Child-to-Parent (Upward)
SELECT Name, Account.Name FROM Contact WHERE Account.Industry = 'Finance'
Retrieves contacts with their account names. -
Parent-to-Child (Downward)
SELECT Name, (SELECT LastName FROM Contacts) FROM Account WHERE Industry = 'Finance'
Fetches accounts with a list of associated contacts.
Subqueries
Use subqueries to filter one object based on related object criteria.
Example:
SELECT Name FROM Account WHERE Id IN (SELECT AccountId FROM Contact WHERE LastName = 'Doe')
Fetches all Accounts that have a Contact with the last name Doe.
Advanced SOSL Techniques
Wildcards and Logic
SOSL supports wildcards (*
, ?
) and logical operators like AND
and OR
.
Example:
FIND 'Acme*' IN NAME FIELDS RETURNING Account(Name)
Returns accounts with names starting with Acme.
Multi-Object RETURNING
SOSL allows you to retrieve fields from multiple objects simultaneously.
Example:
FIND 'Acme' IN ALL FIELDS RETURNING Account(Name), Contact(FirstName, LastName)
Returns matching records from both Accounts and Contacts.
SOQL vs. SOSL: When to Use What
Feature | SOQL | SOSL |
---|---|---|
Query Scope | Single object or related objects | Multiple unrelated objects |
Search Method | Structured queries | Full-text search |
Best For | Exact match retrieval | Broad, keyword-based searches |
Performance | Fast with targeted filters | Slower due to wider search scope |
Use SOQL when you know exactly what data you’re retrieving. Use SOSL when you’re not sure where that data lives.
Common Use Cases
-
SOQL: Fetch opportunities, contacts, or accounts for record detail pages
-
SOQL with Aggregates: Generate reports showing average deal size or total revenue
-
SOSL: Create global search bars or search screens in Lightning Components
Best Practices for Queries
-
Always SELECT only required fields to reduce data load
-
Use WHERE filters to minimize unnecessary results
-
Apply LIMIT in Apex to stay within governor limits
-
Prefer relationship queries over multiple independent queries
-
Use indexed fields in filters for performance
-
Avoid deeply nested subqueries unless necessary
Troubleshooting SOQL and SOSL Issues
-
Too Many SOQL Queries: Combine logic and use collections
-
NullPointerException: Check if related records exist before accessing them
-
Query Timeout (SOSL): Add filters or limit object scope
-
Unexpected Empty Results: Confirm record visibility, field-level security, and sharing rules
FAQs
Q: Can I mix SOQL and SOSL in the same query?
No, they must be executed separately.
Q: Can SOQL perform fuzzy searches like SOSL?
No. SOQL is best for exact matches and structured queries.
Q: Which is better for cross-object queries?
Use SOQL for related objects. Use SOSL for unrelated objects or full-text search.
Q: Are there governor limits for queries?
Yes. Apex allows a maximum of 100 SOQL queries and 20 SOSL queries per transaction.
Q: Can I run these queries outside of code?
Yes! Use the Developer Console, Query Editor in Setup, or tools like Workbench.