altered sql for ordering