How does inner query works in SOQL? Suppose I have a query as :

Select Account.Name,Account.Id,Id,Website from Contact

I know that Contact has a field AccountId which stores the ID of Parent Account but how can it access Account Fields which is stored in Accounts table.. How is this query implemented internally?Is the above query broken into 2 or more Sub Queries?


You access those fields as you might expect: contactRecord.account.name. This isn't a subquery in the context of Salesforce, just a normal relationship query. In this case, it's a "left join", where the contacts are listed, and the account data is related to it. If there are no contacts, no account will be returned, either. Conversely, if you wanted all accounts, and any of their contacts, you would use a subquery, such as select name, website, (select name from contacts) from account. In this case, you have a subquery, and returns all accounts and any contacts that exist. This would be more like a "right join" in regular SQL.

The difference is that the former returns all contacts and their account, if any, while the latter turns all accounts and their contacts, if any. In other words, in the former query, if there are accounts without contacts, they wouldn't show up in the report, while in the latter, if there were contacts without an account, they would not appear in the queyr.

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.