Introduction To SPARQL
Introduction To SPARQL
An Introduction to SPARQL
V. CHRISTOPHIDES
Department of Computer Science
University of Crete
ICS - FORTH, Heraklion, Crete 1
Introduction
SPARQL is the W3C candidate recommendation graph-matching query
language for RDF
The acronym stands for SPARQL Protocol and RDF Query Language
A SPARQL query consists of three parts:
Pattern matching: optional, union, nesting, filtering.
Solution modifiers: projection, distinct, order, limit, offset.
Output part: construction of new triples, -
In addition to the core language, W3C has also defined:
The SPARQL Protocol for RDF specification: it defines the remote
protocol for issuing SPARQL queries and receiving the results
The SPARQL Query Results XML Format specification: it defines an
XML document format for representing the results of SPARQL queries
The SPARQL Federated Query: it extends SPARQL for executing
queries distributed over different SPARQL endpoints
The SPARQL Service Description: it is a method for discovering and a
vocabulary for describing SPARQL services 2
1
1
ICS-FORTH & Univ. of Crete Spring 2013
SPARQL in a Nutshell
2
2
ICS-FORTH & Univ. of Crete Spring 2013
The ASK query form can be used to test whether or not a graph pattern
has a solution
No information is returned about the possible query solutions, just
whether or not a solution exists
3
3
ICS-FORTH & Univ. of Crete Spring 2013
Example:
?x foaf:name ?name . ?x foaf:mbox ?mbox
4
4
ICS-FORTH & Univ. of Crete Spring 2013
Simple examples:
{ ?x foaf:name ?name . ?x foaf:mbox ?mbox }
GGP Example
Data:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Johnny Lee Outlaw" .
_:a foaf:mbox <mailto:jlow@example.com> .
_:b foaf:name "Peter Goodguy" .
_:b foaf:mbox <mailto:peter@example.org> .
_:c foaf:mbox <mailto:carol@example.org> .
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE { ?x foaf:name ?name . ?x foaf:mbox ?mbox }
Result:
?name ?mbox
“Peter Goodguy" <mailto:peter@example.org>
5
5
ICS-FORTH & Univ. of Crete Spring 2013
11
OGP Example
Data:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntaxns#>.
_:a rdf:type foaf:Person .
_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@example.com> .
_:a foaf:mbox <mailto:alice@work.example> .
_:b rdf:type foaf:Person .
_:b foaf:name "Bob" .
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE { ?x foaf:name ?name .
OPTIONAL { ?x foaf:mbox ?mbox }
} ?name ?mbox
Result: "Alice" <mailto:alice@example.com>
"Alice" <mailto:alice@work.example>
12
"Bob"
6
6
ICS-FORTH & Univ. of Crete Spring 2013
13
AGP Example
Data:
@prefix dc10: <http://purl.org/dc/elements/1.0/> .
@prefix dc11: <http://purl.org/dc/elements/1.1/> .
_:a dc10:title "SPARQL Query Language Tutorial" .
_:a dc10:creator "Alice" .
_:b dc11:title "SPARQL Protocol Tutorial" .
_:b dc11:creator "Bob" .
_:c dc10:title "SPARQL" .
_:c dc11:title "SPARQL (updated)" .
Query:
PREFIX dc10: <http://purl.org/dc/elements/1.0/>
PREFIX dc11: <http://purl.org/dc/elements/1.1/>
SELECT ?title ?title
WHERE {{?book dc10:title ?title}
“SPARQL Protocol Tutorial"
UNION
{?book dc11:title ?title}"SPARQL“
} "SPARQL (updated)“
Result: "SPARQL Query Language Tutorial"
14
7
7
ICS-FORTH & Univ. of Crete Spring 2013
15
8
8
ICS-FORTH & Univ. of Crete Spring 2013
Filter Expressions
The FILTER construct restricts variable bindings to those for which the
filter expression evaluates to TRUE by considering
equality = among variables and RDF terms
unary predicate bound
Boolean combinations (∧, ∨, ¬)
Group graph patterns are used to restrict the scope of FILTER conditions
A FILTER condition is a restriction on solutions over the whole group in
which the filter appears
P1 . P2 . FILTER (... boolean expression...)
We can have multiple FILTERs in a group graph pattern
They are equivalent to a single filter with conjoined filter conditions
FILTERs can be very complex Boolean conditions !
The regular expression language used by regex is defined in XQuery
1.0 and XPath 2.0 17
Constraints on Variables
Data:
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix : <http://example.org/book/> .
@prefix ns: <http://example.org/ns#> .
:book1 dc:title "SPARQL Tutorial" .
:book1 ns:price 42 .
:book2 dc:title "The Semantic Web" .
:book2 ns:price 23 .
Query:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX ns: <http://example.org/ns#>
SELECT ?title ?price
WHERE { ?x ns:price ?price .
FILTER (?price < 30.5)
?x dc:title ?title . } ?title ?price
Result: “The Semantic Web" 23 18
9
9
ICS-FORTH & Univ. of Crete Spring 2013
Data:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:b foaf:name "Bob" .
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x ?name
WHERE { ?x foaf:name ?name . }
Result:
?x ?name
_:c "Alice"
_:d "Bob"
19
Data:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:b foaf:name "Bob" .
_:a foaf:knows _:b .
_:b foaf:knows _:a .
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x ?name1 ?y ?name2
WHERE { ?x foaf:name ?name1 . ?y foaf:name ?name2 .
?x foaf:knows ?y }
Result: ?x ?name ?y ?name
_:c "Alice" _:d "Bob"
_:d "Bob" _:c "Alice" 20
10
10
ICS-FORTH & Univ. of Crete Spring 2013
The same blank node is not allowed to appear in two BGPs of the same
query
Data:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:b foaf:name "Bob" .
_:a foaf:knows _:b .
_:b foaf:knows _:a .
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE { _:z foaf:name ?name . }
Result: ?name
"Alice"
"Bob" 22
11
11
ICS-FORTH & Univ. of Crete Spring 2013
Data:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:b foaf:name "Bob" .
_:a foaf:knows _:b .
_:b foaf:knows _:a .
Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name1 ?name2
WHERE { _:z foaf:name ?name1 . _:v foaf:name ?name2 .
_:z foaf:knows _:v } ?name1 ?name2
Result: "Alice" "Bob"
"Bob" "Alice" 23
12
12
ICS-FORTH & Univ. of Crete Spring 2013
SPARQL in a Nutshell
PREFIX rdf: <http://www.w3.org/1999/02/22-
<http://www.w3.org/1999/02/22-rdf-
rdf-syntax-
syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-
Shortcuts <http://www.w3.org/2000/01/rdf
for URIs -schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX oo: <http://purl.org/openorg/> PREFIX DECLARATIONS
SELECT
Which?name ?expertise
results do you want? RESULT CLAUSE
Where
FROM should we look?
<http://data.uoc.gr/> DATASET CLAUSE
WHERE {
?person foaf:name ?name ; foaf:familyName ?surname .
?person rdf:type foaf:Person .
?person foaf:title ?title . FILTER regex(?title, "^Prof")
What are
OPTIONAL { you looking for?
?person oo:availableToCommentOn ?expertiseURI .
?expertiseURI rdfs:label ?expertise
}
} QUERY CLAUSE
ORDER
HowBYshould
?surname
results be ordered/split? SOLUTION MODIFIERS
13
13
ICS-FORTH & Univ. of Crete Spring 2013
Prefix Declarations
PREFIX rdf: <http://www.w3.org/1999/02/22-
<http://www.w3.org/1999/02/22-rdf-
rdf-syntax-
syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-
<http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX oo: <http://purl.org/openorg/> PREFIX DECLARATIONS
foaf:Person ⇔ <http://xmlns.com/foaf/0.1/Person>
Use http://prefix.cc/ …
27
14
14
ICS-FORTH & Univ. of Crete Spring 2013
Solution Modifiers
ORDER BY ?surname SOLUTION MODIFIERS
OFFSET
Return results 20‒30
ORDER BY ?surname LIMIT 10 OFFSET 20 SOLUTION MODIFIERS
The OFFSET clause causes the solutions generated to start after the
specified number of solutions
An OFFSET of zero has no effect
30
15
15
ICS-FORTH & Univ. of Crete Spring 2013
Removing Duplicates
By default, SPARQL query results may contain duplicates (so the result
of a SPARQL query is a bag not a set)
31
16
16
ICS-FORTH & Univ. of Crete Spring 2013
33
17
17
ICS-FORTH & Univ. of Crete Spring 2013
Ω1
?x ?y Ω1 U Ω2
?z is unbound in µ1
a b µ1 ?x ?y ?z
?x ?z a b - µ1
Ω2
c d µ2 c - d µ2 36
18
18
ICS-FORTH & Univ. of Crete Spring 2013
(optional)
(a, b, f): Ω1 Ω2
Ω1 Ω2 (c, d, -): Ω1 Ω2
Ω1 ?x ?y
Ω2 ?x ?y ?z
a b µ1 ?y ?z a b f µ4 = µ1 U µ3
c d µ2 b f µ3 c d - µ2 38
19
19
ICS-FORTH & Univ. of Crete Spring 2013
39
Negation in SPARQL
SPARQL 1.0 does not offer an explicit algebraic difference operator but
this operator is implicit in the definition of the OPTIONAL operator
SPARQL 1.1. offers more non-monotonic operators
40
20
20
ICS-FORTH & Univ. of Crete Spring 2013
Data:
@prefix ns: <http://example.org/ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
_:a ns:p "42"^^xsd:integer .
Query:
PREFIX ns: <http://example.org/ns#>
SELECT ?v
WHERE {
?v ns:p ?y . FILTER (?y != 42)
}
Result:
?V
41
42
21
21
ICS-FORTH & Univ. of Crete Spring 2013
Example
Data:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix ex: <http://example.org/schema/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
_:a foaf:givenName "Alice“ .
_:b foaf:givenName "Bob" .
_:b ex:age “30"^^xsd:integer .
_:m foaf:givenName “Mike" .
_:m ex:age “65"^^xsd:integer .
Query: Find the names of people with name and age
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX ex: <http://example.org/schema/>
SELECT ?name
WHERE { ?x foaf:givenName ?name . ?x ex:age ?age }
Result: ?name
“Bob"
“Mike" 43
Result: ? name
“Alice"
44
22
22
ICS-FORTH & Univ. of Crete Spring 2013
“Bob"
45
Note that the OPTIONAL pattern in the previous query does not generate
bindings in the following two cases:
There is no ex:age property for ?x (e.g., when ?x=_a)
There is an ex:age property for ?x but its value is less than 60 (e.g.,
when ?x=_b)
These two cases are then selected for output by the FILTER condition
that uses !bound
46
23
23
Spring 2013
Closed World Assumption (CWA) and Negation as
ICS-FORTH & Univ. of Crete
Failure (NF)
We saw that it is possible to simulate a non-monotonic construct like
negation as failure through SPARQL language constructs
However, SPARQL makes no assumption to interpret statements in an
RDF graph using negation as failure or some other non-monotonic
assumption (e.g., Closed World Assumption)
SPARQL, but also RDF(S) make the Open World Assumption i.e.
things that are not known to be true or false are assumed to be possible
Monotonicity: Let KB be a set of FOL formulas and φ and θ two
arbitrary FOL formulas
If KB entails φ then KB union { θ } entails φ as well
CWA and NF result in non-monotonicity
If A is a ground atomic formula in FOL, then the CWA says:
If KB does not entail A, then assume not A to be entailed
If A is a ground atomic formula in FOL, then negation as failure says:
If you cannot prove A from the KB, then assume not A has been
proven
47
24
24
ICS-FORTH & Univ. of Crete Spring 2013
49
Aggregates
50
25
25
ICS-FORTH & Univ. of Crete Spring 2013
Example: Aggregates
Data:
@prefix : <http://books.example/> .
:org1 :affiliates :auth1, :auth2 .
:auth1 :writesBook :book1, :book2 .
:book1 :price 9 .
:book2 :price 5 .
:auth2 :writesBook :book3 .
:book3 :price 7 .
:org2 :affiliates :auth3 .
:auth3 :writesBook :book4 .
:book4 :price 7 .
51
Example (cont’d)
Query: Find the total price of books written by authors affiliated with
some organization: output organization id and total price only if the total
price is greater than 10
PREFIX : <http://books.example/>
SELECT (?org SUM(?lprice) AS ?totalPrice)
WHERE { ?org :affiliates ?auth .
?auth :writesBook ?book .
?book :price ?lprice . }
GROUP BY ?org
HAVING (SUM(?lprice) > 10)
26
26
ICS-FORTH & Univ. of Crete Spring 2013
Subqueries
Subqueries are a way to embed SPARQL queries inside other queries to
allow the expression of requests that are not possible otherwise
Subqueries are evaluated first and then the outer query is applied to
their results
Only variables projected out of the subquery (i.e., appearing in its
SELECT clause) will be visible to the outer query
Consider graph patterns P1 , P2 and P3 :
P1 .
{SELECT
WHERE {P2}
}
P3 .
Join the results of the subquery with the results of solving P1 and P3
Subqueries are useful when combining limits and aggregates with other
constructs 53
Example: Subqueries
Data:
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://sales.com/> .
:sale1 :company :c1; :amount 7500^^xsd:integer; :year "2011".
:sale2 :company :c1; :amount 17000^^xsd:integer;:year "2011".
:sale3 :company :c1; :amount 5500^^xsd:integer; :year "2012".
:sale4 :company :c1; :amount 7000^^xsd:integer; :year "2012".
:sale5 :company :c2; :amount 3000^^xsd:integer; :year "2011".
:sale6 :company :c2; :amount 4000^^xsd:integer; :year "2011".
:sale7 :company :c2; :amount 5000^^xsd:integer; :year "2012".
:sale8 :company :c2; :amount 6000^^xsd:integer; :year "2012".
54
27
27
ICS-FORTH & Univ. of Crete Spring 2013
Example (cont’d)
Query: Find companies that increased their sales from 2011 to 2012 and
the amount of increase
PREFIX : <http://sales.com/>
SELECT ?c ((?total2012 - ?total2011) AS ?increase)
WHERE {
{ SELECT ?c (SUM(?m) AS ?total2012)
WHERE { ?s a :Sale ; :company ?c ;
:amount ?m ; :year: "2012" . }
GROUP BY ?c } .
{ SELECT ?c (SUM(?m) AS ?total2011)
WHERE { ?s a :Sale ; :company ?c ;
:amount ?m ; :year: "2011" . }
GROUP BY ?c } .
FILTER (?total2012 > ?total2011)
}
?c ?increase
Result:
<http://sales.com/c2> "4000"^^<http://www.w3.or
g/2001/XMLSchema#integer> 55
Negation
In SPARQL 1.1 we have two ways to express negation:
The algebraic operator MINUS (is the same as the difference
operation of the SPARQL algebraic semantics) used for removing
matches based on the evaluation of two query patterns
P1 MINUS P2 returns all the mappings in P1 that are incompatible
with all mappings in P2 or they have disjoin domains (different vars)
The operator NOT EXISTS in FILTER expressions used for testing
whether a pattern exists in the data, given the bindings already
determined by the query pattern
P1 NOT EXISTS P2 returns true if the pattern P2 do not matches the
data after substituting all mappings in P1 (shared vars)
Proposition: (P1 MINUS P2) is equivalent to:
P1 OPT (P2 AND (?X1, ?X2, ?X3)) FILTER ¬bound(?X1),
where ?X1, ?X2, ?X3 are mentioned neither in P1 nor in P2
Unlike MINUS, NOT EXISTS should only be in a FILTER expression
56
28
28
ICS-FORTH & Univ. of Crete Spring 2013
Example: MINUS
Data:
@prefix : <http://example/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
:alice foaf:givenName "Alice" ; foaf:familyName "Smith" .
:bob foaf:givenName "Bob" ; foaf:familyName "Jones" .
:carol foaf:givenName "Carol" ; foaf:familyName "Smith" .
Query: Find all persons that do not have given name “Bob”
PREFIX : <http://example/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?s
WHERE { ?s ?p ?o .
MINUS { ?s foaf:givenName "Bob" . } }
Result: ?s
<http://example/carol>
<http://example/alice> 57
29
29
ICS-FORTH & Univ. of Crete Spring 2013
@prefix : <http://example/> .
:a :b :c .
SELECT *
WHERE { ?s ?p ?o FILTER NOT EXISTS { ?x ?y ?z } }
evaluates to an empty result set because { ?x ?y ?z } matches given
any ?s ?p ?o, so NOT EXISTS { ?x ?y ?z } eliminates any solution
SELECT *
WHERE { ?s ?p ?o MINUS { ?x ?y ?z } }
since there is no shared variable between the first part (?s ?p ?o) and
the second (?x ?y ?z) no bindings are eliminated, so the result is
s p o
<http://example/a> <http://example/b> <http://example/c>
See more examples
http://www.openlinksw.com/uda/wiki/main/Main/VirtTipsAndTricksGuideSPARQLNOTEXISTS
60
30
30
ICS-FORTH & Univ. of Crete Spring 2013
31
31
ICS-FORTH & Univ. of Crete Spring 2013
Property Paths
SPARQL 1.1 allows us to specify property paths in the place of a
predicate in a triple pattern
Property paths use regular expressions to enable us to write
sophisticated queries that traverse an RDF graph
Property paths allow for the more concise expression of some
queries plus the ability to refer to paths of arbitrary length
63
Examples
The / path operator denotes sequence
Query: Find the name of any people that Alice knows
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x ?name
WHERE { ?x foaf:mbox <mailto:alice@example> .
?x foaf:knows/foaf:name ?name . }
Query: Find the name of people that Alice knows that are 2
"foaf:knows" links away
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x ?name
WHERE { ?x foaf:mbox <mailto:alice@example> .
?x foaf:knows/foaf:knows/foaf:name ?name . }
64
32
32
ICS-FORTH & Univ. of Crete Spring 2013
Example (cont’d)
65
Examples
The + operator denotes one or more occurrences of foaf:knows
Query: Find all the people :x connects to via the foaf:knows
relationship (using a path with an arbitrary length)
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX : <http://example/>
SELECT ?person
WHERE { :x foaf:knows+ ?person }
The * operator denotes zero or more occurrences of rdfs:subClassOf
Query: Find all types, including supertypes ,of each resource in the
dataset
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
SELECT ?x ?type
WHERE { ?x rdf:type/rdfs:subClassOf* ?type } 66
33
33
ICS-FORTH & Univ. of Crete Spring 2013
SPARQL Endpoints
SPARQL query processing service
Supports the SPARQL protocol
Issuing a SPARQL query is an HTTP GET request with parameter query
URL-encoded string
Example: with the SPARQL query
GET /sparql?query=PREFIX+rd... HTTP/1.1
Host: dbpedia.org
User-agent: my-sparql-client/0.1
Several Linked Data sets exposed via SPARQL endpoint: Send your
query, receive the result!
DBpedia http://dbpedia.org/sparql
Musicbrainzhttp://dbtune.org/musicbrainz/sparql
World Factbook http://www4.wiwiss.fu-berlin.de/factbook/snorql/
LinkedMDB http://data.linkedmdb.org/sparql
DBLP http://www4.wiwiss.fu-berlin.de/dblp/snorql/ 67
34
34
ICS-FORTH & Univ. of Crete Spring 2013
69
70
35
35
ICS-FORTH & Univ. of Crete Spring 2013
Readings
The SPARQL syntax & semantics can be found in the W3C specification
www.w3.org/TR/sparql11-query (1.1) www.w3.org/TR/rdf-sparql-query (1.0)
For formal semantics and expressive power of SPARQL read:
J.Perez, M. Arenas and C. Gutierrez. Semantics and Complexity of SPARQL.
ACM Transactions on Database Systems, 34(3), Article 16 (45 pages), 2009
M. Arenas and J. Perez. Querying Semantic Web Data with SPARQL: State of
the Art and Research Perspectives. In Proc. of PODS, Athens, Greece, pages
305-316, 2011
A. Mallea, M. Arenas, A. Hogan and A. Polleres. On Blank Nodes. Proc. of
ISWC 2011
Check out the SPARQL 1.1 Query Results Formats
JSON Format http://www.w3.org/TR/sparql11-results-json/
CSV and TSV Formats http://www.w3.org/TR/sparql11-results-csv-tsv/
XML Format http://www.w3.org/TR/rdf-sparql-XMLres/
See www.w3.org/TR/2012/WD-sparql11-query-20120724/#propertypaths
71
for the exact property path operators and syntax
Acknowledgements
Manolis Koubarakis “An Introduction to SPARQL: Part I and II” lecture
in course “Knowledge Technologies” http://cgi.di.uoa.gr/~pms509/
Dieter Fensel, Federico Facca and Ioan Toma “Storage and Querying”
lecture in course “Semantic Web” http://teaching-
wiki.sti2.at/index.php/Semantic_Web
Andreas Harth, Aidan Hogan, Spyros Kotoulas, Jacopo Urbani “Session
2a: SPARQL/Scalable RDF Indexing” Tutorial Series on Scalable
Integration and Processing of Linked Data http://sild.cs.vu.nl/
72
36
36