Fix query without order by handling