SQL LEFT JOIN, Average and WHERE

average, left-join, sql, sql-server-ce

Solution

Try `HAVING AVG(RATING.rating) > 3` after your `GROUP BY`.

See HAVING on MSDN for more details.

Problem

So I have been trying this for over a week and a bit, I put it to rest because I couldn't get it to work but this is what I am trying to do; Select a product from the database > Use LEFT OUTER JOIN to load its rating and make an average of it > Use INNER JOINs for different information (not really important, but I will say it anyway). This is a simplified (but accurate) replica of my query - the original is quite long and has different language words in it so it might get a bit confusing; ``` SELECT PRODUCT.name AS Name, PRODUCT.price AS Price, BRAND.name AS Brand, AVG(RATING.rating) AS Rating FROM PRODUCT LEFT OUTER JOIN RATING ON RATING.product_id = PRODUCT.product_id INNER JOIN BRAND ON BRAND.brand_id = PRODUCT.brand_id GROUP BY PRODUCT.name, PRODUCT.price, BRAND.name ``` The above works, but it misses the 'filtering' on rating. I wish adding `WHERE AVG(RATING.rating) > 3` would work, but sadly it doesn't. Is this in any way possible to do? Thanks.

Original source