<docbook><section><title>VirtSPARQLSubQuery</title><para> </para>
<title>How can I use sub-queries to enable literal value based joins?</title>How can I use sub-queries to enable literal value based joins?
<bridgehead class="http://www.w3.org/1999/xhtml:h2">What?</bridgehead>
<para>Use of SPARQL sub-queries to enable the dynamic generation of literal values for SPO triple objects.</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2">Why?</bridgehead>
<para>Sophisticated access to literal values via sub-queries provides a powerful mechanism for enhancing SPARQL graph patterns via dynamic literal value generation.</para>
<para> </para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2">How?</bridgehead>
<para>Use select list variables from sub-queries to produce literal object values in SPARQL graph patterns as demonstrated in the example below.</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h3">Example</bridgehead>
<para>Assume the following query in which for ?app there is only exactly one resource which has this identifier: </para>
<programlisting>SELECT DISTINCT ?r 
WHERE 
  {
    graph ?g 
      {
        ?r nie:url ?url .
      }  .
      ?g nao:maintainedBy ?app .
      ?app nao:identifier &quot;nepomukindexer&quot; .
  }
</programlisting><para> If one is not sure that ?app is the only identifier (e.g., if triple ?app nao:identifier &quot;nepomukindexer&quot; can appear in more than one graph), then the query can be rewritten to:</para>
<programlisting>SELECT DISTINCT ?r 
WHERE 
  {
    graph ?g 
      {
        ?r nie:url ?url .
      }  .
      ?g nao:maintainedBy ?app .
      FILTER (?app = (SELECT ?a WHERE { ?a nao:identifier &quot;nepomukindexer&quot; }))
}
</programlisting><para> or even simpler:</para>
<programlisting>SELECT DISTINCT ?r 
WHERE 
  {
   graph ?g 
     {
       ?r nie:url ?url .
     }  .
   ?g nao:maintainedBy `(SELECT ?a WHERE { ?a nao:identifier &quot;nepomukindexer&quot; })` .
 }
</programlisting><para> </para>
<bridgehead class="http://www.w3.org/1999/xhtml:h3">Related</bridgehead>
<itemizedlist mark="bullet" spacing="compact"><listitem><ulink url="VirtTipsAndTricksGuide">Virtuoso Tips and Tricks Collection</ulink> </listitem>
</itemizedlist></section></docbook>