Azianese
Azianese

Reputation: 624

Select rows where, for the same value in column A, column B has a certain value in all rows

I need to find rows where, for the same value in A, column B has a certain value in all rows.

For example, given table:

Column A Column B
mom success
mom success
dad success
dad failure
kid failure
kid failure

Let's say I want all rows where column B is all 'failure' for the same value in column A. In this case, I would want:

Column A Column B
kid failure
kid failure

Upvotes: 1

Views: 498

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271003

You can use not exists:

select t.*
from t
where not exists (select 1
                  from t t2
                  where t2.a = t.a and t2.b <> 'failure'
                 );

Upvotes: 1

Related Questions