bennes
bennes

Reputation: 91

multiple values must have the same value within one table SQL

I have one table, in which there can be one or many values to a first column.

If there are multiple same values within first column, I would need to find those, which has at least 2 different values in another column.

Example table CARS:

CAR        COLOR
BMW        black
Honda      brown
BMW        red
Mercedes   yellow
Honda      brown
Honda      brown

So, the result of the query would be:

enter image description here

Because there are more honda`s but all of them have the same value of second attribute, I am not interested of those. I need to find only those, which has the same attribute 1 with different attribute 2

Hopefully it`s explained well, if not I can provide more info :)

Thanks

Upvotes: 0

Views: 1107

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269543

You can use exists:

select t.*
from t
where exists (select 1 from t t2 where t2.car = t.car and t2.color <> t2.color);

Upvotes: 1

Related Questions