SPARQL Language

Can I use SPARQL to search for substring matches within literal values?

SPARQL provides the function, regex(), which can be used to test whether a literal value contains a certain substring:
SELECT ?title
_:book :title ?title .
FILTER (regex(?title, “SPARQL”)) .
Why don’t I get any matches when I use regex() to match typed literals or plain literals with language tags?

The regex() function expects its first argument to be either a plain literal without a language tag or else a typed literal with a datatype of xsd:string. Plain literals witha language tag or typed literals of other datatypes will evaluate to a type error which causes the filter to fail. If you wish regex() to match solely based upon a literal’s lexical value, use the str() function, which converts typed and plain literals to simple literals—i.e., plain literals without a language tag:

SELECT ?title
_:book :title ?title .
FILTER (regex(str(?title), “SPARQL”)) .
How can I query transitive closures / trees / hierarchies / RDF lists in SPARQL?

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 this issue in early 2005.

There are several workarounds to perform these queries using SPARQL:

Repeated queries. 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).
Inference. 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. Jos De Roo has sketched examples of such SPARQL queries using cwm and Euler and a suitable set of N3 rules.
Implementation-specific approaches. Several SPARQL implementations provide ways to address this question. In ARQ, for example, there are two approaches:
A filter function named <java:com.hp.hpl.jena.query.function.library.listMember> which takes an RDF list node and a resource as parameters and returns true if the resource is a member of the list.
A special predicate named <> can be used inside the SPARQL query pattern (the WHERE clause) to associate an RDF list head with all the list members.

Can I include subqueries in a SPARQL query?

While SPARQL does support nested graph patterns, it does not directly support subqueries (for example, the FROM clause of a SPARQL query cannot itself contain aCONSTRUCT query which generates the dataset to be queried against). The Data Access Working Group postponed this issue in early 2005. A very limited form of subqueries can be accomplished with SPARQL engines that will perform HTTP GETs upon graphs named in FROM or FROM NAMED clauses by creating a URL consisting of an embedded SPARQL CONSTRUCT 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.

Can I bind a variable to a specific value (e.g., the result of a function call)?

SPARQL does not support setting variable bindings except via graph pattern matching or via the GRAPH ?g construct. Expressions are not allowed in the SELECT list of a SPARQL query.

How can I use SPARQL to query maximum/minimum values or other universally quantified criteria?

A combination of the SPARQL OPTIONAL keyword and the bound(…) 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:

PREFIX ex: <>
SELECT ?book ?minprice
?book a ex:book ; ex:price ?minprice .
?book ex:price ?otherprice .
FILTER( ?otherprice < ?minprice ) .
} .
FILTER ( !bound(?otherprice) ) .
Can I use SPARQL to select a single value based on an ordered list of predicates which might appear in the data?

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 OPTIONAL 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 Dublin Core title predicate (dc:title) if it exists, and otherwise use the value of the rdfs:label predicate. This can be accomplished with SPARQL idiom:

PREFIX rdfs: <>
PREFIX dc: <>
SELECT ?text
OPTIONAL { <> dc:title ?text . }
OPTIONAL { <> rdfs:label ?text . }
Can I use SPARQL to query RDFS entailments?

It is sometimes desirable to use SPARQL to query triples entailed from subclass, subproperty, range, domain, and other relations which can be represented using RDF Schema.

The SPARQL specification defines the results of queries based on RDF simple entailment. However, the specification does present a general, parametrized definition of graph pattern matching that can be expanded to other entailments beyond RDF simple entailment.

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.

Whether or not a particular query endpoint supports RDFS entailment is implementation defined. Such a property might be advertised as part of the endpoint’sfunctional description.

For more information, see information on SPARQL extensions for other entailment regimes.

Can I use SPARQL to query OWL entailments?

For the most part, the answer to this question is the same as the above answer regarding RDFS entailment. However, OWL-DL axioms do not always result in a uniquedeductive 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, Pellet, will answer SPARQL queries while considering OWL-DL entailments.

For more information, see information on SPARQL extensions for other entailment regimes.

What do blank nodes mean in a SPARQL query?

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:

SELECT ?title WHERE { _:book :hasTitle ?title }
SELECT ?title WHERE { ?book :hasTitle ?title }
However, a SPARQL query may not reuse the same blank node label twice in different basic graph patterns. That is, the following is not a legal SPARQL query:

_:book rdfs:seeAlso ?g .
GRAPH ?g { _:book dc:title ?title }
In most cases, it is best practice to use the [] and [ :prop :obj ] syntaxes for blank nodes and to only use explicit blank node labels for constructs that cannot be expresed otherwise.

Did you know that the SPARQL WHERE keyword is optional?

Well, it is!

 Why don’t I get any matches when I search for numbers?

Numbers in RDF data can be represented as plain literals or as typed literals (using XML Schema datatypes such as xsd:int). Numbers written in a SPARQL query withsurrounding quotation marks (e.g., “4”) will only match plain literals in the dataset. Numbers written without quotation marks (e.g. 4) will only match typed literals. Be sure to use the appropriate form for the data you are querying.

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:

FILTER (xsd:int(?number) < 100)

Many thanks to Richard Cyganiak for contributing this question and answer.

Can I use SPARQL to insert, update, or delete RDF data?

The current, standardized version of SPARQL deals only with retrieving selected data from RDF graphs. There is no equivalent of the SQL INSERT, UPDATE, or DELETEstatements. Most RDF-based applications handle new, changing, and stale data directly via the APIs provided by specific RDF storage systems. 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 Wiki markup or the Atom Syndication Format.

However, there is significant active work going on to extend SPARQL to support update operations. See the SPARQL extension wiki page dealing with update for more details.