Fix sql query errors