Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rows with multiple corresponding values in the same table

Tags:

sql

db2

Suppose I have a table

Name          Wear
Martin        Hat
Martin        ?
Martin        Shirt
Alfred        Tee
Alfred        Jeans

And I only want names of people whose Wear value is fully given, that is without a NULL (?) value in there.

I thought about using a group by ... having Wear <> NULL but this isn't sufficient, as there can be more than one Wear value.

In this case, I would like to only return 'Alfred'.

like image 732
Blue_Elephant Avatar asked Jul 08 '26 13:07

Blue_Elephant


2 Answers

You can do it like

HAVING SUM(Wear IS NULL) = 0

or

HAVING COUNT(*) = COUNT(Wear)
like image 171
fancyPants Avatar answered Jul 11 '26 12:07

fancyPants


Use correlated subquery

select distinct name from tablename a where not exists 
    (select 1 from tablename b where a.name=b.name and wear is null)
like image 26
Fahmi Avatar answered Jul 11 '26 11:07

Fahmi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!