chris chris - 6 months ago 9
SQL Question

sql join rows in two tables

I'm not good at sql and wonder if this can be done:
I have two tables: table_a and table_b
Both tables have a TEXT type column named category.

Example:
table_a

|-id-|-category-|
| 1 | fruits |
| 2 | meats |
| 3 | fruits |
| 4 | sweets |
| 5 | meats |


table_b

|-id-|-category-|
| 1 | veggies |
| 2 | meats |
| 3 | veggies |
| 4 | veggies |
| 5 | meats |


What I need is to select all distinct categories from both tables in alphabetic order.

The result should be:
fruits
meats
sweets
veggies

Thank you

Answer

You should use UNION and an ORDER BY clause :

SELECT DISTINCT category 
FROM Table_A
UNION 
SELECT DISTINCT category
FROM Table_B
ORDER BY category