complex query implemented