Fixed inefficient query