title: Querying resources in a relationship chain in SPARQL
body: <p>I'm new to SPARQL and I'm trying to fetch some results from triples in a SQL join fashion.</p>
  
  <p>e.g. I'm looking for the ID of the object a certain image belongs to, and the ID of the author of the object, and so on.</p>
  
  <p>RDF relations are defined like this: </p>
  
  <pre><code>&lt;image&gt; &lt;isResourceOf&gt; &lt;object&gt;
  &lt;person&gt; &lt;isAuthorOf&gt; &lt;object&gt;
  </code></pre>
  
  <p>I tried this:</p>
  
  <pre><code>SELECT ?o ?a from &lt;#ri&gt; 
  WHERE {
    {&lt;myns:myImageID&gt; &lt;myns:isResourceOf&gt; ?o}
    UNION
    {?a &lt;myns:isAuthorOf&gt; ?o}
  }
  </code></pre>
  
  <p>But this fetches all the authors, since the two <code>?o</code> variables are not bound. </p>
  
  <p>How do I get to match the authors ONLY for the first match set?</p>
  
  <p>Thanks</p>
  
  <p>gm</p>
  
  <p>EDIT:</p>
  
  <p>More complete example:</p>
  
  <pre><code>PREFIX aic: &lt;http://mydomain.org/definitions/1.0/fedora/3#&gt;
  
  SELECT *
  WHERE {
    &lt;info:fedora/AICTEST:DOResImg-G38562&gt; aic:isResourceOf ?o . 
    ?a aic:isAuthorOf ?o .
  }
  </code></pre>
  
  <p>No results.</p>
  
  <pre><code>SELECT *
  WHERE {
    &lt;info:fedora/AICTEST:DOResImg-G38562&gt; aic:isResourceOf ?o . 
  }
  </code></pre>
  
  <p>Results (CSV):</p>
  
  <pre><code>"o"
  AICTEST:DOArtObj-1946.479
  </code></pre>
  
  <p>With this:</p>
  
  <pre><code>SELECT *
  WHERE {
    ?a aic:isAuthorOf ?o .
  }
  </code></pre>
  
  <p>Results:</p>
  
  <pre><code>"a","o"
  info:fedora/AICTEST:DOAgent-35729,info:fedora/AICTEST:DOArtObj-1946.479
  info:fedora/AICTEST:DOAgent-35729,AICTEST:DOObjSet-1946.494
  </code></pre>