Preface: this post is part of the SOQL: A Beginner’s Guide series.
Whenever you’re traversing related objects in Salesforce, you’re always either going up or down.
Here are a few examples of upwards traversal:
SELECT Id, Account.Name, Account.Industry, Account.Website
WHERE Account.NumberOfEmployees >= 200
The above example might look familiar to you – if you built a workflow on Contacts, you’d be able to reference the contact’s account fields by using the same dot notation.
SELECT Account.Owner.Profile.CreatedBy.Name FROM Contact
You can traverse multiple levels upwards!
SELECT Id, Best_Friend__r.Likes_Ice_Cream__c FROM Contact
Here we’re traversing a custom lookup field Best_Friend__c on the contact object. Notice how the “__c” changes to a “__r” when traversing that field!