Skip to content
Advertisement

How do I check if a column have more than two items and if it have than do the avg() on it in SQL?

I have 4 tables

The user and item from buys and rates are foreign keys. I can`t figure out how to put a condition to take out the items which have only one rating and do the average only on the ones who have 2 or more ratings.

This is the question in the exercises (For each item that has at least two ratings, find the average rating for the item.)

This is my code.

Advertisement

Answer

You can check if the item is presented in the table more than once with HAVING condition:

Take a look here: https://www.postgresqltutorial.com/postgresql-having/

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement