Marry Jane
Marry Jane

Reputation: 354

How to filter unique columns in a table in SQL?

I have a table which looks like this;

col1 - col2

data1 - ip1
data1 - ip2
data2 - ip2

So this ip2 belongs to data1 and data2 but ip1 only belongs to data1 so I want to filter ip1 only. In other words, I want to list every item inside col2 column which only has unique content which is belong to data1. If the same ip* belongs to more than one data* then don't filter that.

Example in SQL;

SELECT col2 FROM table1 WHERE col1 = 'data1' AND (if col1 is unique and doesn't belong to any other data.nth)

So the part in parentheses is what I can't figure out how to do. How can I do that?

Upvotes: 0

Views: 42

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269773

You can use aggregation:

select col2
from table1
group by col2
having min(col1) = 'data1' and min(col1) = max(col1);

Upvotes: 2

Related Questions