Errors fixup in sql queries