Home > Back-end >  SQL - Find all that don't have specific column value
SQL - Find all that don't have specific column value

Time:09-16

I have a table like the following, where the primary key is the Name & Age.

Name Age Spirit Animal
Bob 18 Wolf
Bob 49 Cat
Bob 49 Dog
Jim 11 Cat
Jim 22 Cat

I want to find all of the names that don't have a spirit animal of Wolf. In this example, Jim should be returned but, not Bob. How would I go about doing this ?

CodePudding user response:

You can use aggregation:

select name
from t
group by name
having sum(case when spirit_animal = 'Wolf' then 1 else 0 end) = 0;
  • Related