title: Nested MATCH statements in cypher
body: <p><code>(client)-[:listens]-&gt;(album)-[:contains]-&gt;(song)</code> are my relationships. I need to find how many different and which songs are listened by clients have listened a song with ID 4 (f.e.).</p>
  
  <p>In SQL I could easily accomplish that with double-nested <code>JOIN</code>s and <code>SELECT</code>s, but since I am a novice in <code>Cypher</code>, I'm kinda lost. The scenario is as follows:</p>
  
  <ol>
  <li>Find which clients (their IDs) have listened to song with ID 4</li>
  <li>Get all songs that have been listened to, from the clients found in the previous step, and return them (and their count as well).</li>
  </ol>
  
  <p>I tried to start with smth like:</p>
  
  <pre><code>MATCH (c:Client)--(a:Album)--(s:Song)
  WITH a, s, {cIDs: c.clientID} AS clientsIDs
  // MATCH that gets all songs in albums listened by clients WHERE c.clientID IN clientsIDs -- how to make that?
  WITH COUNT(s), COLLECT(s) as songs
  WHERE s.songID = "4"
  RETURN songs
  </code></pre>
  
  <p>This was another try:</p>
  
  <pre><code>MATCH (c:Client)--(a:Album)--(s:Song)
  WHERE s.songID = "4"
  WITH a, s, {cID: c.clientID} AS clientsIDs
  MATCH (c {clientID: clientsIDs})-[:LISTENS]-&gt;(a)-[:CONTAINS]-&gt;(s)
  WITH COUNT(s) AS songsCount, COLLECT(s) as songs
  RETURN songs, songsCount
  </code></pre>
  
  <p>which obviously didn't work.</p>
  
  <p>I just can't get around the part where I pass the <code>clientsIDs</code> to another <code>MATCH</code> statement...</p>