achiash
achiash

Reputation: 1682

How to count rows that have the same values in two columns and return only rows where count<N(SQL)?

regarding that question: How to count rows that have the same values in two columns (SQL)?

is there a way to return only the rows where the count is < 3?

+-----+-----+-----+
|  A  |  B  |count|
+=====+=====+=====+
|  1  |  3  |  2  |
+-----+-----+-----+
|  4  |  2  |  1  |
+-----+-----+-----+

Upvotes: 0

Views: 229

Answers (2)

John Woo
John Woo

Reputation: 263803

Just add the condition in the HAVING clause

SELECT colName, COUNT(*)
FROM tableName
GROUP BY colName
HAVING COUNT(*) < 3

Upvotes: 1

podiluska
podiluska

Reputation: 51504

The HAVING clause

 SELECT ...
 FROM ...
 WHERE ...
 GROUP BY whatever
 HAVING count(*) <3

Upvotes: 1

Related Questions