Very good. Look at another example of a CTE with an
WITH AvgPrices AS (
AVG(Price) AS AvgPrice
GROUP BY GroceryStoreId
INSERT INTO GroceryAvgPrices
WHERE GroceryStoreId != 2;
In this case, we created the CTE to retrieve the average price of products for each grocery store. We used the
AVG aggregate function to compute the average price of all products in the grocery store. In this query, records are grouped according to
GroceryStoreId, using the
GROUP BY clause.
In the outer query, we inserted rows into the
GroceryAvgPrices table, but only for grocery stores with IDs different than 2.