betemex
betemex

Reputation: 11

SQL query where count is >= 3

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

Answers (3)

geektampa
geektampa

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

Rahul Tripathi
Rahul Tripathi

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

T McKeown
T McKeown

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

Related Questions