reduce sql inquery