Phito Phito - 28 days ago 13
SQL Question

group by having no null values in a column

If I execute the following query

select a, bar.c from foo
left join bar on foo.id = bar.foo


I get theses results

a c
----------
1 1
1 3
1 null

2 1
2 2

3 1
3 null
3 3
3 4


Now I would like to group theses values by
a
, but only when there is not a single null value for
b
, to only get a=2 as a result. How would I do that?

Answer Source

You can use having:

select a
from foo left join
     bar
     on foo.id = bar.foo
group by a
having count(*) = count(bar.c);