# Basic intersection of sets using Cypher

**URL:** <https://community.neo4j.com/t/basic-intersection-of-sets-using-cypher/19758>\
**Category:** Cypher\
**Created:** [June 9, 2020, 5:58pm UTC](https://community.neo4j.com/t/basic-intersection-of-sets-using-cypher/19758 "2020-06-09T17:58:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rui\_madaleno](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/rui_madaleno/32/5106_2.png) [@rui\_madaleno](https://community.neo4j.com/u/rui_madaleno)\
**Post date:** [June 9, 2020, 5:58pm UTC](https://community.neo4j.com/t/basic-intersection-of-sets-using-cypher/19758/1 "2020-06-09T17:58:50Z")

</div>

Hi Group !

I'm a very basic and newbie user of neo4J and Cypher and I'm trying to build a query.

This is about the visibility of a Component in an application given a set of eligibility conditions (items) for the Employee that is connected to the application.

the data model:

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/e/e3800f9a5b386ec5b754eb3b12e3437663e25255.png)

What I'm trying ?

- Get a first set of names of eligibility items for an Employee
- Get a second set of name of eligibility items for a Block
- Intersect both sets (using APOC function), if something is common it means that i can display/render the Block for that Employee

my (very bad) attempts 🙄

```auto
match (ee:Employee {name:"Carlos"})-[:EE_Eligible_For]->(o:EligibilityItem) with ee, collect(o.name) as l1
match (:Component {name:"HEALTH_ACCORDION"})-[:Is_Eligible_For]->(e:EligibilityItem)  
with collect(e.name) as l2, collect (o.name) as l2 return apoc.coll.intersection (l1, l2)

```

```auto
// fetch the eligibility items for the component
match (c:component {key:"HEALTH_ACCORDION"})->[:Is_Eligible_For]->(e:EligibilityItem)
with c,collect(e) as ComponentEList
// fetch the eligibility items for the logged user
match (ee:Employee {name:"Carlos")->[:EE_Eligible_For]->(e2:EligibilityItem)
with ee, collect(e2) as EmployeeEList
match (sal:SalaryClass) where ee.salary > sal.min and ee.salary < sal.max
with sal.name + EmployeeEList 
// intersect the eligibility list
return apoc.coll.intersect(ComponentEList,EmployeeEList)

```

Greatly appreciate if you can point me the right direction to achieve my goal.

Running Neo4J 4.0.4

best regards

Rui

---

<div class="post-metadata">

**Author:** ![rui\_madaleno](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/rui_madaleno/32/5106_2.png) [@rui\_madaleno](https://community.neo4j.com/u/rui_madaleno)\
**Post date:** [June 10, 2020, 5:41pm UTC](https://community.neo4j.com/t/basic-intersection-of-sets-using-cypher/19758/2 "2020-06-10T17:41:56Z")

</div>

I've tried and tried and tried again 🤕 🤕 🤕

but i did it ! 🥳

in short, I was misusing the "WITH" instruction 🙄

My solution:

```auto
// get eligibility items for employee 
match (ee:Employee {name:"Carlos"})-[:EE_Eligible_For]->(e1:EligibilityItem)
with collect(e1.name) as l1
// get eligibility items for component
match (:Component {name:"HEALTH_ACCORDION"})-[:Is_Eligible_For]->(e2:EligibilityItem)  
with collect(e2.name) as l2,l1 
// check if there is common eligibility items 
return apoc.coll.intersection (l1, l2) CommonEligibilityItem, size(apoc.coll.intersection (l1, l2)) as NCommonEligibilityItem

```
