Curriculum
Filtering Records is one of the most important features of SOQL. Instead of retrieving every record from a Salesforce object, developers can use filtering conditions to retrieve only the records that match specific criteria. Filtering improves performance, reduces data processing, enhances application efficiency, and helps developers stay within Salesforce Governor Limits.
In real-world Salesforce applications, businesses rarely need all records. They often require specific data such as active customers, qualified leads, open opportunities, enrolled students, pending payments, or recently created records. SOQL filtering makes these requirements possible.
Understanding how to filter records effectively is essential for Salesforce Developers, Administrators, Consultants, and Platform App Builders.
Record Filtering is the process of retrieving only records that meet specific conditions.
Instead of:
All Records
You retrieve:
Matching Records Only
Filtering helps users find relevant information quickly.
Organizations often store:
Retrieving all records can:
Filtering improves efficiency and scalability.
Retrieve only required records.
Reduce unnecessary processing.
Display relevant information.
Process fewer records.
Focus on meaningful data.
These benefits make filtering essential in Salesforce development.
The WHERE Clause is used to filter records in SOQL.
General Syntax:
SELECT fields
FROM ObjectName
WHERE condition
The WHERE clause specifies which records should be returned.
SELECT Name
FROM Account
WHERE Industry = 'Software'
Result:
| Name | Industry |
|---|---|
| Groot Software | Software |
| Tech Solutions | Software |
Only Software industry accounts are returned.
Filtering relies on comparison operators.
| Operator | Description |
|---|---|
| = | Equal To |
| != | Not Equal To |
| > | Greater Than |
| < | Less Than |
| >= | Greater Than or Equal |
| <= | Less Than or Equal |
These operators define filtering conditions.
Returns records matching an exact value.
Example:
SELECT Name
FROM Student__c
WHERE Course__c = 'Salesforce'
Result:
Only Salesforce students are returned.
Returns records that do not match a value.
Example:
SELECT Name
FROM Student__c
WHERE Course__c != 'Salesforce'
Result:
Students enrolled in other courses are returned.
Returns values greater than the specified value.
Example:
SELECT Name,
Fee__c
FROM Student__c
WHERE Fee__c > 10000
Only students with fees above ₹10,000 are returned.
Returns values below the specified value.
Example:
SELECT Name,
Fee__c
FROM Student__c
WHERE Fee__c < 10000
Only lower-fee records are returned.
Example:
SELECT Name,
Marks__c
FROM Student__c
WHERE Marks__c >= 75
Students scoring 75 or above are returned.
Example:
SELECT Name,
Balance__c
FROM Student__c
WHERE Balance__c <= 0
Returns students with no pending balance.
Example:
SELECT Name
FROM Account
WHERE Name = 'Groot Software'
Only matching Account records are returned.
Example:
SELECT Name,
Revenue__c
FROM Account
WHERE Revenue__c > 500000
Returns high-revenue accounts.
Boolean fields store:
true
false
Example:
SELECT Name
FROM Student__c
WHERE Active__c = true
Returns active students only.
SOQL supports date filtering.
Example:
SELECT Name,
CreatedDate
FROM Account
WHERE CreatedDate = TODAY
Returns records created today.
Salesforce provides built-in date values.
Examples:
Date literals simplify date filtering.
SELECT Name
FROM Account
WHERE CreatedDate = TODAY
Returns today’s records.
SELECT Name
FROM Opportunity
WHERE CloseDate = THIS_MONTH
Returns opportunities closing this month.
Multiple conditions can be combined.
Logical Operators:
These operators create advanced filters.
Both conditions must be true.
Example:
SELECT Name
FROM Student__c
WHERE Course__c = 'Salesforce'
AND Fee_Paid__c = true
Result:
Only students who meet both conditions are returned.
At least one condition must be true.
Example:
SELECT Name
FROM Student__c
WHERE Course__c = 'Salesforce'
OR Course__c = 'Python'
Returns students enrolled in either course.
Excludes matching records.
Example:
SELECT Name
FROM Student__c
WHERE NOT Course__c = 'Salesforce'
Returns non-Salesforce students.
Parentheses improve readability and control logic.
Example:
SELECT Name
FROM Student__c
WHERE
(
Course__c = 'Salesforce'
OR Course__c = 'Python'
)
AND Active__c = true
This creates precise filtering rules.
IN compares multiple values.
Example:
SELECT Name
FROM Student__c
WHERE Course__c IN
(
'Salesforce',
'Python',
'Java'
)
Equivalent to multiple OR conditions.
Example:
SELECT Name
FROM Student__c
WHERE Course__c NOT IN
(
'Salesforce',
'Python'
)
Returns students enrolled in other courses.
LIKE performs partial text matching.
Example:
SELECT Name
FROM Account
WHERE Name LIKE 'Groot%'
Returns:
Groot Software
Groot Academy
Useful for text searches.
Represents multiple characters.
Example:
WHERE Name LIKE '%Tech%'
Matches:
Wildcards increase search flexibility.
NULL means no value exists.
Example:
SELECT Name
FROM Student__c
WHERE Email__c = NULL
Returns students without email addresses.
Example:
SELECT Name
FROM Student__c
WHERE Email__c != NULL
Returns students with email addresses.
Example:
SELECT Name
FROM Student__c
WHERE Status__c = 'Active'
Only active students are returned.
SOQL filters can use Apex variables.
Example:
String courseName =
'Salesforce';
List<Student__c> students =
[
SELECT Name
FROM Student__c
WHERE Course__c =
:courseName
];
This is called Variable Binding.
A software training company wants all active Salesforce students.
Query:
SELECT Name,
Email__c
FROM Student__c
WHERE Course__c = 'Salesforce'
AND Active__c = true
Result:
| Name | |
|---|---|
| Rahul Sharma | rahul@email.com |
| Priya Singh | priya@email.com |
Management receives only relevant student data.
Improve performance.
Optimize execution.
Reduce processing.
Ensure accuracy.
Verify correctness.
These practices improve query efficiency.
Developers should avoid these mistakes.
Understanding Filtering Records helps professionals:
Filtering is one of the most frequently used SOQL skills.
Filtering Records allows Salesforce professionals to retrieve only the data that matches specific conditions. Through the WHERE clause, comparison operators, logical operators, IN, LIKE, NULL checks, date literals, and variable binding, SOQL provides powerful filtering capabilities. Effective filtering improves performance, scalability, and data accuracy, making it a critical skill for Salesforce development and administration.
The WHERE clause filters records based on specified conditions.
The = operator checks whether values are equal.
LIKE performs partial text matching using wildcards.
IN compares a field against multiple values.
Yes. SOQL supports date filtering and date literals such as TODAY and THIS_MONTH.
Filtering improves performance and retrieves only relevant records.
Looking to learn more technologies and programming skills?
Send us your inquiry and start your journey towards a successful IT career.
✔ 100% Secure ✔ No Spam ✔ Instant Response
WhatsApp us