# How to query for nodes belonging to intersection/union of two or more groups?

**URL:** <https://aiida.discourse.group/t/how-to-query-for-nodes-belonging-to-intersection-union-of-two-or-more-groups/190>\
**Category:** General Usage\
**Created:** [December 6, 2023, 8:08pm UTC](https://aiida.discourse.group/t/how-to-query-for-nodes-belonging-to-intersection-union-of-two-or-more-groups/190 "2023-12-06T20:08:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bastonero](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/bastonero/32/32_2.png) [@bastonero](https://aiida.discourse.group/u/bastonero)\
**Post date:** [December 6, 2023, 8:08pm UTC](https://aiida.discourse.group/t/how-to-query-for-nodes-belonging-to-intersection-union-of-two-or-more-groups/190/1 "2023-12-06T20:08:19Z")

</div>

Hello,  
I am wondering if there is an easy way to query for nodes that belong to the intersection of two (or more) groups, as you would do with `filters`.

Say we have `group1` and `group2`, and the question is how to get the nodes in common. The following snippet doesn’t work of course:

```python
q = QueryBuilder()
q.append(Group,filters={'label':'goup1'}, tag='g1')
q.append(Group, filters={'label':'goup2'}, tag='g2')
q.append(Node, with_group='g1')
q.append(Node, with_group='g2')

```

It would be nice something like:

```python
q.append(Group,filters={'label':'goup1'}, tag='g1')
q.append(Group, filters={'label':'goup2'}, tag='g2')
q.append(Node, with_groups=['g1','g2'])

```

Any suggestion how to do this the best way?

---

<div class="post-metadata">

**Author:** ![sphuber](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/sphuber/32/6_2.png) [@sphuber](https://aiida.discourse.group/u/sphuber)\
**Post date:** [December 6, 2023, 10:20pm UTC](https://aiida.discourse.group/t/how-to-query-for-nodes-belonging-to-intersection-union-of-two-or-more-groups/190/2 "2023-12-06T22:20:34Z")

</div>

Good question. Unfortunately, as far as I am aware, this syntax is not available and so the best I can think of is to do it in two queries:

```python
builder = QueryBuilder()
builder.append(Group, filters={'label': 'group1'}, tag='g')
builder.append(Node, with_group='g', project='id')
pks = builder.all(flat=True)

builder = QueryBuilder()
builder.append(Group, filters={'label': 'group2'}, tag='g')
builder.append(Node, with_group='g', filters={'id': {'in': pks}})
nodes = builder.all(flat=True)

```

It would be great if we could support the syntax you sketched out, but not sure if it is possible to implement. Maybe the performance of the alternative above is not so bad compared to a single query. Of course if `group1` is large, having to load all pks into memory is not ideal.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/aiida/original/1X/c417197d3750949351575d03ed03f5e2ad32e649.png) [@system](https://aiida.discourse.group/u/system)\
**Post date:** [December 12, 2023, 6:21pm UTC](https://aiida.discourse.group/t/how-to-query-for-nodes-belonging-to-intersection-union-of-two-or-more-groups/190/3 "2023-12-12T18:21:31Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
