Bee13
Bee13

Reputation: 13

Comparing Two Result in Postgres

I got a problem in comparing two set result in Postgres.

I have two table (consider both of them have primary key attribute), first is a CarWithOwner table that have two attribute CarName and Owner. The Second table is CarList with one attribute Name.

From those table I want to get the owner's name that has all the cars from the CarList table.

My first method is to grouping the person with the car then compare it with the carlist. Like this :

SELECT owner 
from CarWithOwner 
GROUP BY carname, owner 
HAVING carname = ALL(SELECT name from CarList);

But it doesn't give me the right result. Can you please give me a solution?

Upvotes: 1

Views: 128

Answers (1)

user330315
user330315

Reputation:

You need to compare the count of values, not the values itself:

select owner
from carwithowner
group by owner
having count(distinct carname) = (select count(*) from carlist)

Upvotes: 3

Related Questions