41

I am wondering how you would do something simple like this in SOQL.

select first_name as name from tableA

I can not find any documentation on Aliasing a column which is very common in all forms of SQL, does anyone know if this is possible?

43

SOQL has very limited aliasing support. You can alias the table, e.g. SELECT a.name FROM Account a, but that's of very little use. In most contexts you cannot alias fields.

The one exception is for queries that use aggregations, like SELECT COUNT(Id) recordCount FROM Account allows you to alias the result of the aggregation from it's default expr0 to recordCount.

This probobly doesn't do what you're looking for, and you'll have to use some sort of transform in apex, middleware, or on the API client (depending on what and how you're building) to alias columns in non-aggregate use cases.

| improve this answer | |
13

You can use object aliasing in any SOQL query

From the SOQL reference guide.....

Alias Notation

You can use alias notation in SELECT queries: SELECT count() FROM Contact c, c.Account a WHERE a.name = 'MyriadPubs'

To establish the alias, first identify the object, in this example a contact, and then specify the alias, in this case “c.” For the rest of the SELECT statement, you can use the alias in place of the object or field name.

The following are SOQL keywords that can’t be used as alias names: AND, ASC, DESC, EXCLUDES, FIRST, FROM, GROUP, HAVING, IN, INCLUDES, LAST, LIKE, LIMIT, NOT, NULL, NULLS, OR, SELECT, WHERE, WITH

See page 2 of the reference guide here

You can only use field aliasing with aggregate expressions. For more info on aliasing with aggregates, take a look here

http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_select_groupby_alias.htm

| improve this answer | |
  • 3
    Note that this is an object alias, not a field alias. Salesforce only allows field aliasing in aggregate expressions. i.e. GROUP BY – Mark Pond Jun 2 '14 at 21:55
  • 3
    Indeed. I didn't make that clear, will edit the answer to reflect. Thanks – Chris Duncombe Jun 2 '14 at 21:56
  • 1
    @ChrisDuncombe what is the point of object aliasing? Is there any practical use case in SOQL – Eric Apr 28 '17 at 21:43
8

As Chris mentioned, you can only alias aggregated fields. The syntax to use is like this:

SELECT Status, COUNT(id) Total
FROM Case
GROUP BY Status

Notice, no "AS" keyword like in vanilla SQL

| improve this answer | |
4

Aliasing like this:

select first_name as name from tableA

isn't possible in Salesforce SOQL.

| improve this answer | |
3

Alias notation also works with the toLabel() function in both SOQL and SOSL queries.

The toLabel function supports aliasing. In addition, aliasing is required when the query includes the same field multiple times. For example:

FIND {Joe} RETURNING Lead(company, toLabel(Recordtype.Name) AliasName)

SOSL Reference: toLabel(fields)

The above excerpt, shows the use of the alias notation with toLabel function in a SOSL query, however, the same syntax works for SOQL queries.

For example, if we have the following Account records in Salesforce:

+--------------+--------------------+
| Name         | BillingCountryCode | 
+--------------+--------------------+
| A            | US                 |
| B            | IE                 |
| C            | CA                 |
+--------------+--------------------+

Without the toLabel alias we would only be able to select one of BillingCountryCode or toLabel(BillingCountryCode). If we tried to select both without an alias we would run into an error:

duplicate field selected: BillingCountryCode

Now, if we use alias notiation, we can query for both the Billing Country Code as well as the label as follows:

SELECT
    Name,
    BillingCountryCode,
    toLabel(BillingCountryCode) BillingCountryLabel
FROM Account

Then the above query would produce a query result as follows:

+--------------+--------------------+---------------------+
| Name         | BillingCountryCode | BillingCountryLabel | 
+--------------+--------------------+---------------------+
| A            | US                 | United States       |
| B            | IE                 | Ireland             |
| C            | CA                 | Canada              |
+--------------+--------------------+---------------------+
| improve this answer | |
  • 1
    Interestingly the toLabel(field) alias syntax still allows you to return the usual SObject type back instead of AggregateQuery. The label data is only accessible via .get('fieldName') though of course. – Charles T May 3 '18 at 11:58
  • Only picklist fields, field history, or platform entity fields (or Record Type Name, Description in the SELECT list) support translation. =( – Jeremy A. West Sep 16 '19 at 21:01
  • I did the same querry but I am not able to access to Opportunity.BillingCountryLabel – Raphael D Dec 16 '19 at 12:28
  • 1
    @RaphaelD You need to use SObject.get() to access the aliased label. e.g String label = (String) opp.get('BillingCountryLabel') – Alan Morey Dec 16 '19 at 20:02
2

Directly write the alias name for example: select AccountID account ,count(id) countid from Account

you can also retrieve using get() function by passing the alias name in ' ' quotes example: object_reference.get(;

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.