Fix sql query for #711