Alias notation in SOQL statements are used to distinguish different object used in a single SOQL Statement. To establish the alias, first we should identify the object and then specify the alias.

In Salesforce SOQL, we have some reserved keywords which are not used as alias names. They are:

  • AND
  • ASC
  • DESC
  • EXCLUDES
  • FIRST
  • FROM
  • GROUP
  • HAVING
  • IN
  • INCLUDES
  • LAST
  • LIKE
  • LIMIT
  • NOT
  • NULL
  • NULLS
  • OR
  • SELECT
  • WHERE
  • WITH

In this Salesforce Apex tutorial, we are going to learn about the usage of Alias notations in SOQL.

Different types of operators are used to filter the records retrieved via the SOQL query.

Example – Alias Notation in SOQL

Following is an example SOQL query, the object is Account and it’s alias name is Acct.

SELECT Acct.Id, Acct.name FROM Account Acct

From the above code, Acct is the Alias name for Account object. Here we fetching Account Id and Name  of the Account object. We can also fetch the data without out alias notation.

Output

Alias Notation in SOQL with Examples

In Salesforce.com, using SOQL statement we can fetch data using Alias notation from different objects. Let us understand with an example.

SELECT FirstName, LastName FROM Contact Con, Con.Account Acct WHERE Acct.Name = 'Genepoint'

As shown above, Con is the alias name for Contact and Acct is the alias name for Account object.

Alias Notation in SOQL with Examples

Here we fetching firstname and lastname from two standard objects called Contact and Account. In Salesforce.com, we have account name called ‘GenePoint’. As Contacts are associated with Accounts, account name list will be available in contacts.

Alias notations

Conclusion

In this Salesforce Developer Tutorial, we have learned about how to use SOQL Alias notations and how SOQL Alias notations are helpful to fetch data from multiple objects. In our next salesforce SOQL tutorial we will learn about SOQL IN operator.