As you know, Salesforce uses Salesforce Object Query Language (SOQL) to handle its Database Management. But, what if you wanted to see SOQL example queries and wanted to know how SOQL Queries work ?
We are going to list SOQL Top 10 Query examples in this write-up, along with their short descriptions of working algorithm.
Table of Content
- Basic SOQL Query
- How to use the WHERE Clause ?
- How to use Operators to filter Data ?
- AND and OR
- SOQL Query to sort Output Results
- Limiting the Query Results
- Text Base SOQL Queries
- Outputting Real-Time Database Records
- Numbers in Queries
- Outputting Variable values from Database
Note: If you are in a hurry, you can click on above links to jump to the specific sections.
SOQL Query Examples (SOQL Top 10 Query Examples)
In the following lines, you can taste the top 10 SOQL query examples for informational purposes. Please note – you can play around queries for fetching different custom results from a Salesforce Database. Possibilities are Unlimited.
1. Basic SOQL Query
A Basic SOQL query returns field values from Database. The basic query helps you retrieve values from different Objects in Salesforce.
Here is a sample query example of a SOQL Basic Query.
SELECT Id, Name, PurchaseID FROM Account
In the above query, the ID, Name, and PurchaseID are examples of FieldList values.
SELECT count() FROM Account
2. How to use the WHERE clause ?
The WHERE clause is used to filter the output data. You can use the WHERE clause with SELECT statement.
Here is an example of the WHERE clause.
SELECT Number FROM Contact WHERE ID == 24
The above query helps you retrieve user data with default name. This is how we can filter output data with the WHERE clause. Possibilities are unlimited and you can use the WHERE clause in different variations. It depends on your needs.
3. How to use OPERATORS to filter Data ?
While you can use the WHERE clause to filter data, you can also use Operators to narrow filter data.
A WHERE clause may return multiple values if the conditions are met. Unlike WHERE, OPERATORS are used to laser target unique values. Please note, you can safely use OPERATORS inside a WHERE clause’s condition.
Consider the following example.
SELECT City FROM Account WHERE Phone != null
The above query will output the users’ records with a Phone field existed. In other words, the query will exclude users’ without having a Phone number on record.
4. AND and OR
Like Operators, you can also use AND or OR in WHERE clause. In simple words, both help you smartly filter data and help you take actions in a smarter way.
For example, by using AND, you can check for Two conditions in a single WHERE clause. Similarly, you can use OR to take an action if any of the Two conditions are met.
SELECT Value FROM Account WHERE Phone == 0 AND Number == 1
5. SOQL Query to Sort Output Results
If you need the results to be shown in Ascending or Descending order, you can use the SOQL Queries that help you sort output results.
Here is an example query that sorts results.
SELECT Name FROM Table WHERE Phone != null ORDER BY ID ASC
6. Limiting the Query Results
A single SOQL query may return hundreds or thousands of records from Database. If you need a limited number of results, you will need to use the LIMIT function in SOQL query.
The LIMIT function helps you limit the number of shown results. You can simply supply a fix number to define the number of returning records.
Here is an example of SOQL query with LIMIT function.
SELECT City FROM Table WHERE Phone != null LIMIT 5
By using the above query, you can only fetch and show the 5 results, no matter the returning results could cross thousands. In simple words, the LIMIT function helps you control the number of output results against a query.
7. Text-Based Queries
Sometimes, you will need to fetch Text values from database. In this case, you will need to enclose Text values within single quotes.
For example, analyse the following SOQL query.
SELECT Name FROM Table WHERE Phone != null AND Sex__c = ‘Female’
8. Fetching Real-Time Database Records
In many cases, you will need to fetch results based on current situations. For example, you may need to fetch users with a Birthday record of Today.
SELECT Name, Birthdate FROM Contact WHERE BirthDate = TODAY
9. Numbers in SOQL Queries
Unlike Text, you don’t need to use single quotes when using Numbers in SOQL Queries.
Here is an example of SOQL query with Numbers.
SELECT Name FROM Contact WHERE Sex__c = ‘Female’ AND Age == 30
10. Fetching Uncertain Records from Database
SOQL queries provide flexibility while dealing with different records. Here is an example That helps you fetch and show variable values from database.
SELECT Name FROM Contact WHERE Sex__c = ‘Female’ AND Phone LIKE ‘%(365)%’
Have you noticed, you will need to use the SELECT statement or clause in every SOQL query. Actually, the SELECT statement is the base of fetching data from a Database. You can get different variations of SELECT statement in this write-up.
With a mixture of WHERE, AND, OR, and LIMIT functions, you can customize the output results. Also, you will need to take care of the critical issues, such as single quotes for Text Based values.
Last but not least, you can also group together output results. For the said purpose, you will need to use brackets with a mixture of SOQL statements.