Comparison Operators (SOQL) #inSalesforce

Comparison operators return true or false in SOQL.


SOQL queries can include comparison operators like =, <, >, IN, and LIKE in the field expression of a WHERE clause, which you use in a SELECT statement.


1. = Equals

Expression is true if the value in the fieldName equals the value in the expression

Example

SELECT Name FROM Opportunity WHERE StageName = 'Closed Won'


2. != Not equals

Expression is true if the value in the fieldName doesn’t equal the specified value.

Example

SELECT Name FROM Opportunity WHERE StageName != 'Closed Won'


3. < Less than

Expression is true if the value in the fieldName is less than the specified value.

Example

SELECT Name FROM Opportunity where Amount < 100

Similarly,

<= Less or equal - Expression is true if the value in the fieldName is less than, or equals, the specified value.

Example

SELECT Name FROM Opportunity where Amount <= 100


> Greater than - Expression is true if the value in the fieldName is greater than the specified value.

Example

SELECT Name FROM Opportunity where Amount > 100


>= Greater or equal - Expression is true if the value in the fieldName is greater than or equal to the specified value.

Example

SELECT Name FROM Opportunity where Amount >= 100


4. LIKE - Expression is true if the value in the fieldName matches the characters of the text string in the specified value.

Example

SELECT AccountId, FirstName, lastname

FROM Contact

WHERE lastname LIKE 'test%'


5. IN - If the value equals any one of the values in a WHERE clause.

Example

SELECT Name FROM Account

WHERE BillingState IN ('California', 'New York')


6. NOT IN - If the value doesn’t equal any of the values in a WHERE clause.

Example

SELECT Name FROM Account

WHERE BillingState NOT IN ('California', 'New York')


7. INCLUDES and EXCLUDES - This applies only to multi-select picklists.

SELECT Id, Name FROM Candidate__c WHERE Skills__c INCLUDES ('salesforce')

SELECT Id, Name FROM Candidate__c WHERE Skills__c EXCLUDES ('salesforce')



Visual: https://www.youtube.com/watch?v=YK4eJmw-MD0



Reference: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_comparisonoperators.htm

Comments