9

Here's a somewhat odd SOQL query from the question query user and profile:

select user.id, user.Email,user.FirstName,user.LastName,user.profile.name,user.Username,user.IsActive 
from user, user.profile

Note that the FROM clause includes both user and user.profile.

Here is a simpler example to follow the Contact to Account relationship:

select Id, Name, Account.Name from Contact, Contact.Account

The same query with the relationship join can be performed with:

Select Id, Name, Account.Name From Contact

Is there any purpose to supporting additional SObject types in the from clause, or is it vestigial?

Is is outlined as supported syntax in SOQL.

SELECT fieldList [subquery][...]
[TYPEOF typeOfField whenExpression[...] elseExpression END][...]
FROM objectType[,...]
[USING SCOPE filterScope]

Out of interest, if you don't use a valid sObject relationship on the additional sObject types you get the following message:

INVALID_TYPE:   
Name, Account.Name From Contact, Account  
                                 ^
ERROR at Row:1:Column:45
A driving SObject type has already been set, all other entity types in the FROM clause must 
be relationships to the initial object.  The driving object is Contact.

While doing some checking I found this old dev forum question that indicated it was added to the syntax in Winter '15. Or at least documented then. - Missing doc for new SOQL multiple object SELECT?

  • Ah, I finally know how this strange syntax can actually be used. No idea on what it actually does though. Maybe it helps with choosing indices to use, or helps disambiguate multiple references to the same related SObject? – Derek F Apr 9 '19 at 1:26
  • @DerekF Currently is seems redundant. Like it was maybe a left over from the SQL query origins. I'm still not sure what it would help disambiguate. Maybe for polymorphic relationships? – Daniel Ballinger Apr 9 '19 at 1:36
9

One possible use is to use is to combine it with the Alias notation for the related sObjects.

E.g.

select c.Id, c.Name, ca.Name, art.Name 
from Contact c, Contact.Account ca, Contact.Account.RecordType art

If you wanted to query many fields from the related sObject then you could save a significant number of characters if you are running up against the 20,000 character limit.

|improve this answer|||||
  • 2
    Even if you don't care about the query limit, it can be nice to save all that typing! – Adrian Larson Apr 9 '19 at 17:01

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.