supposing you have a many_to_many relation between User and Group I think something like that would do:
from u1 in User,
inner_join: g in assoc(u, :groups),
inner_join: u2 in assoc(g, :users),
where: u1.id == ^user1_id and u2.id == ^user2_id,
distinct: true,
select: g
You can make it directly from groupsusers table and match the foreign key for groups
The efficient way would be a intersect between two selects but I think Ecto doesn’t support that.