Fixed PgSQL and SQLite compatibility in a few queries