Fix SQL queries