SPARQL (pronounced "sparkle", a recursive acronym for SPARQL Protocol and RDF Query Language) is a set of specifications by W3C that provide languages and protocols to query and manipulate RDF graph content on the Web or in an RDF store.
SPARQL
SPARQL (pronounced "sparkle", a recursive acronym for SPARQL Protocol and RDF Query Language) is a set of specifications by W3C that provide languages and protocols to query and manipulate RDF graph content on the Web or in an RDF store.
SPARQL 1.0
SPARQL 1.0 is the original version of SPARQL, and simply provides a query language for RDF. The language is based on Graph Pattern matching and provides 4 forms of query:
ASK WHERE { }
- AnASK
query simply asks whether there exists a match for the Graph Pattern stated in theWHERE
clause in the data being queried.
This returns a Boolean SPARQL Results Set containing a True/False response.SELECT * WHERE { }
- ASELECT
query finds all the solutions that match the Graph Pattern and returns the desired parts of them. Results can beORDER
ed as desired and useLIMIT
and/orOFFSET
for paging purposes. This is the most commonly used query form and corresponds in function (if very differently in syntax and semantics) to the SQL that many developers coming to the Semantic Web are familiar with.
This returns a SPARQL Result Set containing the solutions.DESCRIBE <http://example.org>
- ADESCRIBE
query gets the description of one/more resources from the data. The query engine is free to decide what constitutes a description. AWHERE
clause may be used to select what resources are to be described.
This returns an RDF Graph.CONSTRUCT { } WHERE { }
- ACONSTRUCT
query takes solutions that match theWHERE
clause and uses them to construct a new RDF Graph.
This returns an RDF Graph.
SPARQL 1.0 Example
A SPARQL 1.0 query might look like the following:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT *
FROM <http://default>
WHERE
{
?s a ?type .
OPTIONAL
{
?s rdfs:label ?label .
FILTER (LANGMATCHES(?label, "en"))
}
}
ORDER BY ?label
LIMIT 10
This query looks for things with a type in the graph <http://default>
and optionally includes their labels provided those labels are in English. It orders the results by the label limiting the results returned to 10.
SPARQL 1.1
SPARQL 1.1 is a major extension to the SPARQL ecosystem approved as a W3C Recommendation in March 2013. It provides many extensions to the existing query language including:
- Project expressions in
SELECT
, e.g.(?x + ?y AS ?z)
- Aggregates, e.g.
COUNT()
,GROUP BY
, andHAVING
- Property Paths, e.g.
{?x ex:predicate+ ?y}
EXISTS
andNOT EXISTS
filtersMINUS
clause for subtractive negationSERVICE
clause for federated querying- Subqueries
- Many new built in functions particularly around string and date manipulation
It also adds a number of entirely new features into the ecosystem including:
- The SPARQL Update language which defines a language for modifying RDF data.
- The Graph Store HTTP Protocol which provides a standard RESTful interface for Graph based RDF stores.
- Service Description for providing an RDF description of a SPARQL endpoint.
See the SPARQL 1.1 Implementation Report for implementations that have reported compliance test results. See the SPARQL Wikipedia article for examples, extensions, and another list of implementations.