SPARQL 1.1 introduces several new query features for the SPARQL query language, including the ability to perform aggregates, use subqueries, add negation, include expressions in SELECT clauses, use property paths to query relationships between resources, and support an expanded set of built-in functions and operators. Some of the major extensions are the addition of aggregation operators like COUNT and SUM, the ability to perform complex graph pattern matching with negation using EXISTS and MINUS, and the introduction of property paths to query relationships between resources through paths of arbitrary length.
3. What is SPARQL?
• SPARQL was a query language for RDF data
– http://www.w3.org/TR/rdf-sparql-query/
• And a protocol
– http://www.w3.org/TR/rdf-sparql-protocol/
• Designed around graph patterns
– Graph patterns use Turtle syntax
• SPARQL 1.1 introduces:
–
–
–
–
Query extensions (covered here)
Protocol extensions and a (RESTful) graph store protocol
Updates (covered next)
Service descriptions and federation
SPARQL 1.1 Query
#3
4. SPARQL 1.1 Query Extensions
• SPARQL 1.1 adds the following new features
(http://www.w3.org/TR/sparql11-query)
–
Aggregates
–
–
–
–
–
–
–
Subqueries
Negation
Expressions in the SELECT clause
Property Paths
Assignment
A short form for CONSTRUCT
An expanded set of functions and operators
SPARQL 1.1 Query
#4
5. Aggregates
• Aggregates allow computation of values using:
– COUNT, SUM, MIN, MAX, AVG, GROUP_CONCAT and SAMPLE
• Built around the GROUP BY operator
• For example computing popularity in a social graph:
SELECT ?person (COUNT(?someone) AS ?popularity)
WHERE {?someone foaf:knows ?person}
GROUP BY ?person
• Prune at group level (cf. FILTER) using HAVING, e.g.:
GROUP BY ?person HAVING (COUNT(?someone) > 300)
SPARQL 1.1 Query
#5
6. Subqueries
• Subqueries allow bindings from one (inner) query to be
used in another (outer) query
• For example, to compute whom the most popular
person in a social graph knows:
SELECT ?friend
WHERE {?popular foaf:knows ?friend.
{SELECT ?popular (COUNT(?someone) AS ?popularity)
WHERE {?someone foaf:knows ?popular}
GROUP BY ?popular
ORDER BY DESC (?popularity)
LIMIT 1}}
SPARQL 1.1 Query
#6
7. Negation 1/2
• SPARQL 1.0 negation was limited, applying only to
simple FILTER expressions
• SPARQL 1.1 introduces EXISTS, allowing graph
matching (without binding)
• For example, to compute email addresses of people
who know no Barrys (!):
SELECT ?email
WHERE {?sad foaf:mbox ?email .
FILTER NOT EXISTS {?sad foaf:knows ?barry.
{?barry foaf:givenName "Barry"}
UNION {?barry foaf:firstName "Barry"}}
SPARQL 1.1 Query
#7
8. Negation 2/2
• SPARQL 1.0 negation was limited, applying only to
simple FILTER expressions
• SPARQL 1.1 also introduces MINUS, allowing explicit
removal of triples (according to another match)
• For example, to compute people who know Barrys
other than Barry Norton:
SELECT DISTINCT ?person WHERE
{?person foaf:knows ?barry.
?barry foaf:givenName "Barry".
MINUS {?person foaf:knows
[foaf:mbox <mailto:barry.norton@ontotext.com>]}}
SPARQL 1.1 Query
#8
9. Expressions in SELECT (Projection) Clauses
• SPARQL 1.0 allows built-in functions, etc., to be used in
FILTER
• SPARQL 1.1 allows their use in (the projection onto)
variables in the head of the query
• For example, to glue together names:
SELECT (CONCAT(?forename, " ", ?surname) AS ?name)
WHERE {{{?person foaf:givenName ?forename}
UNION {?person foaf:firstName ?forename}}.
{{?person foaf:familyName ?surname}
UNION {?person foaf:lastName ?surname}}}
SPARQL 1.1 Query
#9
10. Property Paths 1/2
• SPARQL 1.0 builds graph patterns from triple patterns,
where resources are separated in the graph by one arc
• SPARQL 1.1 generalises on triple patterns to model
resources separated by paths of arbitrary length
• Paths are specified using the following grammar that
follows
• Example: any member of an RDF List:
?list rdf:rest*/rdf:first ?element
SPARQL 1.1 Query
#10
11. Property Paths 2/2
• Example: anyone in social graph of ?p knows a Barry:
?p {foaf:knows | ^foaf:knows}* /
(foaf:givenName | foaf:firstName) "Barry"
SPARQL 1.1 Query
#11
12. New Built-ins
• SPARQL 1.1 supports a wider set of built-ins than
SPARQL 1.0, introducing:
– Maths: ABS (absolute value), ROUND/CEIL /FLOOR(rounding),
RAND (random), isNUMERIC
– Strings: CONCAT, CONTAINS, SUBSTR, STRLEN, UCASE, LCASE
– Dates: YEAR, MONTH, DAY, HOURS, MINUTES, SECONDS,
TIMEZONE/TZ (timezone as XML or literal), NOW
– Hashes: MD5 , SHA1, SHA224, SHA256, SHA384, SHA512
– Enumeration: IN, NOT IN
– Control: IF, COALESCE
– Identifiers: ENCODE_FOR_URI, IRI/URI, BNODE (create a new
blank node, can be with parameter)
SPARQL 1.1 Query
#12
13. Short Form CONSTRUCT
• In order to query for a subgraph, without change, it is
no longer necessary to repeat the graph pattern
• Example:
CONSTRUCT WHERE
{?barry foaf:givenName "Barry";
?p ?o}
SPARQL 1.1 Query
#13