Find your content:

Search form

You are here

SOQL: Get Most Recent Child of Parent


I need select the Email off the most recently created Contact for all of my Accounts to export via the Data Loader. So far I have this but then I'm stuck. I keep thinking of functions like MAX(CreatedDate) but how the might help me just isn't turning on a light bulb, any ideas?

SELECT Email from Contact where Account in (Select Id from Account) Order By CreatedDate

Attribution to: mcwhittemore

Possible Suggestion/Solution #1

List <Account> accounts = [SELECT Id, (SELECT Email FROM Contacts ORDER BY CreatedDate DESC LIMIT 1) FROM Account];

Then you can use:

for(Account acc : accounts)
    if(acc.Contacts.size() > 0)
        String accountEmail = acc.Contacts[0].Email;
        // do whatever you need to do with the email...

Attribution to: Boris Bachovski
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content