Alias Notations in SOQL : In our previous salesforce tutorial we have learned about how to write our first SOQL statement. In this salesforce training tutorial we are going to learn about Basic SOQL statement called Alias notation.
What is the alias notation in SOQL ?
Alias notation in SOQL statements are used to distinguish different object used in a single SOQL Statement.They are different types SOQL reserved keywords that can not be used in alias names or alias notations in SOQL. Some of the alias name are.
Here’s an overview of essential SOQL Keywords and Clauses in tabular format, with explanations and examples for each:
Keyword | Explanation | Example |
---|---|---|
AND | Combines multiple conditions, all of which must be true. | SELECT Name FROM Account WHERE Industry = 'Technology' AND AnnualRevenue > 1000000 |
ASC | Specifies ascending order in sorting results. | SELECT Name FROM Account ORDER BY CreatedDate ASC |
DESC | Specifies descending order in sorting results. | SELECT Name FROM Account ORDER BY CreatedDate DESC |
EXCLUDES | Filters results by excluding records with specific values in a multi-select picklist. | SELECT Name FROM Account WHERE Industry EXCLUDES ('Finance', 'Healthcare') |
FIRST | Fetches the first record from a sorted list. Useful in combination with ORDER BY . | SELECT Name FROM Account ORDER BY CreatedDate ASC LIMIT 1 |
FROM | Specifies the object from which to retrieve data. | SELECT Name FROM Account |
GROUP BY | Groups records based on specified fields. | SELECT Industry, COUNT(Id) FROM Account GROUP BY Industry |
HAVING | Filters aggregated results after grouping, similar to WHERE for grouped data. | SELECT Industry, COUNT(Id) FROM Account GROUP BY Industry HAVING COUNT(Id) > 5 |
IN | Filters results by matching any of the specified values in a list. | SELECT Name FROM Account WHERE Industry IN ('Technology', 'Manufacturing') |
INCLUDES | Filters results by including records with specific values in a multi-select picklist. | SELECT Name FROM Account WHERE Industry INCLUDES ('Finance', 'Technology') |
LAST | Retrieves the last record in a sorted list, often used with ORDER BY . | SELECT Name FROM Account ORDER BY CreatedDate DESC LIMIT 1 |
LIKE | Searches for records matching a pattern using wildcards (% ). | SELECT Name FROM Account WHERE Name LIKE 'Tech%' |
LIMIT | Restricts the number of returned records. | SELECT Name FROM Account LIMIT 10 |
NOT | Negates a condition, used to exclude results matching a specified condition. | SELECT Name FROM Account WHERE NOT Industry = 'Finance' |
NULL | Specifies fields without values. | SELECT Name FROM Account WHERE Description = NULL |
NULLS | Defines how null values are handled in sorting (NULLS FIRST or NULLS LAST ). | SELECT Name FROM Account ORDER BY CreatedDate ASC NULLS LAST |
OR | Combines multiple conditions where at least one condition must be true. | SELECT Name FROM Account WHERE Industry = 'Technology' OR AnnualRevenue > 1000000 |
SELECT | Initiates the query and specifies the fields to retrieve. | SELECT Name, Industry FROM Account |
WHERE | Filters results based on specified conditions. | SELECT Name FROM Account WHERE Industry = 'Technology' |
WITH | Filters based on specific data sharing settings (e.g., WITH SECURITY_ENFORCED ). | SELECT Name FROM Account WITH SECURITY_ENFORCED |
These keywords provide powerful ways to filter, sort, group, and manage retrieved data within Salesforce, making SOQL queries flexible and precise for various data retrieval scenarios.
Let us see an example to understand alias notations in SOQL.
- SELECT Acct.Id, Acct.name FROM Account Acct
From above SOQL statement we fetching Account Id, Account name records from the Standard object called Account. we have mentioned the word “ Acct”. “Acct” is the alias name for “Account”. We can fetch data directly with out using alis name in order to understand alias names in SOQL we are using alias names in this example.
- SELECT Firstname, lastname FROM Contact Con, Con.Account Acct WHERE Acct.name= ‘Ashok‘
From above SOQL Statement Acct is the alias name for “Account” Con is the alias name for “Contact”. We are fetching firstname and lastname from two objects called Contacts and Account. In both contacts and account we have firstname and lastname. Where is the condition to get the details from specific record called “Ashok”.
From above SOQL statement we fetching lastname and firstname from the Account and Contacts of Ashok. These alias name are helpful when writing SOQL queries for multiple objects. When writing SOQL statements for multiple objects many fields are common in all the objects so the alias names are helpful to distinguish the difference between the objects.