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
Kauser FatimaKauser Fatima 

SOQL returns all the objects even though there are few objects in Salesforce satisfying the parent child relationship query.

I'm trying out a parent child relationship query for Account and Contacts.

SOQL: Select Id,Name,Description, (SELECT Id,AccountId,Email FROM Contacts WHERE Email = 'info@salesforce.com') From Account

Here my expectation would be I get only the Accounts for which the child objects email is info@salesforce.com. But Salesforce returns all the Account objects (the ones which do not satisfy the criteria for them Contacts is returned as null.)

Sample response:
{
    "totalSize": 2180,
    "done": false,
    "nextRecordsUrl": "/services/data/v40.0/query/01g1I00000SjHMFQA3-999",
    "records": [ {
            "attributes": {
                "type": "Account",
                "url": "/services/data/v40.0/sobjects/Account/0011I000005AswhQAC"
            },
            "Id": "0011I000005AswhQAC",
            "Name": "dfs",
            "Description": null,
            "Contacts": null
        },
        {
            "attributes": {
                "type": "Account",
                "url": "/services/data/v40.0/sobjects/Account/0011I000005FA6aQAG"
            },
            "Id": "0011I000005FA6aQAG",
            "Name": "Global Media",
            "Description": "GBM is the worldwide leader in technology news and information on the Web and the producer of the longest-running and farthest-reaching television shows about technology.",
            "Contacts": {
                "totalSize": 1,
                "done": true,
                "records": [{
                    "attributes": {
                        "type": "Contact",
                        "url": "/services/data/v40.0/sobjects/Contact/0031I000003583XQAQ"
                    },
                    "Id": "0031I000003583XQAQ",
                    "LastName": "White",
                    "AccountId": "0011I000005FA6aQAG",
                    "Email": "info@salesforce.com"
                }]
            }
        },...
]

Is this expected? Or can we workaround this so that we get only those parent objects for which the child object criteria is satisfied.

Any help would be appreciated.

Thanks
Steven NsubugaSteven Nsubuga
The subquery has no impact on the main query. That is how Salesforce was designed. Try this in stead
SELECT Id,AccountId, Account.Name, Account.Description, Email FROM Contacts WHERE Email = 'info@salesforce.com'