Fix SQL so that it works with PostgreSQL