Level 73
SELECT p.name, (SELECT avg(r.rating) FROM ratings r WHERE r.place_id = p.id) rating FROM places p WHERE p.country_id = 99 AND p.is_active = 1
Should work unless I made a type-o.
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
Sorry I'm not excellent in SQL and I need to write a plain sql query to get me the top 5 rated places by calculating the average rating for all services.
My tables
PLACE Table
RATING
I tried the below but it throws an error:
$sql = "SELECT p.id as id, p.name as name, avg(r.rating) as rating FROM place p, rating r WHERE r.place_id = p.id AND p.country_id = 99 AND p.is_active = 1 GROUP BY r.place_id"; $results = DB::select($sql);
SELECT p.name, (SELECT avg(r.rating) FROM ratings r WHERE r.place_id = p.id) rating FROM places p WHERE p.country_id = 99 AND p.is_active = 1
Should work unless I made a type-o.
Please or to participate in this conversation.