The HAVING Clause
As we saw, the aggregate functions will solve many problems for us. Eventually, however, we'll want to filter the aggregate function value of a query result set. We have learned how to build SQL queries that calculate aggregate function values. If we want to filter the query results on those aggregate function values, the WHERE
clause won't work. For example, we might want to see the query results of a SQL MAX
function that land below a specific value. We might need the query results of a SQL AVG
function that match a specific value. The HAVING
clause will help. Starting with this query, suppose we want only those rows with MINIMUM NET RETAIL PRICE
values greater than 5.00
in the result set:
USE packt_online_shop; SELECT PC.ProductCategoryID, PC.ProductCategoryName, AVG(P.UnitKGWeight) AS 'AVERAGE PRODUCT KG WEIGHT', MIN(P.NetRetailPrice) AS 'MINIMUM NET RETAIL PRICE' FROM ProductCategories PC INNER...