Without getting too far into the differences of DISTINCT
vs. GROUP BY
, my preference in this case would be to use DISTINCT
, since you are not using aggregates and you are projecting a specific column in your SELECT
statement (See this post for further details: Is there any difference between GROUP BY and DISTINCT).
There are a couple of ways that you could approach this. Arguably using INNER JOIN
is the most readable version (below), however if your style is typically to run lots of subqueries then the subquery version (further below) might appeal to you better:
INNER JOIN method:
SELECT DISTINCT t1.category_name AS categories
FROM table_1 t1
INNER JOIN table_2 t2 ON t1.category_id = t2.category_id
ORDER BY 1
See SQL Fiddle for active example: http://sqlfiddle.com/#!9/51de19/5
Subquery Method:
SELECT t1.category_name AS categories
FROM table_1 t1
WHERE category_id IN(
SELECT DISTINCT category_id
FROM table_2 t2
)
ORDER BY 1
See SQL Fiddle for active example: http://sqlfiddle.com/#!9/51de19/6