MySQL MariaDB

How to Use the MySQL HAVING Clause Where the Sum Is Greater Than the Threshold

When working with MySQL database, you will often have instances where you want to filter the values. Although you can use the WHERE clause, it doesn’t always work for all instances. The HAVING clause is used when you want to execute a filter query using an aggregate function and apply the groupings.

The HAVING clause relies on the GROUP BY clause in its filter and executes the query based on the provided condition. It restricts the filter based on the condition and only returns the selected groups if they meet the condition. We have examples of using the MySQL HAVING clause where the sum exceeds the threshold. That way, you will master how to use it at the end of today’s post.

How to Use the MySQL HAVING Clause Where the Sum Is Greater Than the Threshold

You apply the MySQL HAVING clause with the GROUP BY clause. Its syntax is as follows:

SELECT expression1, .. expression_n, aggregate_func (expression) FROM table_name GROUP BY expression HAVING <condition>;

You can use the different aggregate functions including SUM(), COUNT(), MIN(), MAX(), and AVG(). Note that any expression that is not used with the aggregate function must be mentioned with the GROUP BY clause.

As for the condition, it is applied to the aggregated results where you specify the threshold that you want to check with the condition. For instance, you can apply the SUM() function and verify if the expression meets a threshold of 10. We will understand more about it in the provided examples.

To understand how the HAVING clause works, let’s create a sample database that we will work with. We name our database as “register.”

Let’s also create a table named “workers” to contain our different columns and data types. Here, we work with the workers’ data such as the name, hours, workdate, etc.

By checking the description of our table, we can confirm that all columns are created successfully.

We insert the values into our table. We have different workers who are working in different hours and days. Using this data, we can apply the HAVING clause where the sum exceeds the threshold.

For our first example, let’s consider a case where we want to find the workers who worked the most hours. For the HAVING clause, include the sum aggregate function for hours. In the GROUP BY clause, we group the rows using the name row to help classify the workers with total hours that are greater than the threshold.

If we have our threshold for the total hours as 7, we execute our command as follows:

SELECT name, SUM(hours) AS total_hours_per_person FROM workers GROUP BY name HAVING sum(hours) > 7;

By executing the command, we get an output that contains two rows since only two workers exceeded the threshold that is specified in the HAVING clause.

Suppose we want to classify the departments to see those with workers who work in hours greater than a threshold of 7 hours. We select the department, then use the SUM aggregate function with the hours and group the rows using the department expression.

Our command is as follows:

SELECT department, SUM(hours) AS total_hours_per_department FROM workers GROUP BY department HAVING sum(hours) > 7;

From the results, we can verify that we managed to filter the rows to only leave those with a sum of their hours greater than our threshold.

Similarly, if we want to filter the workdate with the highest number of working workers to exceed a threshold of 10 hours, we select the workdate expression. Then, we use the SUM function with the hours and group the rows using the workdate.

The command is as follows:

SELECT workdate, SUM(hours) AS highest_hours_worked FROM workers GROUP BY workdate HAVING sum(hours) > 10;

After executing the command, the results show that only one workdate has its sum of total hours that exceed the threshold:

Conclusion

The MySQL HAVING clause is used when you want to execute a filter query using an aggregate function. It is combined with the GROUP BY clause to execute a more specific query. This post detailed everything about the MySQL HAVING clause where the sum is greater than the threshold. With the provided examples, you now understand how to work with the MySQL HAVING clause.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.