0

I am still new to SQL and was wondering what would be the best option to get distinct category names from two different columns from the same table.

Example:

Table Name: Fruits

ID          CAT1           CAT2
1           APPLE          PEACH
2           PEACH          GRAPE
3           APPLE          GRAPE
4           ORANGE         APPLE
5           PEACH          PEAR

Desired Output

Distinct CAT
APPLE
PEACH
GRAPE
ORANGE
PEAR

I know that I would want to do a join where I name each table a letter like fruits a and fruits b so I match it via the ID but I cannot figure how to display it in one column only the distinct CAT from both columns.

2 Answers 2

3

You could query the distinct values of both columns separately and UNION (e.g. MySQL documentation) the results:

(SELECT DISTINCT CAT1 FROM Fruits)
UNION
(SELECT DISTINCT CAT2 FROM Fruits)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I was way over thinking this
1

If you would have played with it little you would have get this already.

Select distinct cat from ( (Select cat1 as cat from fruits) union all (Select cat2 as cat from fruits)) q

1 Comment

Yes I was only thinking of using join I completely forgot the purpose of Union which was why my queries were not working

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.