fixes #5790; fixes sql query