implement query handling with join/subquery