Fixed an bug on the SQL query