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 { }- AnASKquery simply asks whether there exists a match for the Graph Pattern stated in theWHEREclause in the data being queried.
This returns a Boolean SPARQL Results Set containing a True/False response.SELECT * WHERE { }- ASELECTquery finds all the solutions that match the Graph Pattern and returns the desired parts of them. Results can beORDERed as desired and useLIMITand/orOFFSETfor 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>- ADESCRIBEquery gets the description of one/more resources from the data. The query engine is free to decide what constitutes a description. AWHEREclause may be used to select what resources are to be described.
This returns an RDF Graph.CONSTRUCT { } WHERE { }- ACONSTRUCTquery takes solutions that match theWHEREclause 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} EXISTSandNOT EXISTSfiltersMINUSclause for subtractive negationSERVICEclause 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.