Multiple aggregate functions in HAVING clause
Filter groups with multiple conditions on aggregate functions, combine HAVING
, AND
, and OR
. Example: groups where SUM(column2)
is over 100 and AVG(column3)
is under 20:
Here, we are using SUM(column2)
and AVG(column3)
as our aggregate functions in the HAVING
clause to filter results.
A deep dive into HAVING
The HAVING
clause, filters results after the GROUP BY
clause has done its job. The GROUP BY
sorts your data into neat, unique groups, but HAVING
allows you to go even deeper.
Count on COUNT
Filtering based on COUNT
is a common usage of HAVING
clause.
An exact number (say, 3) could be achieved with:
If you want a range, for instance counts between 3 and 6:
Sort your sums
Beyond counting, you could total a specific column with SUM(column) > amount
. For example,
Filters groups where the total exceeds a grand.
Similarly, to target groups with average values in a certain range, use BETWEEN
.
Advanced Aggregates
Sometimes, simple isn't enough. In such cases, advanced solutions involving HAVING
with complex aggregate functions come to rescue.
The Magic of COUNT(*)
Consider a case where you want to filter by the size of each group.
This will pinpoint groups with exactly five records.
SUM with Financial Transactions
Especially in financial tables, results boiled down to HAVING SUM() > value
could be crucial. Consider this:
You're now only showing accounts having transactions that total more than $10,000.
ORDER BY for Sort-based Elegance
Ranking and sorting is a breeze with ORDER BY
:
This would rank accounts from highest to lowest total transaction.
Building towards mastery
Null handling
Bear in mind null values when using COUNT(column)
. To include nulls, use COUNT(*)
or COALESCE
to assign default values to nulls.
Conditional aggregates for complex scenarios
In cases where you want aggregate functions to be applied conditionally --
Conditional aggregates prove to be a powerful tool for selectively summing values.
Grouping rules
Ensure to include all non-aggregate fields in your GROUP BY
clause. Neglecting can lead to undefined behavior and incorrect results.
Was this article helpful?