I want to pull a list of units that have an overall rating of less than 4. The first query returns 1760 rows with data that seems to check out when cross referenced. The second query returns only 434 rows. Why would this be? I don't fully understand the "Group By" and "Having" clauses, so I'm guessing it has something to do with those.
'''
SELECT u.unitid
,AVG(r.overall) AS AverageRating
, u.ratingcount
FROM reviews r
LEFT JOIN units u
ON u.unitid = r.unitid
-- only retrieve active, non-terminated units set to display
WHERE u.active = 1
AND u.display = 1
AND u.terminated = 0
GROUP BY u.unitid
HAVING AverageRating < 4
;
```
```
SELECT u.UnitID
, u.rating
, u.ratingcount
from units u
WHERE u.rating < 4
-- only retrieve active, non-terminated units set to display
and u.active = 1
and u.display = 1
and u.terminated = 0
;
```