I have two tables **Category** and **Device** with one-to-many relationship.

```
Category:
- id
- name

Device:
- id
- category_id
- brand

```

If I want to retrieve all categories with devices I can easily join these two tables. 

```
SELECT c.*, d.* FROM Category c INNER JOIN Device d ON d.category_id = c.id

```

The problem is that I need to filter by `brand` as well.

Suppose, that I have one category and 3 devices for this category. 
Now I want to get all categories with all devices where at least one out of many devices (per specific category) has a brand with value **foo**.

If at least one of devices matches this brand then I need to return all devices. If there's no match then I need to return empty result (no category and no devices).

I tried the following query, but seems like it's not working correctly.

```
SELECT c.*, d.*
FROM Category c
    INNER JOIN Device d ON d.category_id = c.id
WHERE EXISTS (SELECT * FROM Device d WHERE brand = 'foo')

```

The thing is that this `WHERE EXISTS` should be executed for each category.

Any help?