---
schema_version: "1"
id: 0ab211d1fab9c0f53bab187f5a914f898a332253cdf581cb3775a1ad6f5cfaea
title: HAVING
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/salesforce-soql-sosl/guides/soql-sosl/sforce-api-calls-soql-select-having.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-soql-sosl/guide/sforce-api-calls-soql-select-having.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 548d0b73e78245c2d35a8fc79a1c471881fdf4e5c771208a177f980406d6c799
products:
  - SOQL and SOSL Reference
  - Platform Developer Content
---

# HAVING

`HAVING` is an optional clause that can be used in a SOQL query to filter results that aggregate functions return.

You can use a `HAVING` clause with a `GROUP BY` clause to filter the results returned by [aggregate functions](https://developer.salesforce.com/docs/platform/salesforce-soql-sosl/guide/sforce-api-calls-soql-select-agg-functions.md), such as `SUM()`. The `HAVING` clause is similar to a `WHERE` clause. The difference is that you can include aggregate functions in a `HAVING` clause, but not in a `WHERE` clause.

Similar to a WHERE clause, the HAVING clause supports all the comparison operators, such as =, in conditional expressions, which can contain multiple conditions using the logical AND, OR, and NOT operators.

```soql
[HAVING havingConditionExpression]
```

havingConditionExpression specifies one or more conditional expressions using aggregate functions to filter the query results.

For example, you can use a `GROUP BY` clause to determine how many leads are associated with each `LeadSource` value with the following query:

<!-- otherprops: set_clipboard -->

```soql
SELECT LeadSource, COUNT(Name)
FROM Lead
GROUP BY LeadSource
```

However, if you are only interested in `LeadSource` values that have generated more than 100 leads, you can filter the results by using a `HAVING` clause. For example:

<!-- otherprops: set_clipboard -->

```soql
SELECT LeadSource, COUNT(Name)
FROM Lead
GROUP BY LeadSource
HAVING COUNT(Name) > 100
```

This example query returns accounts with duplicate names.

<!-- otherprops: set_clipboard -->

```soql
SELECT Name, Count(Id)
FROM Account
GROUP BY Name
HAVING Count(Id) > 1
```

This example query is invalid as `City` is not included in the `GROUP BY` clause:

```soql
SELECT LeadSource, COUNT(Name)
FROM Lead
GROUP BY LeadSource
HAVING COUNT(Name) > 100 and City LIKE 'San%'
```

**note:** A HAVING clause can't contain any semi-join or anti-join subqueries.
