6

SOQL Keywords

 Keywords Used In SOQL Queries

Some Of The Major Keywords Used In SOQL Queries are: 

1. IN

The IN keyword is used to query bulk data, which means the WHERE condition involves a collection of records. The collection of records can be a Set, a List of IDs, or sObjects with populated ID fields.

Ex:

Set < Id > positionIdSet = new Set < Id > { 'a056000000WZEpq', 'a056000000WZYey' }; // Note: Hardcoding IDs is not a best practice. List < Position__c > positionList = [SELECT Name FROM Position__c WHERE Id IN: positionIdSet ];

Note: A Map cannot be used directly with the IN keyword. However, you can use Map.keySet() (returns a Set) or Map.values() (returns a List) with IN.

2. LIKE

LIKE keyword allows selective queries using wildcards. For example, to query all the position records where the Name starts with ‘John’, you can use a query such as the one given in the code here:

List < Position__c > positionList = [SELECT Name FROM Position__c WHERE Name LIKE 'John%' ];

3. AND/OR

With AND/OR keywords, you can apply AND/OR logic to your query conditions. For example, to query all the position records whose name starts with John and whose Status is open, use the query syntax as given here:

List < Position__c > positionList = [SELECT name FROM Position__c WHERE Name LIKE 'John%' AND Status__c = 'Open' ];

4. NOT

The NOT keyword is used for negation. For example, if you want to query all the Position records apart from the two Position Salesforce IDs in a given set, use NOT syntax as given here.

Set < Id > positionIdSet = new Set < Id > { 'a056000000WZEpq', 'a056000000WZYey' }; // Note: Hardcoding IDs is not a best practice. List < Position__c > positionList = [SELECT name FROM Position__c WHERE Id NOT IN: positionIdSet ];

5. ORDER BY

ORDER BY is used to sort the records in ascending(ASC) or descending(DESC) order. It is used after the WHERE clause. Also, you can specify if null records should be ordered at the beginning (NULLS FIRST) or end (NULLS LAST) of the results. By default, null values are sorted first.

List < Position__c > positionList = [SELECT Type__c, Comments__c FROM Position__c WHERE Status__c = 'Open' ORDER BY Type__c ASC NULLS LAST ];

6. GROUP BY

GROUP BY is used with Aggregate functions to group the result set by single or multiple columns. This keyword also groups results for a particular field with minimal code.

SELECT Status__c, COUNT(Name) FROM Position__c GROUP BY Status__c;

Position Status Number of Records
Open 24
Close 13
On Hold 7

7. HAVING

HAVING is an optional clause that can be used in a SOQL query to filter results that Aggregate functions return.

You can use a HAVING clause with a GROUP BY clause to filter the results returned by aggregate functions.

List < AggregateResult > agrRes = [SELECT LeadSource, COUNT(Name) FROM Lead GROUP BY LeadSource HAVING COUNT(Name) > 100 ];

8. LIMIT

The LIMIT keyword restricts the number of records returned by a SOQL query and is generally placed at the end of the query.

Student__c st1 = [SELECT Name FROM Student__c WHERE student_name__c = 'shrey' LIMIT 1];

9. FOR UPDATE

The FOR UPDATE keyword locks the queried records from being updated by any other Apex Code or Transaction. When the records are locked, other users cannot update them.

List < Position__c > positionList = [SELECT Name, Comments__c FROM Position__c WHERE Status__c = 'Open' FOR UPDATE ];

10. ALL ROWS

ALL ROWS keyword in a SOQL query retrieves all the records from the database, including those that have been deleted. This keyword is mostly used to undelete records from the Recycle Bin.

Kickstart-Your-Salesforce-Career CTA

Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

Frequently Asked Questions

What are the most commonly used SOQL keywords?

The most common SOQL keywords are: SELECT, FROM, WHERE, AND/OR, ORDER BY, LIMIT, GROUP BY, HAVING, IN/NOT IN, AND LIKE.

What does ORDER BY do in SOQL?

The ORDER BY SOQL keyword in Salesforce is used to sort results by one or more fields in either ascending or descending order.

What is LIMIT in SOQL?

The SOQL limit restricts the maximum number of queries that can be returned. It helps avoid Salesforce governor limits.

Can we use multiple keywords in one SOQL query?

Yes, multiple keywords can be used in a single query in a standard order (SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT). Only SELECT and FROM keywords are mandatory.

Book Free15-Minutes Career Counselling