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: 
If you want have `catagory_id` 1 and 2 use this:

```sql
select movie_id from your_table
group by movie_id
having string_agg(catagory_id::text, ',' order by catagory_id) ~ '(1,)+(2)+'

```

If you want have `catagory_id` 1 or 2 use this:

```sql
select distinct(movie_id) from your_table
where catagory_id in (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)

```