WITH rates as
(SELECT rate, type, route_id, weight, volume
FROM test_rates rs
WHERE rs.dt_del IS NULL
AND (rs.volume >= 300 OR rs.weight >= 50000))
SELECT rate, type, route_id, weight, volume
FROM rates
where rate = (select max(rate) from rates)
select views, image_url, (select avg(votes.value))
from images
inner join votes
on votes.image_id = images.id
group by views, image_url
order by views asc