使用しているデータベースサーバーについて言及していないため、以下のクエリは、以下を含むほぼすべてのRDBMSで機能します。MySQL
SELECT r.rating,
r.product,
l.places,
l.address,
l.telephone,
l.lng,
l.lat
FROM locations AS l
INNER JOIN ratings AS r
ON l.places = r.places
(
SELECT a.places, b.product, MAX(b.rating) max_rating
FROM locations a
INNER JOIN ratings b
ON a.places = b.places
GROUP BY a.places, b.product
) c ON l.places = c.places AND
r.product = c.product AND
r.rating = c.max_Rating
ORDER BY r.rating desc
または、RDBMSがウィンドウ関数をサポートしている場合、
SELECT rating, product, places,
address, telephone, lng, lat
FROM
(
SELECT r.rating, r.product, l.places,
l.address, l.telephone, l.lng, l.lat
ROW_NUMBER() OVER (PARTITION BY r.product, l.places
ORDER BY r.rating DESC) rn
FROM locations AS l
INNER JOIN ratings AS r
ON l.places = r.places
) x
WHERE x.rn = 1
ORDER BY rating desc