Fixed sql query errors