Fix incorrect query construction