PostgreSQL Where count condition

SELECT a.license_id, a.limit_call , count(b.license_id) AS overall_count FROM “License” a LEFT JOIN “Log” b USING (license_id) WHERE a.license_id = 7 GROUP BY a.license_id — , a.limit_call — add in old versions HAVING a.limit_call > count(b.license_id) Since Postgres 9.1 the primary key covers all columns of a table in the GROUP BY clause. In older versions … Read more

SQL – HAVING vs. WHERE

WHERE clause introduces a condition on individual rows; HAVING clause introduces a condition on aggregations, i.e. results of selection where a single result, such as count, average, min, max, or sum, has been produced from multiple rows. Your query calls for a second kind of condition (i.e. a condition on an aggregation) hence HAVING works … Read more