<para>
The <literal>LIKE</literal> operator does pattern matching and
is explained in <xref linkend="functions-matching"/>.
</para>
</callout>
</calloutlist>
</para>
<para>
It is important to understand the interaction between aggregates and
<acronym>SQL</acronym>'s <literal>WHERE</literal> and <literal>HAVING</literal> clauses.
The fundamental difference between <literal>WHERE</literal> and
<literal>HAVING</literal> is this: <literal>WHERE</literal> selects
input rows before groups and aggregates are computed (thus, it controls
which rows go into the aggregate computation), whereas
<literal>HAVING</literal> selects group rows after groups and
aggregates are computed. Thus, the
<literal>WHERE</literal> 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
<literal>HAVING</literal> clause always contains aggregate functions.
(Strictly speaking, you are allowed to write a <literal>HAVING</literal>
clause that doesn't use aggregates, but it's seldom useful. The same
condition could be used more efficiently at the <literal>WHERE</literal>
stage.)
</para>
<para>
In the previous example, we can apply the city name restriction in
<literal>WHERE</literal>, since it needs no aggregate. This is
more efficient than adding the restriction to <literal>HAVING</literal>,
because we avoid doing the grouping and aggregate calculations
for all rows that fail the <literal>WHERE</literal> check.
</para>
<para>
Another way to select the rows that go into an aggregate
computation is to use <literal>FILTER</literal>, which is a
per-aggregate option:
<programlisting>
SELECT city, count(*) FILTER (WHERE temp_lo < 45), max(temp_lo)
FROM weather
GROUP BY city;
</programlisting>
<screen>
city | count | max
---------------+-------+-----
Hayward | 1 | 37
San Francisco | 1 | 46
(2 rows)
</screen>
<literal>FILTER</literal> is much like <literal>WHERE</literal>,
except that it removes rows only from the input of the particular
aggregate function that it is attached to.
Here, the <literal>count</literal> aggregate counts only