reverting subquery to inner join