select *
from `categories`
where exists (
select * from `products` where `categories`.`id` = `products`.`category_id`
);
or
select *
from categories
left join (select category_id, count(id) as product_count from products group by category_id) as temp
on category_id = categories.id
where product_count > 0;