Fixed malformed SQL query issue