Include child records in queries
Fetch child records alongside a parent in a single HOQS query, optionally filtered, sorted, and nested.
Prerequisites
Section titled “Prerequisites”- A relationship exists whose inverse (
toInverseKey) names the children you want to fetch. See Relationships and Lookups. - You can already run a basic query — see Query and filter records.
1. Add a bare INCLUDE
Section titled “1. Add a bare INCLUDE”The relationship name comes from the lookup’s toInverseKey — for example, if Contact has a lookup to Account with toInverseKey: "contacts", you can include contacts when querying accounts:
SELECT name, industry FROM core.AccountINCLUDE contactsLIMIT 10Each Account in the result has a contacts array with the matching child records.
2. Filter, select fields, sort, and limit within the INCLUDE
Section titled “2. Filter, select fields, sort, and limit within the INCLUDE”SELECT name FROM core.AccountINCLUDE contacts (firstName, email) WHERE status = 'active' ORDER BY lastName ASC LIMIT 5LIMIT 103. Nest INCLUDEs for multiple levels
Section titled “3. Nest INCLUDEs for multiple levels”SELECT name FROM core.AccountINCLUDE contacts INCLUDE opportunities WHERE stage = 'Closed Won' LIMIT 10LIMIT 10This returns Accounts with their Contacts, and each Contact carries its own Opportunities. Nesting depth is limited to 3 levels.
Verification
Section titled “Verification”Check that each parent record in data carries the included relationship as a nested array, and that any WHERE/ORDER BY/LIMIT you added inside the INCLUDE clause is reflected in that array (not the top-level result set).
See also
Section titled “See also”- Querying Data with HOQS – why INCLUDE issues a query per parent row instead of a SQL JOIN, and why nesting is capped at 3 levels
- Query and filter records
- HOQS Query Language Reference – full INCLUDE grammar and limits