Filter by COUNT(*)?

MysqlSql

Mysql Problem Overview


Is it possible to group results and then filter by how many rows are in the group?

Something like this:

SELECT * FROM mytable WHERE COUNT(*) > 1 GROUP BY name

Mysql Solutions


Solution 1 - Mysql

You want to use HAVING to filter on the aggregate function.

SELECT name, COUNT(*)
    FROM mytable
    GROUP BY name
    HAVING COUNT(*) > 1

Solution 2 - Mysql

You need to use HAVING

SELECT * FROM mytable GROUP BY name HAVING COUNT(*) > 1

Although, SELECT * doesn't make much sense when you're grouping. I assume it's just for an example

Solution 3 - Mysql

Use having in your query:

SELECT * FROM mytable GROUP BY name having COUNT(*) > 1 

Solution 4 - Mysql

You want a HAVING clause.

SELECT *
FROM mytable
GROUP BY name
HAVING COUNT(*) > 1

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDonutReplyView Question on Stackoverflow
Solution 1 - MysqlJoe StefanelliView Answer on Stackoverflow
Solution 2 - MysqlJohnPView Answer on Stackoverflow
Solution 3 - Mysqluser319198View Answer on Stackoverflow
Solution 4 - MysqlR HillView Answer on Stackoverflow