How can I find the values in an SQL column that are repeated exactly N times? For example, let's say I have a column that has the values fanta, sprite, sprite, sprite, cola, cola, cola, cabbages, cabbages, cabbages, cabbages
, and I want to find the values which is repeated exactly 3 times, which should return sprite
and cola
, but not fanta
or cabbages
, because they are not repeated exactly 3 times.
I tried to adapt the SQL from Find most frequent value in SQL column:
SELECT value1, COUNT(value1) AS value1_count
FROM table1
GROUP BY value1
WHERE value1_count=3
But that gives me near "WHERE": syntax error:
, so obviously I'm not doing it right.
Edit: Finding duplicate values in a SQL table was suggested as exact duplicate, but it is about finding all repeated values, regardless of how many times they are repeated, though I admit the answers there make it obvious how to reach a solution in my case, so I don't know.