Having Clause
- Last Updated: July 30, 2025
- 1 minute read
- DataDirect Connectors
- ODBC
- MongoDB 8.0
- Documentation
Purpose
Specifies conditions for groups of rows (for example, display only the departments that have salaries totaling more than $200,000). This clause is valid only if you have already defined a Group By clause.
Syntax
HAVING expr1
rel_operator
expr2
where:
- expr1 | expr2
- can be column names, constant values, or expressions. These expressions do not have to match a column expression in the Select clause. See "SQL Expressions" for details regarding SQL expressions.
- rel_operator
- is the relational operator that links the two expressions.
Example
The following example returns only the departments that have salaries totaling more than $200,000:
SELECT dept_id, sum(salary) FROM emp GROUP BY
dept_id HAVING sum(salary) > 200000
Notes
The driver uses the MongoDB aggregation framework whenever possible. In some instances, MongoDB may aggregate data in unexpected ways. For example, if you use the HAVING clause to return zip codes and the database contains the zip code 15237 as both a string and an integer, then two rows will be returned for 15237 (one for the string representation and another for the integer representation).