iteration with custom sql