fix broken sql query