These operators require the subquery to return a single value. If the subquery returns multiple values, an error will occur.
Operator | | Example use case |
= | Matches records where the field is equal to the subquery’s result. | Fetch Deals created on the same date as the latest deal. |
!= | Retrieves records where the field is not equal to the subquery’s result. | Fetch Deals not created on the same date as the latest deal. |
<, >, <=, >= | Compares a field with the single value returned by the subquery. | Fetch all Deals that closed before the latest high-value deal. |
between | Ensures a field's value falls within a range. | Fetch all Deals closed between two specific deal closure dates. |
not between | Excludes records within the range. | Fetch all Deals closed outside a specific date range. |
Example 1: Using the IN operator to fetch Contacts linked to high-value Deals
{ "select_query": "SELECT Full_Name, Email FROM Contacts WHERE Account_Name in (SELECT Account_Name FROM Deals WHERE Amount > 100000 AND Stage = 'Closed Won')" }
Example 2: Using the NOT IN operator to fetch Contacts from Accounts that have never closed a Deal
{ "select_query": "SELECT Full_Name, Email FROM Contacts WHERE Account_Name not in (SELECT Account_Name FROM Deals WHERE Stage = 'Closed Won')"}
Example 3: Using the BETWEEN operator to identify Deals closed during a specific Campaign (Q1 Marketing Blitz)
{ "select_query": "SELECT Deal_Name, Created_Time FROM Deals WHERE Closing_Date between (SELECT Start_Date FROM Campaigns WHERE Campaign_Name = 'Q1 Marketing Blitz') and (SELECT End_Date FROM Campaigns WHERE Campaign_Name = 'Q1 Marketing Blitz')"}
Combine JOINs and Subqueries for Smarter Filtering
In many business scenarios, it is not enough to simply retrieve related fields via JOINs—you also need to dynamically filter data based on conditions from another module. This is where subqueries can be incredibly useful in conjunction with JOINs.
Let’s say you're analyzing deal performance and want to pull a list of Deals that are:
- Associated with Active Campaigns
- Linked to Accounts in high-revenue Industries (Annual Revenue > 10,000,000)
- You need to fetch details from the related modules—like Account name, Campaign name, and Contact email—in the same query
Why this can't be solved easily: While COQL supports JOINs for pulling data from related modules and subqueries for dynamic filtering, you can’t do all of this in one approach alone.
- JOINs allow you to retrieve fields from related modules (like Contact_Name.Email )—but they do not let you dynamically filter using fields from those modules.
- Subqueries, on the other hand, allow you to filter based on fields from related modules (like filtering Deals based on the Annual Revenue of the associated Account)—but you can’t fetch fields from multiple related modules using just a subquery.
So, if you need to both filter based on fields in a related module (like Accounts.Annual_Revenue) and retrieve fields from related modules (like Contact_Name.Email, Campaign_Source.Campaign_Name), you need both tools.
Solution: Combine JOINs and Subqueries in the Same Query
{"select_query": "SELECT Deal_Name, Amount, Account_Name, Campaign_Source, Contact_Name.Email FROM Deals WHERE (Campaign_Source.Status = 'Active') AND Account_Name.Industry in (SELECT Industry FROM Accounts WHERE Annual_Revenue > 10000000 GROUP BY Industry)"}
This query:
- Uses JOINs to fetch:
- Contact_Name.Email – from Contacts
- Uses a subquery to apply dynamic filtering on Deals based on the Industry of Accounts with Annual Revenue > 10000000. COQL subqueries return a maximum of 100 records. If multiple accounts satisfying the criteria share the same industry, using GROUP BY Industry ensures only unique industry values are returned. This helps stay within the 100-record limit and avoids redundant filtering.
In the results, you get a deduplicated, filter-accurate list of Deals tied to Active Campaigns and high-value industries—with rich contextual fields in one API call.
You can even use JOINs inside subqueries. For instance, to fetch all deals from Optical Networking accounts managed by Jane (jane.doe@zohotest.com), and include key details about the deal, contact, account, and campaign:
{ "select_query": "SELECT Deal_Name, Amount, Account_Name, Contact_Name.Email, Campaign_Source FROM Deals WHERE Account_Name in (SELECT id FROM Accounts WHERE Industry = 'Optical Networking' AND Owner.email = 'jane.doe@zohotest.com')"}
By combining JOINs and subqueries, you can handle more complex, real-world business requirements in a single query. JOINs help you enrich your data with fields from related modules, while subqueries let you apply smart, condition-based filters across modules.
Advanced Filtering: Nested Subqueries
For more advanced use cases, you can nest subqueries within subqueries to create complex filtering conditions.
Let's say Imagine you need to identify all Contacts associated with Accounts that meet two specific criteria:
- The Account is in the "Retail" industry.
- The Account has at least one associated Deal that is in the "Closed Won" stage and has a value greater than $50,000.
This scenario is common in sales and marketing, where you want to target high-potential customers within a specific industry. Here's how you can achieve this using a nested subquery in COQL:
{
"select_query": "SELECT Full_Name, Email FROM Contacts WHERE Account_Name in (SELECT id FROM Accounts WHERE Industry = 'Communications' AND id in (SELECT Account_Name FROM Deals WHERE Stage = 'Closed Won' AND Amount > 50000))"
}
Innermost Subquery: (SELECT Account_Name FROM Deals WHERE Stage = 'Closed Won' AND Amount > 50000)
This subquery identifies all Account_Name values from the Deals module where the Stage is "Closed Won" and the Amount is greater than $50,000. Although the field name is Account_Name, it's a lookup to the Accounts module, so it returns the Account ID. This effectively isolates Accounts that have closed high-value deals.
Middle Subquery: (SELECT id FROM Accounts WHERE Industry = 'Retail' AND id IN (...))
This filters Accounts by the "Retail" industry and ensures that only those whose ID appears in the innermost subquery are selected.
Main Query: SELECT Full_Name, Email FROM Contacts WHERE Account_Name IN (...)
Finally, the main query retrieves the Full_Name and Email of all Contacts whose Account_Name matches the id values returned by the middle subquery.

Note: COQL supports a maximum subquery nesting depth of 5, and you can include up to 5 subqueries total within a single query. The nesting limit refers to subqueries embedded within the WHERE clause of another subquery.
Subqueries in COQL are a game-changer for developers looking to build efficient, context-aware queries across modules—allowing dynamic filtering that was previously only possible with multiple API calls. While JOINs help you fetch rich, multi-module data in a single response, subqueries let you filter records based on conditions from related modules. And when you combine the two, you unlock a powerful mechanism to tackle real-world CRM business cases with precision and clarity.
For advanced use cases, don’t hesitate to nest subqueries—COQL allows up to five levels deep—giving you the flexibility to build truly nuanced filters. Mastering the balance between JOINs and subqueries is key to writing smarter, cleaner, and more performant queries.
We hope you found this guide useful in understanding and implementing COQL subqueries. If you have any questions or require further clarification, please don't hesitate to leave a comment below or reach out to us directly at
support@zohocrm.com.
Keep experimenting, and as always, happy querying!