In SQL, how do I check what are the data in a categorical attribute?

Viewed 37

For example, in a table, I have an attribute call "color", how to check what unique colors are included in the "color" attribute?

Name Color
Alice red
Ben blue

Looking for a result that looks like: red, blue, green, black

Thanks in advance!

2 Answers

Use DISTINCT to return only different values:

SELECT DISTINCT Color FROM table_name;

Either you select the unique values of the column

select distinct color from yourtable

Or you group by the column

select color, count(*) as total
from yourtable
group by color
order by count(*) desc
Related