SPARQL
SPARQL Protocol And RDF Query Language
Prof. Viswanathan V
School of Computing Science and Engineering
Vellore Institute of Technology
Chennai
SPARQL - Introduction
Querying with SPARQL
RDF Triples
stores
SPARQL Result Set
Query
Viswanathan V, VIT Chennai 2
SPARQL – Introduction…..
SPARQL is the standard language to query graph data represented
as RDF triples.
Types of SPARQL queries
• SELECT - Return a table of all X, Y, etc. satisfying the following
conditions ...
• CONSTRUCT - Find all X, Y, etc. satisfying the following
conditions ... and substitute them into the following template in
order to generate (possibly new) RDF statements, creating a new
graph.
• DESCRIBE - Find all statements in the dataset that provide
information about the following resource(s) ... (identified by
name or description)
• ASK - Are there any X, Y, etc. satisfying the following conditions
...
Viswanathan V, VIT Chennai 3
Structure of a SPARQL Query
What does a basic SPARQL query look like?
SELECT clause - to identify the values to be returned
FROM clause - to identify the data sources to query
WHERE clause - the triple/graph pattern to be matched against the
triples/graphs of RDF
- a conjunction of triples:
{ ?x rdf:type ex:Person
?x ex:name ?name }
PREFIX - to declare the schema used in the query
Viswanathan V, VIT Chennai 4
Structure of a SPARQL Query …..
Definition of Prefix
PREFIX ex: <http://www.vit.ac.in#>
SELECT ?name Variables,
ie..What to search for
Type of
query
WHERE
{ ?x ex:legalname ?name}
RDF triple
Patterns,
ie. The conditions that
have to be met
Viswanathan V, VIT Chennai 5
Sample dataset represented in Turtle
@prefix ex: <http://www.vit.ac.in#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
ex:raj a ex:Person ;
ex:dob "1980-05-27"^^xsd:date ; ex:knows "Java" ;
ex:name "Raj Kumar" ; ex:school ex:scope .
ex:ravi a ex:Person ;
ex:dob "1970-06-17"^^xsd:date ; ex:knows "Java" ;
ex:name "Ravichandaran" ; ex:school ex:smec .
ex:mary a ex:Person ;
ex:dob "2005-08-17"^^xsd:date ; ex:knows "Java" , "Python" ;
ex:name "Mary Joseph" ; ex:school ex:scope .
ex:jayaram a ex:Person ; ex:knows
"Java" , "Python" ; ex:name "Jayaram" ; ex:school ex:smec .
ex:radha a ex:Adult ;
ex:knows "Python" ; ex:name "Radha kumari" ; ex:school ex:sense .
Viswanathan V, VIT Chennai 6
SPARQL Query - Simple Example
Example : persons and their names
PREFIX ex: <http://www.vit.ac.in#>
SELECT ?person ?name
WHERE {
?person rdf:type ex:Person .
?person ex:name ?name .
Output :
}
Viswanathan V, VIT Chennai 7
SPARQL Query -Filter
FILTER - to add constraints to the graph pattern (e.g.,
numerical like x >17 )
Example : persons at least 18-year old
PREFIX ex: <http://www.vit.ac.in#>
SELECT ?name ?age
WHERE {
?person rdf:type ex:Person .
?person ex:name ?name . The BIND( expression
?person ex:dob ?d . AS ?variable ) clause
BIND(year(now()) - year(?d ) as ?age ) . can be used to assign the
FILTER( ?age > 17 ) result of an expression to a
variable
}
F I L T E R can use many operators, functions (e.g., regular
expressions)
Viswanathan V, VIT Chennai 8
SPARQL Query –Filter ….
Output :
Viswanathan V, VIT Chennai 9
SPARQL Query -Optional
How do we allow for missing or unknown information?
OPTIONAL - to make the matching of a part of the pattern
optional
Example : retrieve the name of the person and age(if available)
PREFIX ex: <http://www.vit.ac.in#>
SELECT ?name ?age
WHERE {
?person rdf:type ex:Person .
?person ex:name ?name .
OPTIONAL{ ?person ex:dob ?d .
BIND(year(now()) - year(?d ) as ?age )
}.
}
Viswanathan V, VIT Chennai 10
SPARQL Query –Optional ….
Output :
Viswanathan V, VIT Chennai 11
SPARQL Query - UNION
How do we allow for alternatives or variations in the graph?
UNION - to give alternative patterns in a query
Example : List of all adult name - explicit or implicit adults
PREFIX ex: <http://www.vit.ac.in#> Output :
SELECT ?name
WHERE {
?person ex:name ?name .
{
{ ?person rdf:type ex:Adult }
UNION
{ ?person ex:dob ?d
BIND(year(now()) - year(?d ) as ?age )
FILTER (?age > 17) }
}
}
Viswanathan V, VIT Chennai 12
SPARQL Query – Sorting and restrict the results
How do we apply a sort order to the results?
How can we restrict the number of results returned?
ORDER BY to sort
LIMIT result number
OFFSET rank of first result
Example: results 11 to 30 ordered by name
PREFIX ex: <http://www.vit.ac.in#>
SELECT ?name
WHERE {
?person rdf:type ex:Person .
?person ex:name ?name .
}
ORDER BY DESC(?name)
LIMIT 20
OFFSET 10 Viswanathan V, VIT Chennai 13
SPARQL Query - DISTINCT
How do we remove duplicate results?
PREFIX ex: <http://www.vit.ac.in#>
SELECT distinct ?person ?name
WHERE {
?person rdf:type ex:Person .
?person ex:name ?name .
}
Viswanathan V, VIT Chennai 14
SPARQL Query – group by
Example : retrieve the number of persons in each school
who knows “Java”
SELECT ?sc (count(?sc) AS ?co)
WHERE {
?person ex:knows "Java" .
?person ex:school ?sc .
} GROUP BY ?sc
Output :
Viswanathan V, VIT Chennai 15
SPARQL – Bound()
The function BOUND(variable) returns true if variable is bound to
a value. It returns false otherwise.
Consider the example:
PREFIX ex: <http://www.vit.ac.in#>
SELECT ?name
WHERE {
?person ex:name ?name .
?person ex:knows ?x
FILTER ( ?x != "Java" )
}
Does this find persons who do not know "java" ?
Answer: NO! - It will return persons who know something else !
Viswanathan V, VIT Chennai 16
SPARQL – Bound() ….
PREFIX ex: <http://www.vit.ac.in#>
SELECT ?name
WHERE {
?person ex:name ?name .
?person ex:knows ?x
FILTER ( ?x != "Java" )
}
ex:raj ex:knows "Java"
ex:raj ex:knows "C++“
raj is a answer...
Viswanathan V, VIT Chennai 17
SPARQL – Bound() ….
Persons who are not known to know "java" ... negation of an option
PREFIX ex: <http://inria.fr/schema#>
SELECT ?name
WHERE {
?person ex:name ?name . Output :
OPTIONAL { ?person ex:knows ?x
FILTER ( ?x = "Java" ) }
FILTER ( ! bound(?x) )
}
More details about SPARQL/Expressions and Functions
https://en.wikibooks.org/wiki/SPARQL/Expressions_and_Functions
Viswanathan V, VIT Chennai 18
SPARQL - ASK Query
Test whether the graph contains some data of interest
ASK - to check just if there is at least one answer ; result is
"true" or "false“
Example: Is there a person born after the year 2000 ?
PREFIX ex: <http://www.vit.ac.in#>
ASK
{
?person ex:dob ?d Output :
FILTER (year(?d) > 2010) False
}
Viswanathan V, VIT Chennai 19
SPARQL – Construct Query
Create a custom RDF graph based on query criteria can be used
to transform RDF data
CONSTRUCT - return a specific RDF graph for each result
Example : return instances of adults for persons older than 17
PREFIX ex: <http://www.vit.ac.in#>
CONSTRUCT Output :
{ @prefix ex: <http://www.vit.ac.in#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
?person rdf:type ex:Adult @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
} @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
WHERE ex:ravi a ex:Adult .
{ ex:raj a ex:Adult .
?person ex:dob ?d
BIND(year(now()) - year(?d ) as ?age )
FILTER (?age > 17)
}
Viswanathan V, VIT Chennai 20
SPARQL – DESCRIBE
Generate an RDF description of a resource(s)
Example : This query returns an RDF graph that describes all the
persons who knows Python. OUTPUT :
@prefix ex: <http://www.vit.ac.in#> .
DESCRIBE ?x @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
WHERE @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
{ ex:radha a ex:Adult ;
ex:knows "Python" ;
?x ex:knows "Python" ex:name "Radha kumari" ;
ex:school ex:sense .
}
ex:mary a ex:Person ;
ex:dob "2005-08-17"^^xsd:date ;
ex:knows "Python" , "Java" ;
ex:name "Mary Joseph" ;
ex:school ex:scope .
ex:jayaram a ex:Person ;
ex:knows "Python" , "Java" ;
ex:name "Jayaram" ;
ex:school ex:smec .
Viswanathan V, VIT Chennai 21
Viswanathan V, VIT Chennai 22