If we want to answer the question
“Which stations were the starting point for 10,000 or more rides?”, it would seem like we want to write something like this:
Unfortunately, you can never include an aggregating function or result within the
WHERE
clause. Trying to do so will give an error. However, there is a
HAVING
clause that can be used just like a
WHERE
to filter data, only it works with aggregations. To ask about the stations with over 10000 trips we would write:
As with the
ORDER BY
clause,
HAVING
is an operation on the results. While
WHERE
filters the data being queried,
HAVING
filters the results based on the value of an aggregating function. The
HAVING
clause can only be used immediately following the
GROUP BY
clause.