Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Use 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 this 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 this 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