Some small fixes in the SQL queries