In this Salesforce Object Query language SOQL tutorial, we are going to learn about IN operator in SOQL statements and why we use IN operator in WHERE clause.

SOQL IN Operator

SOQL IN Operator is used to fetch the data from the matched values specified in the the SOQL statement. SOQL IN operator is mainly used to compare a value to a list of values that have been specified, and it retrieves the records if it matches the values specified in the list. The IN operator is used if you want to compare a value with multiple values to ensure the retrieved records are accurate.

Example – SOQL IN Operator

In this example, we will use IN operator in WHERE expression to filter the rows.

SELECT Firstname, Lastname FROM user WHERE firstname IN ('adarsh','Prasanth')
SOQL In Operator

From above SOQL query, the preceding query will return all users where the firstname name equals to ‘adarsh’ and ‘Prasanth’. As shown above the values for IN must be in parenthesis and string values must be added in between single quotes. IN and NOT IN operators are also used for semi-joins and anti-joins.

SOQL NOT IN Operator

SOQL NOT IN operator is similar to NOT operator. This operator is used to specify multiple values in the WHERE clause for non matching and filtering records. This operator retrieve the data if the values does not equal to any of the specified values in a WHERE clause.

Example – SOQL NOT IN operator

In this example, we will use NOT IN operator in WHERE expression to filter the rows.

SELECT Firstname, Lastname FROM user WHERE firstname NOT IN ('Prasanth')

Output

NOT IN Operator

As shown above, the result will not contain any user which equals to ‘Prasanth’. 

Conclusion

In this Salesforce developer tutorial, we have learned about SOQL IN operator and SOQL NOT IN operator. In our upcoming SOQL tutorials, we learn about relationship between custom objects in SOQL.