fix bug in sql query