Fixing SQL to be compatible with PostgreSQL