Concise, fast (especially with many rows), my favorite concerning readability and would work with dupes, too:
SELECT count(*) = 1 AND min(val) = 1 FROM foo;
FALSE .. or
NULL - only in the case of exactly one row with
val IS NULL, because
count() never returns
NULL or no row.
1 in the example just happens to be the same as the first, because of your example.
The query in the question fails with
NULL values. Consider the simple demo:
CREATE TABLE foo (id int, val int);
INSERT INTO foo VALUES (1, 1),(2, NULL);
WHERE EXISTS(SELECT * FROM foo WHERE val = 1)
AND NOT EXISTS(SELECT * FROM foo WHERE val <> 1);
IS DISTINCT FROM would fix this, but it could still fail with duplicates in
val - which you have ruled out for this case.
Your answer works fine.
'yes' / no row.
I would prefer this shorter form, though. Don't forget that PostgreSQL (unlike Oracle) has a proper
SELECT array_agg(val) = array FROM foo;