query logic