Very good. Look at another example of the CTE with
WITH avg_prices AS (
avg(price) AS avg_price
GROUP BY grocery_store_id
INSERT INTO grocery_avg_prices
WHERE grocery_store_id != 2;
In this case we created the CTE to retrieve the average price of products for each grocery store. We use the
AVG aggregate function to compute average price of all products of the grocery store. In this query the records are grouped according to
grocery_store_id using the
GROUP BY clause.
In the outer query we insert rows into table
grocery_avg_prices, but only for grocery stores with id different than 2.