text: Q:
  
  Return common node between 2 different queries
  
  I want to find out the common node between 2 different queries. I am trying hard to find out, but could not think of a solution. My motive is to collect top 5 out-degree nodes, collect top 5 root nodes, and return nodes that are common between top 5 outdegree and root nodes. I don't know how to merge the results because after using "return" option in the first query, no further statements would be executed, but without "return" option we cannot collect results. (Please do correct me, if I am thinking wrong). Following are the queries,
  // for root nodes
  match (u:Port1)<-[r]-(root)
  where not((root)<--())
  return distinct(root.id) as Node, count(r) as Outdegree
  ORDER BY count(r) desc limit 5
  
  //for outdegree nodes
  match (n:Port1)-[r]->()
  return n.id as Node, count(r) as Outdegree
  order by Outdegree DESC
  union
  match (a:Port1)-[r]->(leaf)
  where not((leaf)-->())
  return leaf.id as Node, 0 as Outdegree limit 5
  
  How should I combine both results, and get the output of a list of nodes that are common? Please do help me. Thanks in advance.
  
  A:
  
  If you just want the intersection between the two, you don't even need unions. Just collect the top 5 in both cases and find the intersection.
  APOC Procedures can help here. Something like:
  // for root nodes
  match (:Port1)<-[r]-(root)
  where not((root)<--())
  // no need for distinct since you're aggregating
  with root, count(r) as Outdegree
  ORDER BY Outdegree desc limit 5
  with collect(root) as rootNodes // back to a single row
  
  //for outdegree nodes
  match (n:Port1)
  // more efficient way to get out degree without needing to expand
  with rootNodes, n, size((n)-->()) as Outdegree
  order by Outdegree DESC limit 5
  
  with rootNodes, collect(n) as outdegreeNodes
  with apoc.coll.intersection(rootNodes, outdegreeNodes) as commonNodes
  
  // if needed, unwind and get outdegree
  unwind commonNodes as commonNode
  return commonNode.id as Node, size((commonNode)-->()) as Outdegree
  
  