Fix sql queries