1

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?

2
  • 1
    Don't edit your question if your question is solved. If you want to mark the question as "Solved", accept the answer that solved it. Commented Oct 13, 2014 at 15:06
  • So sorry. Thank for the advice :) Commented Oct 14, 2014 at 2:18

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.