Reputation: 11
I'm new with SQL, and am trying to execute a query that would only return each Category if each one appears 4 times or more. From googling, it seems like I should be using having count, but it doesn't seem return the categories.
Any ideas?
SELECT Category
FROM myTable
HAVING COUNT(*) >= 3";
Upvotes: 0
Views: 3486
Reputation: 110
This is the query that will "return each Category if each one appears 4 times or more."
SELECT Category
FROM mytable
GROUP BY Category
HAVING count(*) >= 4;
Upvotes: 0
Reputation: 172378
Just modify your query like this by adding the GROUP BY
clause:
SELECT Category
FROM myTable
group by category
HAVING COUNT(*) >= 4; -- Change this to 4 to return each Category if each one appears 4 times or more
Upvotes: 1
Reputation: 12847
The HAVING
clause only works in conjunction with GROUP BY
SELECT Category
FROM myTable
GROUP BY CATEGORY
HAVING COUNT(*) > 3;
Also changed your HAVING
based on your post of wanting to know 4 or more.
Upvotes: 0