How to check if a subquery has exactly one distinct result and a specified value concisely?

dba.stackexchange https://dba.stackexchange.com/questions/23637

  •  30-10-2019
  •  | 
  •  

Question

I found myself writing the following:

select 'yes' 
where exists(select * from foo where val=1)
and not exists(select * from foo where val<>1);

and wondering if there is a more concise way without sacrificing too much readability.

I found one way which I am posting as an answer but I'm not entirely happy with it and would be very interested in alternatives

In this case val is unique within foo - there are no duplicates

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top