<?xml version="1.0"?>
<atom:feed xmlns:atom="http://www.w3.org/2005/Atom" xmlns:xh="http://www.w3.org/1999/xhtml">
  <atom:title type="text">SPARQL Frequently Asked Questions</atom:title>
  <atom:id>http://thefigtrees.net/lee/sw/sparql-faq</atom:id>
  <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq"/>
  <atom:link rel="self" type="application/atom+xml" href="http://thefigtrees.net/lee/sw/sparql-faq-atom.xml"/>
  <atom:author>
    <atom:name>Lee Feigenbaum</atom:name>
    <atom:uri>http://thefigtrees.net/lee/ldf-card#LDF</atom:uri>
    <atom:email>lee@thefigtrees.net</atom:email>
  </atom:author>
  <atom:rights>Creative Commons Attribution 2.5</atom:rights>
  <atom:updated>2008-02-08T12:46:39-05:00</atom:updated>
  <atom:entry>
    <atom:title type="html">What is SPARQL?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>
SPARQL is a <a href="http://en.wikipedia.org/wiki/Recursive_acronym" shape="rect">recursive
acronym</a> standing for SPARQL Protocol and RDF Query Language. As the name
implies, SPARQL is a general term for both a protocol and a query language.</p>

<p>Most uses of the SPARQL acronym refer to the RDF query language. In
this usage, SPARQL is a syntactically-SQL-like language for querying <a href="http://www.w3.org/RDF/" shape="rect">RDF</a> graphs via pattern matching. The
language's features include basic conjunctive patterns, value filters, optional patterns,
and pattern disjunction.</p>

<p>The SPARQL protocol is a method for remote invocation of SPARQL queries. It
specifies a simple interface that can be supported via HTTP or SOAP that
a client can use to issue SPARQL queries against some endpoint.</p>

<p>Both the SPARQL query language and the SPARQL protocol are products of the
<a href="http://www.w3c.org/" shape="rect">W3C's</a> <a href="http://www.w3.org/2001/sw/DataAccess/" shape="rect">RDF Data Access Working
Group</a>. The latest released versions of the Working Group's specifications
(excluding intermediate working drafts) can be found here:</p>
<ul>
<li><a href="http://www.w3.org/TR/rdf-sparql-query/" shape="rect">SPARQL Query Language</a></li>
<li><a href="http://www.w3.org/TR/rdf-sparql-protocol/" shape="rect">SPARQL Protocol</a></li>
<li><a href="http://www.w3.org/TR/rdf-sparql-XMLres/" shape="rect">SPARQL Query Results XML
Format</a></li>
</ul>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#what-is</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#what-is"/>
    <atom:updated>2006-09-29T16:00:00Z</atom:updated>
    <atom:category term="cat-general" label="General SPARQL Discussion"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">How can I learn SPARQL?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
        <p>There are a variety of SPARQL tutorials and introductions scattered
        around the Web. Some notable ones include:</p>
        <ul>
            <li><a href="http://jena.sourceforge.net/ARQ/Tutorial/" shape="rect">Jena/ARQ
                SPARQL tutorial</a></li>
            <li><a href="http://www.xml.com/lpt/a/2005/11/16/introducing-sparql-querying-semantic-web-tutorial.html" shape="rect">Leigh
                Dodds' "Introducing SPARQL" article</a></li>
            <li><a href="http://www-128.ibm.com/developerworks/library/j-sparql/" shape="rect">Philip
                McCarthy's "Search RDF data with SPARQL" article</a></li>
        </ul>
    </div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#tutorials</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#tutorials"/>
    <atom:updated>2007-01-18T20:30:00Z</atom:updated>
    <atom:category term="cat-general" label="General SPARQL Discussion"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">What are the benefits/drawbacks of SPARQL vis a vis SQL
and XQuery?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>
The jury is still out on best practices surrounding using
SPARQL compared to other query languages. Some benefits of SPARQL include:</p>
<ul>
<li>Queries RDF data. If your data is in RDF, then SPARQL can query it
natively.</li>
<li>Implicit join syntax. SPARQL queries RDF graphs, which consist of various
triples expressing binary relations between resources, by specifying a subgraph with certain
resources replaced by variables. Because all relationships are of a fixed size
and data lives in a single graph, SPARQL does not require explicit joins that
specify the relationship between differently structured data. That is, SPARQL is a
query language for pattern matching against RDF graphs, and the queies
themselves <i>look and act</i> like RDF. This is one main
point made by Oracle's Jim Melton in his analysis of SPARQL vis a vis SQL and
XQuery: <a href="http://xtech06.usefulinc.com/schedule/paper/119" shape="rect">SQL, XQuery,
and SPARQL: What's Wrong With This Picture?</a>.</li>
<li>SPARQL has strong support for querying semistructured and ragged data—i.e., data with
an unpredictable and unreliable structure. Variables may occur in the predicate
position to query unknown relationships, and the <tt>OPTIONAL</tt> keyword
provides support for querying relationships that may or may not occur in the
data (a la SQL left joins).</li>
<li>SPARQL is often an appropriate query language for querying disparate data
sources (not sharing a single native representation) in a single query.
Because RDF represents all data as a collection of simple binary relations,
most data can be easily mapped to RDF and then queried and joined using
SPARQL. Often, these mappings can be performed on the fly, meaning that SPARQL
can be used to join heterogeneous data at a higher level than that of the
native structure of the data.</li>
<li>SPARQL is built to support queries in a networked, web environment. SPARQL
introduces the notion of an RDF dataset, which is the pairing of a default
graph and zero or more named graphs. As both the default graph and the named
graphs are identified by URIs, it is common for SPARQL implementations to
retrieve a graph by performing an HTTP GET on the graph's URI. This allows
a single query to join information from multiple data sources accessible
across different Web sites.</li>
<li>Similarly, the SPARQL <tt>GRAPH</tt> keyword allows data to be queried
along with its provenance information. <tt>GRAPH</tt> can be used to discover
the URI of the graph that contains the data that matches the query. </li>
</ul>
<p>Some drawbacks are:</p>
<ul>
<li>Lack of wide deployment. SPARQL is relatively young, and as such there are
not many data stores which can be directly queried with SPARQL (as compared
with SQL or XPath).</li>
<li>Immaturity. As a young query language, SPARQL lacks the explicit processing model of XQuery or the
decades of SQL-optimization research. As with the above point, this is likely
to improve as current and new research and implementations contribute to a
body of knowledge surrounding SPARQL.</li>
<li>Lack of support for transitive/hierarchical queries. While SPARQL is
designed to query RDF graphs, SPARQL has no facilities for easily querying
transitive relations or hierarchical structures within a graph. There are <a href="#transitive" shape="rect">some workarounds</a> for this, but SPARQL does not approach
the power of, for instance, <a href="http://www.w3.org/TR/xquery/#axes" shape="rect">XQuery's axes</a>.</li>
</ul>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#benefits</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#benefits"/>
    <atom:updated>2006-09-29T16:00:00Z</atom:updated>
    <atom:category term="cat-general" label="General SPARQL Discussion"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">What SPARQL implementations are available?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>The community maintains <a href="http://esw.w3.org/topic/SparqlImplementations" shape="rect">a list of SPARQL
implementations</a> at the W3C ESW
Wiki.</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#implementations</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#implementations"/>
    <atom:updated>2006-09-29T16:00:00Z</atom:updated>
    <atom:category term="cat-general" label="General SPARQL Discussion"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Can I use SPARQL to query data that's not stored in RDF?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>Several software packages exist which allow SPARQL queries to generate
answers from data sources other than RDF, such as relational databases, LDAP
servers, or XML data. The community maintains <a href="http://esw.w3.org/topic/RdfAndSql" shape="rect">a list of these
tools</a> at the W3C ESW Wiki.</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#non-rdf-sources</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#non-rdf-sources"/>
    <atom:updated>2006-09-29T16:00:00Z</atom:updated>
    <atom:category term="cat-general" label="General SPARQL Discussion"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">How can I tell what dataset, functions, or extensions a SPARQL endpoint supports?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>
There is currently no established, interoperable method for representing or
accessing functional descriptions of SPARQL endpoints. (This is not to be
confused with the <a href="http://www.w3.org/TR/rdf-sparql-protocol/sparql-protocol-query.wsdl" shape="rect">WSDL</a> 
which describes the SPARQL Protocol itself.)
</p><p>
The Data Access Working Group postponed this topic in 2005, leaving behind <a href="http://www.w3.org/2001/sw/DataAccess/proto-wd/saddle" shape="rect">a
draft</a> "of historical interest only." In the meantime, implementations have
devised their own vocabularies and techniques for specifying and advertising
the services and datasets supported by a SPARQL endpoint. For example, HP
Labs' <a href="http://www.joseki.org/" shape="rect">Joseki</a> allows service descriptions to be <a href="http://www.joseki.org/configuration.html" shape="rect">specified</a> with an RDF
configuration vocabulary. See <a href="http://esw.w3.org/topic/SPARQL/Extensions/Service_Description" shape="rect">the
    SPARQL service description wiki page</a> for more information.
</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#service-description</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#service-description"/>
    <atom:updated>2008-02-08T16:00:00Z</atom:updated>
    <atom:category term="cat-general" label="General SPARQL Discussion"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Do SPARQL queries perform well against small datasets?
Large datasets?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">

<p> The performance of a SPARQL query against any particular dataset depends
not only upon the size of the dataset but also on the nature of the dataset's
storage (a relational store, a native triple store, LDAP, etc.), the
complexity of the query itself, optimizations in use by the SPARQL engine, the
distribution of the data, and other environmental factors.
To date, little work has been done in analyzing SPARQL query performance in
particular, and the field of SPARQL query optimization is relatively
inchoate.</p>

<p>Some analysis has been done on the topic of RDF stores which can handle
large datasets. (A large dataset in this context is usually considered one on
the order of tens or hundreds of millions of triples). The W3C ESW wiki <a href="http://esw.w3.org/topic/LargeTripleStores" shape="rect">contains</a> information on a variety of
RDF stores which can scale to large numbers of triples, but does not speak
specifically to the performance of SPARQL queries against these stores.</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#dataset-size</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#dataset-size"/>
    <atom:updated>2006-09-29T16:00:00Z</atom:updated>
    <atom:category term="cat-general" label="General SPARQL Discussion"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Is there anywhere on the Web where I can try out SPARQL queries?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>
The creators of several SPARQL implementations provide online services where SPARQL queries can be input and executed against either canned datasets or arbitrary datasets (identified by URLs). The community maintains <a href="http://esw.w3.org/topic/SparqlEndpoints" shape="rect">a list of SPARQL endpoints</a> on the W3C ESW Wiki.
</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#sparql-online</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#sparql-online"/>
    <atom:updated>2006-10-18T05:39:00Z</atom:updated>
    <atom:category term="cat-general" label="General SPARQL Discussion"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">What is the status of the W3C SPARQL specifications?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>
On January 15, 2008, the W3C published the three SPARQL specifications
as Recommendations. This means that the specifications have been
endorsed by the W3C membership and should be considered stable
documents.</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#specification-status</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#specification-status"/>
    <atom:updated>2008-01-17T16:24:00Z</atom:updated>
    <atom:category term="cat-w3c" label="W3C and the Data Access Working Group (DAWG)"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Will there be a SPARQL 2? When will feature X be
standardized?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>The Working Group that defined SPARQL left behind <a href="http://www.w3.org/2001/sw/DataAccess/issues" shape="rect">twelve postponed
issues</a>: potential SPARQL features that were not included in the SPARQL
standard due to time constraints and lack of implementation experience.</p>
<p>SPARQL users have asked for many extensions to the SPARQL query language.
Some of these have been accomodated by SPARQL implementations. In an attempt
to inform SPARQL users and to minimize implementation differences of
non-standard SPARQL features, the community maintains a list of <a href="http://esw.w3.org/topic/SPARQL/Extensions" shape="rect">SPARQL extensions</a>.
Discussion of these and other extensions occurs on the <a href="http://lists.w3.org/Archives/Public/public-sparql-dev/" shape="rect">public-sparql-dev@w3.org</a>
mailing list.</p>
<p>There are no immediate plans for a Working Group to deliver a new version
of SPARQL. By maintaining communication between both SPARQL users and
implementors, we hope that when the time comes to standardize a new version of
SPARQL that choosing interoperable extensions to the original SPARQL language
will be straightforward.</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#future-specification</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#future-specification"/>
    <atom:updated>2008-02-08T16:07:00Z</atom:updated>
    <atom:category term="cat-w3c" label="W3C and the Data Access Working Group (DAWG)"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Can I use SPARQL to search for substring matches within
literal values?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>
SPARQL provides the function, <a href="http://www.w3.org/TR/rdf-sparql-query/#funcex-regex" shape="rect"><tt>regex()</tt></a>,
which can be used to test whether a literal value contains a certain
substring:
</p>
<pre xml:space="preserve">
   SELECT ?title 
   WHERE {
     _:book :title ?title .
     FILTER (regex(?title, "SPARQL")) .
   }
</pre>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#regex</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#regex"/>
    <atom:updated>2006-09-29T16:00:00Z</atom:updated>
    <atom:category term="cat-language" label="SPARQL Language"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Why don't I get any matches when I use
regex() to
match typed literals or plain literals with language tags?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>
The <a href="#regex" shape="rect"><tt>regex()</tt></a> function expects its first argument to be either a plain
literal without a language tag or else a typed literal with a datatype of
<tt>xsd:string</tt>. Plain literals <i>with</i> a language tag or typed
literals of other datatypes will evaluate to a type error which causes the
filter to fail.
If you wish <tt>regex()</tt> to match solely based upon a literal's lexical
value, use the <a href="http://www.w3.org/TR/rdf-sparql-query/#func-str" shape="rect"><tt>str()</tt></a>
function, which converts typed and plain literals to simple
literals—i.e., plain literals without a language tag:</p>
<pre xml:space="preserve">
   SELECT ?title 
   WHERE {
     _:book :title ?title .
     FILTER (regex(str(?title), "SPARQL")) .
   }
</pre>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#regex-language-tag</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#regex-language-tag"/>
    <atom:updated>2006-09-29T16:00:00Z</atom:updated>
    <atom:category term="cat-language" label="SPARQL Language"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">How can I query transitive closures / trees / hierarchies
/ RDF lists in SPARQL?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>
There is no built-in support within SPARQL to query hierarchical structures of
an unknown depth (e.g. trees or lists), to query transitive relations, or to
query via XPath like paths. The Data Access Working Group postponed <a href="http://www.w3.org/2001/sw/DataAccess/issues#accessingCollections" shape="rect">this
issue</a> in early 2005.
</p>
<p>There are several workarounds to perform these queries using SPARQL:</p>
<ul>
 <li><i>Repeated queries.</i> A repeating structure can be queried via a
 series of queries. If the structure's internal nodes have URIs (or if the
 SPARQL endpoint supports blank node identifiers which are stable across
 queries ("told bnodes"), then the same query can be issued repeatedly to
 explore the structure. Alternatively, an ever-growing query can be created
 which repeatedly queries the structure from its root to an increasing, fixed
 depth (until a desired value is found or the end of the structure is
 reached).</li>
 <li><i>Inference.</i> In an environment which supports querying over an
 inferred graph, inference rules can be used to specify transitive closures or
 hierarchy membership relations that can then be queried with SPARQL. <a href="http://www.agfa.com/w3c/jdroo/" shape="rect">Jos De
 Roo</a> has sketched <a href="http://lists.w3.org/Archives/Public/www-archive/2005Nov/att-0005/arc.html" shape="rect">examples</a>
 of such SPARQL queries using <a href="http://www.agfa.com/w3c/jdroo/" shape="rect">cwm</a> and <a href="http://www.agfa.com/w3c/euler/" shape="rect">Euler</a> and a
 suitable <a href="http://eulersharp.sourceforge.net/2003/03swap/rpo-rules.n3" shape="rect">set of N3
 rules.</a></li>
 <li><i>Implementation-specific approaches.</i> Several SPARQL implementations
 provide ways to address this question. In <a href="http://jena.sourceforge.net/ARQ/" shape="rect">ARQ</a>, for example, there are two
 approaches:
 <ol>
  <li>A filter function named
  <tt>&lt;java:com.hp.hpl.jena.query.function.library.listMember&gt;</tt>
  which takes an RDF list node and a resource as parameters and returns
  <tt>true</tt> if the resource is a member of the list.</li>
  <li>A special predicate named
  <tt>&lt;http://www.jena.hpl.hp.com/ARQ/list#member&gt;</tt> can be used
  inside the SPARQL query pattern (the <tt>WHERE</tt> clause) to associate an
  RDF list head with all the list members.</li>
 </ol>
 </li>
</ul>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#transitiv8</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#transitiv8"/>
    <atom:updated>2006-10-01T16:00:00Z</atom:updated>
    <atom:category term="cat-language" label="SPARQL Language"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Can I include subqueries in a SPARQL query?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>While SPARQL does support nested graph patterns, it 
does not directly support subqueries (for example, the <tt>FROM</tt> clause of
a SPARQL query cannot itself contain a <tt>CONSTRUCT</tt> query which
generates the dataset to be queried against). The Data Access Working Group
postponed <a href="http://www.w3.org/2001/sw/DataAccess/issues#cascadedQueries" shape="rect">this
issue</a> in early 2005. A very limited form of subqueries can be accomplished
with SPARQL engines that will perform HTTP GETs upon graphs named in
<tt>FROM</tt> or <tt>FROM NAMED</tt> clauses by creating a URL consisting of
an embedded SPARQL
<tt>CONSTRUCT</tt> query submitted to a SPARQL endpoint and supplying this URL
as part of the RDF dataset being queried. In practice, this technique is often
inefficient and is subject to possible URL-maximum-length restrictions of the
involved software.</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#subqueries</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#subqueries"/>
    <atom:updated>2006-09-29T16:00:00Z</atom:updated>
    <atom:category term="cat-language" label="SPARQL Language"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Can I bind a variable to a specific value (e.g., the result of a function call)?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer"><p>SPARQL does not support setting variable bindings except
via graph pattern matching or via the <tt>GRAPH ?g</tt> construct. Expressions
are not allowed in the <tt>SELECT</tt> list of a SPARQL query.</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#variable-values</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#variable-values"/>
    <atom:updated>2006-09-29T16:00:00Z</atom:updated>
    <atom:category term="cat-language" label="SPARQL Language"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">How can I use SPARQL to query maximum/minimum values or other universally quantified criteria?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>A combination of the SPARQL <tt>OPTIONAL</tt> keyword and the
<tt>bound(...)</tt> filter function can be used to mimic some universally
quantified queries. As an example, consider this query which finds the minimum
price of every book in the underlying default graph:</p>
<pre xml:space="preserve">
  PREFIX ex: &lt;http://example.org/&gt;
  SELECT ?book ?minprice
  WHERE {
    ?book a ex:book ; ex:price ?minprice .
    OPTIONAL { 
      ?book ex:price ?otherprice . 
      FILTER( ?otherprice &lt; ?minprice ) .
    } .
    FILTER ( !bound(?otherprice) ) .
  }
</pre>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#universal</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#universal"/>
    <atom:updated>2006-09-29T16:00:00Z</atom:updated>
    <atom:category term="cat-language" label="SPARQL Language"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Can I use SPARQL to select a single value based on an
ordered list of predicates which might appear in the data?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>When writing SPARQL queries against heterogeneous data sources, one often
wants to select a value for a certain purpose without knowing which one of
several predicates might be used in the data. The SPARQL <a href="http://www.w3.org/TR/rdf-sparql-query/#optionals" shape="rect"><tt>OPTIONAL</tt></a>
keyword can be used to accomplish this. Suppose we are selecting a
human-readable label for a Web page, and we want to use the value of the <a href="http://dublincore.org/" shape="rect">Dublin Core</a> <a href="http://dublincore.org/2006/08/28/dces.rdf#title" shape="rect">title</a> predicate
(<tt>dc:title</tt>) if it exists, and otherwise use the value of the <a href="http://www.w3.org/TR/rdf-schema/#ch_label" shape="rect"><tt>rdfs:label</tt></a>
predicate. This can be accomplished with SPARQL idiom:</p>
<pre xml:space="preserve">
  PREFIX rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;    
  PREFIX dc: &lt;http://purl.org/dc/elements/1.1/&gt;
  SELECT ?text
  WHERE {
    OPTIONAL { &lt;http://example.org/myWebPage&gt; dc:title ?text . }
    OPTIONAL { &lt;http://example.org/myWebPage&gt; rdfs:label ?text . }
  }
</pre>
<p>More information on this idiom is available in <a href="http://www.thefigtrees.net/lee/blog/2006/04/sparql_calendar_demo_using_spa.html" shape="rect">this
blog post</a>.</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#alternative-predicates</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#alternative-predicates"/>
    <atom:updated>2006-09-29T16:00:00Z</atom:updated>
    <atom:category term="cat-language" label="SPARQL Language"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Can I use SPARQL to query RDFS entailments?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">

<p>It is sometimes desirable to use SPARQL to query triples entailed from subclass, subproperty,
range, domain, and other relations which can be represented using <a href="http://www.w3.org/TR/rdf-schema/" shape="rect">RDF Schema.</a></p>

<p>The SPARQL specification <a href="http://www.w3.org/TR/rdf-sparql-query/#BasicGraphPatternMatching" shape="rect">defines</a> the results of queries
based on <a href="http://www.w3.org/TR/rdf-mt/#entail" shape="rect">RDF simple
entailment</a>. However, the specification does present a general, parametrized
definition of 
graph pattern matching that can be expanded to other
entailments beyond RDF simple entailment.</p>
<p>
As an alternative, the SPARQL specification acknowledges that queries are
often performed against a virtual graph which may not be fully materialized.
Because RDFS entailment rules will always lead to a unique, deductive closure
of a base graph, a query engine can treat the RDFS deductive closure of a base
graph as the virtual graph against which (simple-entailment based) SPARQL
queries are executed.
</p>

<p>Whether or not a particular query endpoint supports RDFS entailment is
implementation defined. Such a property might be advertised as part of the
endpoint's <a href="#service-description" shape="rect">functional description</a>.</p>

<p>For more information, see information on <a href="http://esw.w3.org/topic/SPARQL/Extensions/Entailment_Regimes" shape="rect">SPARQL
extensions for other entailment regimes</a>.</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#rdfs</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#rdfs"/>
    <atom:updated>2008-02-08T16:00:00Z</atom:updated>
    <atom:category term="cat-language" label="SPARQL Language"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Can I use SPARQL to query OWL entailments?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>For the most part, the answer to this question is the same as the <a href="#rdfs" shape="rect">above answer</a> regarding RDFS entailment. However, OWL-DL
axioms do not always result in a <i>unique</i> deductive closure, and as such
querying OWL-DL entailments requires instantiating the parametrized SPARQL
basic graph pattern definition with values appropriate for OWL-DL
entailment. The open-source OWL-DL reasoner, <a href="http://www.mindswap.org/2003/pellet/" shape="rect">Pellet</a>, will answer SPARQL
queries while considering OWL-DL entailments.</p>

<p>For more information, see information on <a href="http://esw.w3.org/topic/SPARQL/Extensions/Entailment_Regimes" shape="rect">SPARQL
extensions for other entailment regimes</a>.</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#owl</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#owl"/>
    <atom:updated>2008-02-08T16:00:00Z</atom:updated>
    <atom:category term="cat-language" label="SPARQL Language"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">What do blank nodes mean in a SPARQL query?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>
For the most part, blank nodes in SPARQL queries function exactly as variables
which cannot be returned to the user/client. So, the following two queries
behave identically:</p>
<pre xml:space="preserve">
  SELECT ?title WHERE { _:book :hasTitle ?title }
</pre>
<pre xml:space="preserve">
  SELECT ?title WHERE { ?book :hasTitle ?title }
</pre>
<p>However, a SPARQL query <i>may not</i> reuse the same blank node label
twice in different <a href="http://www.w3.org/TR/rdf-sparql-query/#BasicGraphPatternMatching" shape="rect">basic
graph patterns</a>. That is, the following is not a legal
SPARQL query:</p>
<pre xml:space="preserve">
  SELECT ?title WHERE { 
    _:book rdfs:seeAlso ?g .
    GRAPH ?g { _:book dc:title ?title }
  }
</pre>

<p>In most cases, it is best practice to use the <tt>[]</tt> and <tt>[ :prop
    :obj ]</tt> syntaxes for blank nodes and to only use explicit blank node
labels for constructs that cannot be expresed otherwise.</p>

</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#blank-nodes</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#blank-nodes"/>
    <atom:updated>2007-02-11T23:00:00Z</atom:updated>
    <atom:category term="cat-language" label="SPARQL Language"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Did you know that the SPARQL WHERE keyword is
optional?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
        <p>Well, it is!</p>
      </div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#where-keyword</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#where-keyword"/>
    <atom:updated>2006-09-29T16:00:00Z</atom:updated>
    <atom:category term="cat-language" label="SPARQL Language"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html"> Why don't I get any matches when I search for numbers?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>Numbers in RDF data can be represented as plain literals or as typed  
literals (using <a href="http://www.w3.org/TR/xmlschema-2/" shape="rect">XML Schema datatypes</a>
such as <tt>xsd:int</tt>). Numbers written in a SPARQL  
query <i>with</i> surrounding quotation marks (e.g., <tt>"4"</tt>) will only match 
plain literals in the dataset. Numbers  
written <i>without</i> quotation marks (e.g. <tt>4</tt>) will only match typed 
literals. Be sure to use the appropriate form for the data you are querying.</p>

<p>If your data contains numbers as plain literals, then they will be  
compared as strings not numbers, and you may have to cast them to  
typed literals to get the desired results. For example, to check if a number expressed
as a plain literal is less than 100, you'd say:</p>
<pre xml:space="preserve">
  ... 
  FILTER (xsd:int(?number) &lt; 1000)
  ...
</pre>
<p><i>Many thanks to <a href="http://dowhatimean.net/" shape="rect">Richard Cyganiak</a> for contributing this question and answer.</i></p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#numbers</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#numbers"/>
    <atom:updated>2006-10-05T05:10:00Z</atom:updated>
    <atom:category term="cat-language" label="SPARQL Language"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Can I use SPARQL to insert, update, or delete RDF data?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>The current, standardized version of SPARQL deals only with retrieving selected data from RDF graphs. There is no equivalent of the SQL <tt>INSERT</tt>, <tt>UPDATE</tt>, or <tt>DELETE</tt> statements. Most RDF-based applications handle new, changing, and stale data directly via the APIs provided by <a href="http://esw.w3.org/topic/SemanticWebTools#head-805c63479c854babe4657d5184de605910f6d3e2" shape="rect">specific RDF storage systems</a>. Alternatively, RDF data can exist virtually (i.e. created on-demand in response to a SPARQL query). Also, there are systems which create RDF data from other forms of markup, such as <a href="http://wiki.ontoworld.org/index.php/Semantic_Wiki_State_Of_The_Art" shape="rect">Wiki markup</a> or <a href="http://wingerz.com/blog/?p=35" shape="rect">the Atom Syndication Format</a>.</p>
<p>However, there is significant active work going on to extend SPARQL to
support update operations. See the <a href="http://esw.w3.org/topic/SPARQL/Extensions/Update" shape="rect">SPARQL extension wiki
page dealing with update</a> for more details.</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#update</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#update"/>
    <atom:updated>2008-02-08T16:27:00Z</atom:updated>
    <atom:category term="cat-language" label="SPARQL Language"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Can I receive the results of SPARQL queries as
RDF?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>The <a href="http://www.w3.org/TR/rdf-sparql-protocol/#SparqlQuery" shape="rect">SPARQL
protocol</a> requires that the results of SPARQL <a href="http://www.w3.org/TR/rdf-sparql-query/#construct" shape="rect"><tt>CONSTRUCT</tt></a> and <a href="http://www.w3.org/TR/rdf-sparql-query/#describe" shape="rect"><tt>DESCRIBE</tt></a> queries be
RDF graphs.</p>

<p><a href="http://www.w3.org/TR/rdf-sparql-query/#select" shape="rect"><tt>SELECT</tt></a> and <a href="http://www.w3.org/TR/rdf-sparql-query/#ask" shape="rect"><tt>ASK</tt></a> queries, on the
other hand, usually have their <a href="#results-in-xml" shape="rect">results returned as
XML</a> (or sometimes <a href="#results-in-json" shape="rect">as JSON</a>). However, as
part of the group's test suite, the <abbr title="Data Access Working Group">DAWG</abbr> includes an <a href="http://www.w3.org/2001/sw/DataAccess/tests/result-set.n3" shape="rect">RDF
vocabulary</a> which can be used to represent
the results of <tt>SELECT</tt> or <tt>ASK</tt> queries in RDF.</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#results-in-rdf</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#results-in-rdf"/>
    <atom:updated>2006-09-29T16:00:00Z</atom:updated>
    <atom:category term="cat-protocol" label="SPARQL Protocol"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Can I receive the results of SPARQL queries as
XML?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p>The <abbr title="Data Access Working Group">DAWG</abbr> defines an XML
serialization format, the <a href="http://www.w3.org/TR/rdf-sparql-XMLres/" shape="rect">SPARQL Query Results XML
Format</a>, which is used to return the results of SPARQL 
<a href="http://www.w3.org/TR/rdf-sparql-query/#select" shape="rect"><tt>SELECT</tt></a> and <a href="http://www.w3.org/TR/rdf-sparql-query/#ask" shape="rect"><tt>ASK</tt></a> queries. 
The SPARQL protocol requires that the results of SPARQL <a href="http://www.w3.org/TR/rdf-sparql-query/#construct" shape="rect"><tt>CONSTRUCT</tt></a> and <a href="http://www.w3.org/TR/rdf-sparql-query/#describe" shape="rect"><tt>DESCRIBE</tt></a>
queries be RDF graphs, which can be represented using the XML-based <a href="http://www.w3.org/TR/rdf-syntax-grammar/" shape="rect">RDF/XML syntax</a>.</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#results-in-xml</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#results-in-xml"/>
    <atom:updated>2006-09-29T16:00:00Z</atom:updated>
    <atom:category term="cat-protocol" label="SPARQL Protocol"/>
  </atom:entry>
  <atom:entry>
    <atom:title type="html">Can I receive the results of SPARQL queries as
JSON?</atom:title>
    <atom:content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml" class="answer">
<p><a href="http://www.json.org/" shape="rect">JSON</a> is a lightweight serialization
format which is a subset of JavaScript's literal-object notation. It is a
useful format to use for SPARQL results when a Web client is issuing the
queries and processing the results.
The <abbr title="Data Access Working Group">DAWG</abbr> has published 
<a href="http://www.w3.org/TR/rdf-sparql-json-res/" shape="rect">a Note defining a JSON format</a>
that closely follows the <a href="http://www.w3.org/TR/rdf-sparql-XMLres/" shape="rect">SPARQL Query Results XML
Format</a>, and which can be used to return the results of SPARQL 
<a href="http://www.w3.org/TR/rdf-sparql-query/#select" shape="rect"><tt>SELECT</tt></a> and <a href="http://www.w3.org/TR/rdf-sparql-query/#ask" shape="rect"><tt>ASK</tt></a> queries.
</p>
</div>
    </atom:content>
    <atom:id>http://thefigtrees.net/lee/sw/sparql-faq#results-in-json</atom:id>
    <atom:link rel="alternate" type="application/xhtml+xml" href="http://thefigtrees.net/lee/sw/sparql-faq#results-in-json"/>
    <atom:updated>2006-10-05T08:00:00Z</atom:updated>
    <atom:category term="cat-protocol" label="SPARQL Protocol"/>
  </atom:entry>
</atom:feed>
