Fixing Psql queries