Home >Database >Mysql Tutorial >How to Filter Groups Based on Aggregate Function Counts in SQL?
Grouping and Filtering by Aggregate Function Value
You may encounter a scenario where you need to group results and then filter by the number of rows in each group. While the suggested query SELECT * FROM mytable WHERE COUNT(*) > 1 GROUP BY name is syntactically incorrect, it highlights the desired functionality.
To achieve this, you can employ the HAVING clause, which allows you to apply conditions on aggregate functions. The following modified query will accomplish your goal:
SELECT name, COUNT(*) FROM mytable GROUP BY name HAVING COUNT(*) > 1
In this query:
This query will retrieve the name and count of all groups where the count exceeds 1, allowing you to identify and filter based on the occurrence of values.
The above is the detailed content of How to Filter Groups Based on Aggregate Function Counts in SQL?. For more information, please follow other related articles on the PHP Chinese website!