PostgreSQL HAVING
Summary: in this tutorial, you will learn how to use the PostgreSQL HAVING clause to specify a search condition for a group or an aggregate.
Introduction to PostgreSQL HAVING clause
The HAVING
clause specifies a search condition for a group. The HAVING
clause is often used with the GROUP BY
clause to filter groups based on a specified condition.
The following statement illustrates the basic syntax of the HAVING
clause:
In this syntax:
- First, the
GROUP BY
clause groups rows into groups by the values in thecolumn1
. - Then, the
HAVING
clause filters the groups based on thecondition
.
If a group satisfies the specified condition, the HAVING
clause will include it in the result set.
Besides the GROUP BY
clause, you can also include other clauses such as JOIN
and LIMIT
in the statement that uses the HAVING
clause.
PostgreSQL evaluates the HAVING
clause after the FROM
, WHERE
, GROUP BY
, and before the DISTINCT
, SELECT
, ORDER BY
and LIMIT
clauses:
Because PostgreSQL evaluates the HAVING
clause before the SELECT
clause, you cannot use the column aliases in the HAVING
clause.
This restriction arises from the fact that, at the point of HAVING
clause evaluation, the column aliases specified in the SELECT
clause are not yet available.
HAVING vs. WHERE
The WHERE
clause filters the rows based on a specified condition whereas the HAVING
clause filter groups of rows according to a specified condition.
In other words, you apply the condition in the WHERE
clause to the rows while you apply the condition in the HAVING
clause to the groups of rows.
PostgreSQL HAVING clause examples
Let’s take a look at the payment
table in the sample database:
1) Using PostgreSQL HAVING clause with SUM function example
The following query uses the GROUP BY
clause with the SUM()
function to find the total payment of each customer:
Output:
The following statement adds the HAVING
clause to select the only customers who have been spending more than 200
:
Output:
2) PostgreSQL HAVING clause with COUNT example
See the following customer
table from the sample database:
The following query uses the GROUP BY
clause to find the number of customers per store:
Output:
The following statement adds the HAVING
clause to select a store that has more than 300 customers:
Output:
Summary
- Use the
HAVING
clause to specify the filter condition for groups returned by theGROUP BY
clause.