By using SOQL you can create a simple but very powerful query string, below are some of the SOQL environments:
- In the Apex Statements.
- In the Schema Explorer of the Force.com IDE.
- In the Query String parameter in the query() call.
- In the Visualforce controllers and getter methods.
As we write SELECT command in the Structured Query Language (SQL), SOQL also allows us to specify the source object, a list of fields to retrieve, and conditions for selecting rows in the source object.
[NOTE: SQL supports many advanced features of SELECT command but SOQL don’t. For example, You cannot use SOQL to perform arbitrary join operators, using calculation expressions, or using wildcards in field lists.
]
SOQL SELECT statement combined with filtering statements to return sets of data
SYNTAX:
SELECT one or more fields FROM an object WHERE filter statements and, optionally, results are ordered
EXAMPLE:
SELECT Name, Type, Phone FROM Account WHERE Name = ‘United Oil & Gas Corp.’
NOTE
- Apex takes SOQL and SOSL statements within square brackets to use them on the fly. Variable and expressions of Apex scripts preceded by a colon(:).
- SOQL is used when we know which objects having data and by using SOQL, we can retrieve data from a single object or from multiple objects that are related to one another. We can count the number of records that meet specified criteria. And sorting the results as a part of the query, and also retrieving data from number, date, or checkbox fields.