May 2019
Intermediate to advanced
484 pages
11h 23m
English
HAVING further filters the result based on values in the results, rather than the actual data. A HAVING clause only applies to columns that are included in the GROUP BY clause or in an aggregate function. Building on the same example used in the WHERE, ORDER BY, and GROUP BY sections, here we want to additionally know which locations carry an inventory of over 100 items per product. For that, after the GROUP BY clause, the query has a HAVING clause over the aggregate function, where its result is greater than 100:
SELECT P.Name AS ProductName, SUM([PI].Quantity) AS Total_Quantity, L.Name AS LocationNameFROM Production.Product AS PINNER JOIN Production.ProductInventory AS [PI] ON P.ProductID = [PI].ProductIDINNER JOIN Production.Location ...
Read now
Unlock full access