added subquery logic