18.3. Filtering with Aggregated Results¶
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:
COUNT(*) AS n_trips
n_trips > 10000
Unfortunately, you can never include an aggregating function or result within the
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.
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.
Below is a query to find the total time each bike was ridden. Add a
clause to select only the bikes that were used for less than 1000 minutes.