refactor basic SQL selects for filtering