refactor query from insert