In Salesforce, when we execute the SOQL queries, we define conditions to filter records based on specific criteria. In SOQL, we have logical operators such as AND, OR, and NOT, which help us to create complex filter conditions in SOQL queries. Using these operators in the SOQL queries is efficient, so that it returns only what is required.
In this Salesforce tutorial, we will learn about the Salesforce logical operators, AND, OR, and NOT Operators in SOQL. With the help of examples, we will discuss their execution in Salesforce SOQL queries.
Logical Operators in Salesforce SOQL
Logical operators in Salesforce SOQL are used in the WHERE clause to combine multiple filter conditions. They filter the conditions to include or exclude records in the query results. Logical operators allow for complex filtering logic by specifying whether all, any, or none of the conditions must be met. The logical operators used in SOQL are:
- AND – Ensures all combined conditions in the query are true.
- OR – This operator ensures that at least one of the conditions is true.
- NOT – Excludes records that match a specified condition in the query.
AND Logical Operator in SOQL
In Salesforce SOQL queries, the AND operator ensures that all conditions must be true for a record to match the criteria. When we combine the conditions using the AND operator, only the records that satisfy each defined condition will be selected.
Let’s understand the use case of an AND operator by executing an SOQL query to retrieve accounts where the billing city is New York, with an annual revenue greater than $1,00,000.
SELECT Name, AnnualRevenue, BillingCity
FROM Account
WHERE BillingCity = 'New York'
AND AnnualRevenue > 100000
After executing the above query, it only returned that record in which both conditions, ‘Billing City’ = New York and the AnnualRevenue > 100000, were true.
OR Logical Operator in SOQL
In Salesforce SOQL queries, the OR operator retrieves the records that meet at least one of the conditions to match the criteria. When we define two or more conditions in a query using the OR operator, it will return the records if they match any of the conditions.
Let’s understand it with the help of an example, where we will retrieve opportunities that are either in the “Prospecting” stage OR have a probability greater than 50%. In the output, we will find the records that satisfy either of the defined conditions.
SELECT Name, StageName, Probability
FROM Opportunity
WHERE StageName = 'Prospecting'
OR Probability > 50
LIMIT 10Output:

As we can see, the SOQL query has retrieved the opportunities either in the Prospecting stage or have a Probability of more than 50.
NOT Operator in Salesforce SOQL
In Salesforce SOQL, the NOT operator is used in the query to exclude records that meet a specific condition.
In the example below, we will execute an SOQL query to retrieve leads not located in New York.

In the output, we can see that the query fetched all the lead records where the city is not New York.
Combining the AND, OR, and NULL Operators
In the example below, we will combine all the logical operators (AND, OR, and NULL) to create more complex condition filters in an SOQL query.
In this SOQL query, we will retrieve contacts who are in California AND have the title of “Manager” OR those who are not in California but are associated with an account that has an annual revenue greater than $1,00,000.
SOQL query:
SELECT Name, Title, Customer_State__c, Account.AnnualRevenue
FROM Contact
WHERE (Customer_State__c = 'California' AND Title = 'Manager')
OR (Customer_State__c != 'California' AND Account.AnnualRevenue > 1000000)Output:

This way, we can use the logical operators (AND, OR, and NOT) in Salesforce SOQL queries.
Conclusion
In this Salesforce tutorial, we have learned the logical operators AND, OR, and NOT with their use-case examples. Logical operators in Salesforce SOQL make it easy to filter records based on multiple conditions.
With AND, you can combine strict conditions, OR allows flexibility, and NOT lets you exclude specific results. By the above syntax, you can use logical operators and write SOQL queries efficiently.
You may also like to read:
- Selective SOQL Queries in Salesforce
- Salesforce SOQL Inner Join and Outer Join Relationships
- Salesforce SOQL Distinct Queries
- LIKE Clause in SOQL
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.