function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdc550sfdc550 

SOQL Query doubt

I am new to development and SOQL

how do we write a query on contact object to fetch account details?

How do u write a query on account object to fetch contact details?


These are not exactly my soql but related to this my objects are same relationships but different named objects.

please help me out
David ZhuDavid Zhu
Select name,(select name from contacts) from account where name like 'aa%'

Note: "contacts" is plural.  Account and Contact objects are master detail relationship. subquery on detail object using plural. hope this helps.
William TranWilliam Tran
how do we write a query on contact object to fetch account details?

This is known as query "upward": 
 
SELECT Id, Account.Name, Account.Industry, Account.Website
FROM Contact

How do u write a query on account object to fetch contact details?

This is known as query "downward":
 
SELECT Id, Name, Industry, AnnualRevenue,
( SELECT Name, Email, BirthDate FROM Contacts )
FROM Account

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks