Salesforce Apex Tutorial Chapter 19: Apex SOQL Query
SOQL stands for Salesforce Object Query Language. SOQL can be used to access information stored in your organization’s database. The syntax of SOQL is similar to that of SQL (Structured Query Language).SOQL queries can be written in Apex code or in the Query Editor of the Developer Console. In Soql records can only be searched on a single sObject if it meets the given criteria. Unlike SOSL, it cannot search across multiple objects but nested queries are supported in Soql.
To work on Soql you must have records in the database. So, before starting working on Soql follow the below steps to insert some records via apex.
Step 1: Open the developer console and click on the Debug tab to open the anonymous window as shown below the image.
Using the Query Editor, we can now write and execute a SOQL query to fetch the above-inserted records. A SOQL query searches for data in a specific object and optionally, you may add a condition to the WHERE clause to filter your search.
In the image below we have queried the Account and its associated contact and opportunity records and filtered the result by Account Name.
- SOQL allows you to query records from Standard and Custom objects in one go.
- SOQL allows querying on a single object or multiple objects that are related to one another.
- SOQL supports aggregate functions just like SQL. We can roll up and summarize data with aggregate functions. For example, Avg, Min, Max, etc.
- SOQl allows binding apex variables in the query to filter the records.
- SOQl supports Parent to Child and Child to Parent queries.