Newer Version Available

This content describes an older version of this product. View Latest

Using Aliases with GROUP BY

You can use an alias for any field or aggregated field in a SELECT statement in a SOQL query. Use a field alias to identify the field when you’re processing the query results in your code.

Specify the alias directly after the associated field. For example, the following query contains two aliases: n for the Name field, and max for the MAX(Amount) aggregated field.

1SELECT Name n, MAX(Amount) max
2FROM Opportunity
3GROUP BY Name

Any aggregated field in a SELECT list that does not have an alias automatically gets an implied alias with a format expri, where i denotes the order of the aggregated fields with no explicit aliases. The value of i starts at 0 and increments for every aggregated field with no explicit alias.

In the following example, MAX(Amount) has an implied alias of expr0, and MIN(Amount) has an implied alias of expr1.

1SELECT Name, MAX(Amount), MIN(Amount)
2FROM Opportunity
3GROUP BY Name

In the next query, MIN(Amount) has an explicit alias of min. MAX(Amount) has an implied alias of expr0, and SUM(Amount) has an implied alias of expr1.

1SELECT Name, MAX(Amount), MIN(Amount) min, SUM(Amount)
2FROM Opportunity
3GROUP BY Name