# Parent and Child Relationship Filters

| AVAILABLE API VERSION |
| --------------------- |
| API v56.0 and later   |

In [Field Operators](/docs/platform/graphql/guide/filter-fields.md) and [Boolean Operators](/docs/platform/graphql/guide/filter-boolean.md), we discussed how fields map to the filter type and how to combine filters with the `AND`, `OR`, and `NOT` functions. The sObject is also added to the filter type for each parent relationship.

Objects can have a child-to-parent relationship or a parent-to-child relationship. For example, the Contact object has a child-to-parent relationship to the Account object. An Account has parent-to-child relationships to Assets, Cases, and Contacts, and other objects.

## Child-to-Parent Relationships

Child-to-parent relationships can be many-to-one, just like multiple child contacts can be on a parent account.

```sfdocs-code {"lang":"graphql", "title": "Query contacts for a given account industry", "src": "" }
query {
  uiapi {
    query {
      Contact (where: { Account: {
        Industry: { eq : "media" } } } ) {
          edges {
            node {
              Id
              Name { value }
              Account {
                Name { value }
              }
            }
          }
        }
    }
  }
}
```

The previous query is similar to this SOQL statement.

```sfdocs-code {"lang":"sql", "title": "SOQL statement showing child-to-parent relationship", "src": "" }
SELECT Id, Name, Account.Name
FROM Contact
WHERE Account.Industry = 'media'
```

For some objects that can have a parent relationship with the same object, you can query the parent relationship using the `Parent` field. For example, an account can have a parent account and a campaign can have a parent campaign.

```sfdocs-code {"lang":"graphql", "title": "Query accounts and parent accounts", "src": "" }
query AccountWithNameAndParent {
    uiapi {
        query {
            Account(where: { Name: { like: "Express%" } }) {
                edges {
                    node {
                        Id
                        Name {
                            value
                        }
                        Parent {
                            Id
                            Name {
                                value
                            }
                        }
                    }
                }
            }
        }
    }
}
```

Accounts without parent account records are also returned. The previous query returns this example response.

```sfdocs-code {"lang":"json", "title": "Response for query on accounts and parent accounts", "src": "" }
{
  "data": {
    "uiapi": {
      "query": {
        "Account": {
          "edges": [
            {
              "node": {
                "Id": "001RM000005ZdMLYA0",
                "Name": {
                  "value": "Express Logistics",
                  "displayValue": null
                },
                "Parent": {
                  "Id": "001RM000005Zcl7YAC",
                  "Name": {
                    "value": "Express Logistics and Transport"
                  }
                }
              }
            },
            {
              "node": {
                "Id": "001RM000005Zcl7YAC",
                "Name": {
                  "value": "Express Logistics and Transport",
                  "displayValue": null
                },
                "Parent": null
              }
            }
          ]
        }
      }
    }
  },
  "errors": []
}
```

Here’s a query that returns cases with their parent accounts. Cases without parent accounts are also returned by the query.

```sfdocs-code {"lang":"graphql", "title": "Query cases with parent accounts", "src": "" }
query CasesWithParent {
    uiapi {
        query {
            Case {
                edges {
                    node {
                        Subject {
                          value
                        }
                        Parent {
                            Id
                            Account {
                                Name {
                                  value
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
```

You can filter records to return only those accounts without a null Parent Id value.

```sfdocs-code {"lang":"graphql", "title": "Query accounts with parent accounts that are not null", "src": "" }
query  {
  uiapi {
    query {
      Account (where: { Parent: { Id: { ne: null }}}) {
        edges {
          node {
            Name { value }
            Parent {
              Name { value }
            }
          }
        }
      }
    }
  }
}
```

The previous query is similar to this SOQL statement.

```sfdocs-code {"lang":"soql", "title": "SOQL statement with Parent fields", "src": "" }
SELECT Name, Parent.Name
FROM Account
WHERE Parent.Id!=null
```

Only accounts with parent account records are returned. The previous query returns this example response.

```sfdocs-code {"lang":"json", "title": "Response for query on accounts and parent accounts (no null values)", "src": "" }
{
  "data": {
    "uiapi": {
      "query": {
        "Account": {
          "edges": [
            {
              "node": {
                "Name": {
                  "value": "United Oil &amp; Gas Corp"
                },
                "Parent": {
                  "Name": {
                    "value": "United Oil &amp; Gas, UK"
                  }
                }
              }
            }
          ]
        }
      }
    }
  },
  "errors": []
}
```

## Parent-to-Child Relationships

Parent-to-child relationships are usually one-to-many, just like a parent account can have many child contacts. To include subqueries for standard objects, specify the plural form of the object name, which is the name of the relationship for the object. For example, by selecting from Account, you can query the children records via the Cases, Contacts, and Opportunities fields.

Let’s say you want to query cases on an account.

```sfdocs-code {"lang":"graphql", "title": "Query cases on account records", "src": "" }
query  {
  uiapi {
    query {
      Account {
        edges {
          node {
            Id
            Name { value }
            Cases {
              edges {
                node {
                  Subject { value }
                  Priority { value }
                }
              }
            }
          }
        }
      }
    }
  }
}
```

The previous query is similar to this SOQL statement.

```sfdocs-code {"lang":"soql", "title": "SOQL statement with a subquery on cases", "src": "" }
SELECT Name,
  (
    SELECT Subject, Priority
    FROM Cases
  )
FROM Account
```

You can also query account names and the last name of contacts on each account.

```sfdocs-code {"lang":"graphql", "title": "Query accounts with child contacts", "src": "" }
query  {
  uiapi {
    query {
      Account {
        edges {
          node {
            Name { value }
            Contacts {
              edges {
                node {
                  LastName { value }
                }
              }
            }
          }
        }
      }
      }
  }
}
```

The previous query is similar to this SOQL statement.

```sfdocs-code {"lang":"sql", "title": "SOQL statement showing parent-to-child relationship", "src": "" }
SELECT Name,
  (
    SELECT LastName
    FROM Contacts
  )
FROM Account
```

Some objects have children relationships to the same object. For example, use the `ChildAccounts` field to query children accounts on account records. Accounts without children accounts are also returned.

```sfdocs-code {"lang":"graphql", "title": "Query accounts and children acccounts", "src": "" }
query AccountWithChildrenAccounts {
  uiapi {
      query {
          Account(where: { Name: { like: "Express%" } }) {
              edges {
                  node {
                      Id
                      Name {
                          value
                      }
                      ChildAccounts {
                          edges {
                            node {
                              Name {
                                value
                              }
                            }
                          }
                      }
                  }
              }
          }
      }
  }
}
```

To query object metadata that includes child relationships, pass in the [`childRelationships` field](/docs/platform/graphql/guide/query-objectinfo.md).

You can also traverse the parent-to-child relationship using a reference to a different object. In this case, the CreatedBy field references the Name field on the User object.

```sfdocs-code {"lang":"graphql", "title": "", "src": "" }
query  {
  uiapi {
    query {
      Account {
        edges {
          node {
            Name { value }
            Notes {
              edges {
                node {
                  CreatedBy {
                    Name {
                      value
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
```

The previous query is similar to this SOQL statement.

```sfdocs-code {"lang":"sql", "title": "SOQL statement showing parent-to-child relationship with a reference field", "src": "" }
SELECT Name,
  (
    SELECT CreatedBy.Name
    FROM Notes
  )
FROM Account
```

A query and its subqueries can include the `where` argument. The argument can filter on a supported object in the current scope via parent relationships.

For example, you can query accounts in a given industry and the last name of every contact whose created by first name is 'Marc'.

```sfdocs-code {"lang":"sql", "title": "Query accounts with where argument in query and subquery", "src": "" }
query  {
  uiapi {
    query {
      Account ( where: { Industry: { eq: "Technology"}} ) {
        edges {
          node {
            Name { value }
            Contacts (where: { CreatedBy: { FirstName: { eq : "Marc" } } } ) {
              edges {
                node {
                  LastName { value }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

The previous query is similar to this SOQL statement.

```sfdocs-code {"lang":"sql", "title": "SOQL statement showing WHERE clause in query and subquery", "src": "" }
SELECT Name,
  (
    SELECT LastName
    FROM Contacts
    WHERE CreatedBy.FirstName = 'Marc')
 FROM Account WHERE Industry = 'Technology'
```

## Lookup Relationships

With a [lookup relationship](https://help.salesforce.com/s/articleView?id=platform.overview_of_custom_object_relationships.htm), you can retrieve field information on a child or parent object. For example, you can query accounts and their associated contact names.

```sfdocs-code {"lang":"graphql", "title": "Query accounts with children contacts", "src": "" }
query AccountWithContacts {
  uiapi {
    query {
      Account(where: { Name: { like: "Edge%" } }) {
        edges {
          node {
            Name {
              value
            }
            Contacts {
              edges {
                node {
                  Name {
                    value
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
```

Accounts without child contact records are also returned. The previous query returns this example response.

```sfdocs-code {"lang":"json", "title": "Response for query on accounts with children contact records", "src": "" }
{
  "data": {
    "uiapi": {
      "query": {
        "Account": {
          "edges": [
            {
              "node": {
                "Name": {
                  "value": "Edge Engineering"
                },
                "Contacts": {
                  "edges": []
                }
              }
            },
            {
              "node": {
                "Name": {
                  "value": "Edge Communications",
                  "displayValue": null
                },
                "Contacts": {
                  "edges": [
                    {
                      "node": {
                        "Name": {
                          "value": "Sean Forbes"
                        }
                      }
                    },
                    {
                      "node": {
                        "Name": {
                          "value": "Rose Gonzalez"
                        }
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  },
  "errors": []
}
```

You can also use the [`orderBy` clause](/docs/platform/graphql/guide/order.md) to order your child records in ascending or descending order.

```sfdocs-code {"lang":"graphql", "title": "Query an account with children contacts and order them", "src": "" }
query AccountsWithContactsOrderBy {
  uiapi {
    query {
      Account (where: {Name: { eq: "Alpha Dynamics"}}) {
        edges {
          node {
            Name {
              value
            }
            Contacts(orderBy: {
              Name: {order: ASC }
            }) {
              edges {
                node {
                  Name {
                    value
                  }
                }
              }
            }
          }
          cursor
        }
      }
    }
  }
}
```

The child records are paginated and include cursor and pagination information. By default, the first 10 children records are returned. To specify the number of children records to return, use the `first` argument on the child object.

```sfdocs-code {"lang":"json", "title": "Response for query on children records in ascending order with pagination information", "src": "" }
{
  "data": {
    "uiapi": {
      "query": {
        "Account": {
          "edges": [
            {
              "node": {
                "Name": {
                  "value": "Alpha Dynamics"
                },
                "Contacts": {
                  "edges": [
                    {
                      "node": {
                        "Name": {
                          "value": "Amy Taylor"
                        }
                      }
                    },
                    {
                      "node": {
                        "Name": {
                          "value": "Caroline Kingsley"
                        }
                      }
                    },
                    {
                      "node": {
                        "Name": {
                          "value": "Jennifer Wu"
                        }
                      }
                    }
                  ]
                }
              },
              "cursor": "djE6MA=="
            }
          ]
        }
      }
    }
  },
  "errors": []
}
```

## Lookup Relationships with null Values

With an `orderBy` argument, records are returned even if the reference field has a null value.

For example, any case records with a null AccountId field are also returned.

```sfdocs-code {"lang":"graphql", "title": "Query cases and order by the parent account name", "src": "" }
query  {
  uiapi {
    query {
      Case (orderBy: { Account: { Name: { order: ASC } } } ) {
        edges {
          node {
            Id
            CaseNumber { value }
            AccountId { value }
            Account {
              Name { value }
            }
          }
        }
      }
    }
  }
}

```

The previous query is similar to this SOQL statement.

```sfdocs-code {"lang":"sql", "title": "SOQL statement with ORDER BY clause and reference fields", "src": "" }
SELECT Id, CaseNumber, Account.Id, Account.Name
FROM Case
ORDER BY Account.Name
```

With a `where` argument that uses the `or` boolean operator, records are returned even if the reference field value in a record is null.

For example, if a contact with a specific LastName has a null AccountId field and another contact with a different last name and specific parent account name, the query returns both contacts.

```sfdocs-code {"lang":"graphql", "title": "Query contacts that matches a condition with the or operator", "src": "" }
query  {
  uiapi {
    query {
      Contact (where: {
        or: [
            { LastName: { eq: "Young" } },
            { Account : { Name: { eq: "Edge Communications" } } }
          ]
  }) {
        edges {
          node {
            Id
            FirstName { value }
          }
        }
      }
    }
  }
}

```

The previous query is similar to this SOQL statement.

```sfdocs-code {"lang":"sql", "title": "", "src": "" }
SELECT Id, FirstName
FROM Contact
WHERE LastName = 'Young' or Account.Name = 'Edge Communications'
```

With a `where` argument that checks for a value in a parent field, the record is returned even if the parent value is null.

```sfdocs-code {"lang":"graphql", "title": "Query cases whose contact last name is null", "src": "" }
query  {
  uiapi {
    query {
      Case (where: { Contact: { LastName: { eq: null } } } ) {
        edges {
          node {
            Id
            Description { value }
            Priority { value }
          }
        }
      }
    }
  }
}
```

The previous query is similar to this SOQL statement.

```sfdocs-code {"lang":"sql", "title": "SOQL statement with a lookup value of null", "src": "" }
SELECT Id, Description, Priority
FROM Case
WHERE Contact.LastName = null
```

## See Also

[*SOQL and SOSL Reference*: Understanding Relationship Fields and Polymorphic Fields](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_and_polymorph_keys.htm)

[Paginate Results](/docs/platform/graphql/guide/paginate.md)
