Fixing the query for postgres