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:

KeywordExplanationExample
ANDCombines multiple conditions, all of which must be true.SELECT Name FROM Account WHERE Industry = 'Technology' AND AnnualRevenue > 1000000
ASCSpecifies ascending order in sorting results.SELECT Name FROM Account ORDER BY CreatedDate ASC
DESCSpecifies descending order in sorting results.SELECT Name FROM Account ORDER BY CreatedDate DESC
EXCLUDESFilters results by excluding records with specific values in a multi-select picklist.SELECT Name FROM Account WHERE Industry EXCLUDES ('Finance', 'Healthcare')
FIRSTFetches the first record from a sorted list. Useful in combination with ORDER BY.SELECT Name FROM Account ORDER BY CreatedDate ASC LIMIT 1
FROMSpecifies the object from which to retrieve data.SELECT Name FROM Account
GROUP BYGroups records based on specified fields.SELECT Industry, COUNT(Id) FROM Account GROUP BY Industry
HAVINGFilters aggregated results after grouping, similar to WHERE for grouped data.SELECT Industry, COUNT(Id) FROM Account GROUP BY Industry HAVING COUNT(Id) > 5
INFilters results by matching any of the specified values in a list.SELECT Name FROM Account WHERE Industry IN ('Technology', 'Manufacturing')
INCLUDESFilters results by including records with specific values in a multi-select picklist.SELECT Name FROM Account WHERE Industry INCLUDES ('Finance', 'Technology')
LASTRetrieves the last record in a sorted list, often used with ORDER BY.SELECT Name FROM Account ORDER BY CreatedDate DESC LIMIT 1
LIKESearches for records matching a pattern using wildcards (%).SELECT Name FROM Account WHERE Name LIKE 'Tech%'
LIMITRestricts the number of returned records.SELECT Name FROM Account LIMIT 10
NOTNegates a condition, used to exclude results matching a specified condition.SELECT Name FROM Account WHERE NOT Industry = 'Finance'
NULLSpecifies fields without values.SELECT Name FROM Account WHERE Description = NULL
NULLSDefines how null values are handled in sorting (NULLS FIRST or NULLS LAST).SELECT Name FROM Account ORDER BY CreatedDate ASC NULLS LAST
ORCombines multiple conditions where at least one condition must be true.SELECT Name FROM Account WHERE Industry = 'Technology' OR AnnualRevenue > 1000000
SELECTInitiates the query and specifies the fields to retrieve.SELECT Name, Industry FROM Account
WHEREFilters results based on specified conditions.SELECT Name FROM Account WHERE Industry = 'Technology'
WITHFilters 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
Alias Notations in SOQL|Basic SOQL Statements
Alias Notations in SOQL|Basic SOQL Statements

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
Alias Notations in SOQL|Basic SOQL Statements
Alias Notations in SOQL|Basic SOQL Statements

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.