1

I have one table - Users - and I have another table - Colors. Every user can create more records in Colors and each of these are "owned" by the user in question (this means that Colors holds a field named "Userid", which is the same as the field named "Id" in the Users table).

Example: User Per has created these records in Colors: Red Blue Black

User Phil has created the record in Colors. Blue

Now I want to select the users that have created BOTH Blue and Red. Can I do this in one select statement?

Thanks!

3 Answers 3

2

try (example, modify according to your table structure!):

SELECT X.UserID FROM 
(SELECT UserID, COUNT(DISTINCT COLOR) C FROM COLORS WHERE COLOR IN ( 'Red', 'Blue' ) GROUP BY UserID) X 
WHERE X.C = 2
Sign up to request clarification or add additional context in comments.

Comments

1

Sure you can :)

Hint: it'll probably involve a "join".

Q: What have you tried so far?

Q: Is this a homework assignment? We can help you find the answer - but we can't just GIVE you the answer...

1 Comment

So you admit this is not an answer? Why don't you post it as a comment then?
0

Yes you can do this in one select statement. Perhaps offer the tables stucture and your attempt if you cannot get this to work.

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.