4 ms·
A much better resource if you are new and want to understand the difference between where and having is here near the bottom. https://www.postgresql.org/docs/1
by throwawayffffas 5y ago
A much better resource if you are new and want to understand the difference between where and having is here near the bottom.
https://www.postgresql.org/docs/14/tutorial-agg.html https://www.postgresql.org/docs/14/tutorial-agg.html
- RobertRoberts 5y agoRelevant snippet: > It is important to understand the interaction between aggregates and SQL's WHERE and HAVING clauses. The fundamental difference between WHERE and HAVING is this: WHERE selects input rows before groups and aggregates are computed (thus, it controls which rows go into the aggregate computation), whereas HAVING selects group rows after groups and aggregates are computed. Thus, the WHERE clause must not contain aggregate functions; it makes no sense to try to use an aggregate to determine which rows will be inputs to the aggregates. On the other hand, the HAVING clause always contains aggregate functions. (Strictly speaking, you are allowed to write a HAVING clause that doesn't use aggregates, but it's seldom useful. The same condition could be used more efficiently at the WHERE stage.) Thank you for linking this, it's very concise and actually helpful.