Question: I have associative table movies and their categories.

```
movie_id catagory_id
   1         1
   1         2
   1         3
   2         1
   2         2
   2         4
   3         1
   3         5
   3         6

```

How can i get movies that have category 1 and 2?

Result:

```
movie_id
   1
   2

```

Answer: 
Aggregate all the categories of each movie in an array and then filter those that contain all the target categories in `having` clause. The `contains` array operator is handy for the job.

```sql
 select movie_id from t
 group by movie_id
 having array_agg(catagory_id) @> array[1,2];

```
You should try this.

```
select movie_id 
from movies m 
join categories c on c.movie_id = m.movie_id and c.category_id in (1,2)

```