Implement the query logic