2. Query Languages: SQL
(SQL Query Language)
A language for querying collections of tuples:
SELECT SALARY, HIRE_DATE
FROM EMPS
WHERE EMP_ID = 13954
EMP_ID NAME HIRE_DATE SALARY
13954 Joe 2000-04-14 48000
10335 Mary 1998-11-23 52000
… … … …
04182 Bob 2005-02-10 21750
2
3. Query Languages: XQuery
(XML Query)
A language for querying trees of XDM nodes:
for $e in document(my_employees.xml)
where $emp/emp/@emp-id = 13954
return $emp/emp/salary
< e m p lo ye e s>
< e m p> < e m p> <e m p> <e m p>
e m p-id = 1 39 54
... ... ...
< n a m e> < h ire -d a te> < s a la ry>
Joe 2 0 00 -04 -14 4 80 00
3
4. Why an RDF Query Language?
Different XML Representations
XML at a lower level of abstraction than RDF
There are various ways of syntactically representing an
RDF statement in XML
Thus we would require several XQuery queries, e.g.
//uni:lecturer/uni:title if uni:title element
//uni:lecturer/@uni:title if uni:title attribute
Both XML representations equivalent!
4
5. Two basic families of SQL-like langages for
RDF(S)
RDQL
Implementations: Jena, Sesame, RDFStore, ...
RQL
Implementations: RQL, SPARQL, ...
5
6. Introduction to RDQL
RDF Data Query Language
JDBC/ODBC friendly
Simple:
SELECT
some information
FROM
somewhere
WHERE
this match
AND
these constraints
USING
these vocabularies
6
8. Example
q1 contains a query:
SELECT ?x
WHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, "John Smith")
For executing q1with a model m1.rdf:
java jena.rdfquery --data m1.rdf --query q1
The outcome is:
x
=============================
<http://somewhere/JohnSmith/>
8
9. Example
Return all the resources that have property FN and the
associated values:
SELECT ?x, ?fname
WHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, ?fname)
The outcome is:
x | fname
================================================
<http://somewhere/JohnSmith/> | "John Smith"
<http://somewhere/SarahJones/> | "Sarah Jones"
<http://somewhere/MattJones/> | "Matt Jones"
9
10. Example
Return the first name of Jones:
SELECT ?givenName
WHERE (?y, <http://www.w3.org/2001/vcard-rdf/3.0#Family>, "Jones"),
(?y, <http://www.w3.org/2001/vcard-rdf/3.0#Given>, ?givenName)
The outcome is:
givenName
=========
"Matthew"
"Sarah"
10
11. URI Prefixes : USING
RDQL has a syntactic convenience that allows prefix strings
to be defined in the USING clause :
SELECT ?x
WHERE (?x, vCard:FN, "John Smith")
USING vCard FOR <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?givenName
WHERE (?y, vCard:Family, "Smith"),
(?y, vCard:Given, ?givenName)
USING vCard FOR <http://www.w3.org/2001/vcard-rdf/3.0#>
11
12. Filters
RDQL has a syntactic convenience that allows prefix strings
to be defined in the USING clause :
SELECT ?resource
WHERE (?resource, info:age, ?age)
AND ?age >= 24
USING info FOR <http://somewhere/peopleInfo#>
12
13. Limitations
Does not take into account semantics of RDF(S)
For example:
ex:human rdfs:subClassOf ex:animal
ex:student rdfs:subClassOf ex:human
ex:john rdf:type ex:student
Query: “ To which class does the resource John belong?”
Expected answer: ex:student, ex:human, ex:animal
However, the query:
SELECT ?x
WHERE (<http://example.org/#john>, rdf:type, ?x)
USING rdf FOR <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
Yields only:
<http://example.org/#student>
Solution: Inference Engines
13
14. Why an RDF Query Language?
Different XML Representations
XML at a lower level of abstraction than RDF
There are various ways of syntactically representing an
RDF statement in XML
Thus we would require several XPath queries, e.g.
//uni:lecturer/uni:title if uni:title element
//uni:lecturer/@uni:title if uni:title attribute
Both XML representations equivalent!
14
15. SPARQL
SPARQL is RDF Query Language
It provides facilities to:
extract information in the form of URIs, blank nodes, plain and typed
literals.
extract RDF subgraphs.
construct new RDF graphs based on information in the queried graphs
16. SPARQL Basic Queries
SPARQL is based on matching graph patterns
The simplest graph pattern is the triple pattern :
- like an RDF triple, but with the possibility of a variable instead of an RDF
term in the subject, predicate, or object positions
Combining triple patterns gives a basic graph pattern, where an exact match
to a graph is needed to fulfill a pattern
16
17. Basic graph pattern
Set of Triple Patterns
Triple Pattern – similar to an RDF Triple (subject, predicate, object), but any
component can be a query variable; literal subjects are allowed
?book dc:title ?title
Matching a triple pattern to a graph: bindings between variables and RDF
Terms
Matching of Basic Graph Patterns
A Pattern Solution of Graph Pattern GP on graph G is any substitution S
such that S(GP) is a subgraph of G.
SELECT ?x ?v
WHERE { ?x ?x ?v }
rdf:type rdf:type rdf:Property
x v
rdf:type rdf:Property
18. Examples
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?c
WHERE
{
?c rdf:type rdfs:Class .
}
Retrieves all triple patterns, where:
- the property is rdf:type
- the object is rdfs:Class
Which means that it retrieves all classes
18
19. Examples (2)
Get all instances of a particular class (e.g. course) :
PREFIX uni: <http://www.mydomain.org/uni-ns#>
SELECT ?i
WHERE
{
?i rdf:type uni:course .
}
19
20. Multiple Matches
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> .
PREFIX foaf: <http://xmlns.com/foaf/0.1/> Query
SELECT ?name ?mbox Group Graph Pattern
WHERE (set of graph patterns)
{ ?x foaf:name ?name . also!
?x foaf:mbox ?mbox }
Query Result name mbox
"Johnny Lee <mailto:jlow@example.com>
Outlaw"
"Peter Goodguy" <mailto:peter@example.org>
21. Using select-from-where
As in SQL, SPARQL queries have a SELECT-FROM-WHERE structure:
SELECT specifies the projection: the number and order of retrieved data
FROM is used to specify the source being queried (optional)
WHERE imposes constraints on possible solutions in the form of graph
pattern templates and boolean constraints
Retrieve all phone numbers of staff members:
SELECT ?x ?y
WHERE
{ ?x uni:phone ?y .}
Here ?x and ?y are variables, and ?x uni:phone ?y represents a resource-
property-value triple pattern
21
22. Implicit Join
Retrieve all lecturers and their phone numbers:
SELECT ?x ?y
WHERE
{ ?x rdf:type uni:Lecturer ;
uni:phone ?y . }
Implicit join: We restrict the second pattern only to those triples, the resource
of which is in the variable ?x
Notice the syntax shortcut : a semicolon indicates that the following
triple shares its subject with the previous one
22
23. Implicit join (2)
The previous query is equivalent to writing:
SELECT ?x ?y
WHERE
{
?x rdf:type uni:Lecturer .
?x uni:phone ?y .
}
23
24. Explicit Join
Retrieve the name of all courses taught by the lecturer with ID 949352
SELECT ?n
WHERE
{
?x rdf:type uni:Course ;
uni:isTaughtBy 949352 .
?c uni:name ?n .
FILTER (?c = ?x) .
}
24
25. Basic Graph Pattern - Blank Nodes
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
Data
_:a foaf:name "Alice" .
_:b foaf:name "Bob" .
PREFIX foaf: <http://xmlns.com/foaf/0.1/> Query
SELECT ?x ?name
WHERE { ?x foaf:name ?name }
x name
Query Result _:c “Alice“
_:d “Bob”
27. Value Constraints
@prefix dc: <http://purl.org/dc/elements/1.1/> .
Data @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 .
PREFIX dc: <http://purl.org/dc/elements/1.1/> Query
PREFIX ns: <http://example.org/ns#>
SELECT ?title ?price
WHERE { ?x ns:price ?price .
FILTER ?price < 30 .
?x dc:title ?title . }
title price
Query Result
"The Semantic Web" 23
28. Regular expressions can be used
• “Find the name and email addresses of authors of a paper
about SPARQL”
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX ldap: <http://ldap.hp.com/people#>
PREFIX foaf:
SELECT ?name ?name2
{
WHERE ?doc dc:title ?title .
FILTER regex(?title, “SPARQL”) .
?doc dc:creator ?reseacher .
?researcher ldap:email ?email .
?researcher ldap:name ?name
}
28
29. Optional graph patterns
<uni:lecturer rdf:about=“949352”>
<uni:name>Grigoris Antoniou</uni:name>
</uni:lecturer>
<uni:professor rdf:about=“94318”>
<uni:name>David Billington</uni:name>
<uni:email>david@work.example.org</uni:email>
</uni:professor>
For one lecturer it only lists the name
For the other it also lists the email address
29
30. Optional graph patterns (2)
All lecturers and their email addresses:
SELECT ?name ?email
WHERE
{ ?x rdf:type uni:Lecturer ;
uni:name ?name ;
uni:email ?email .
}
The result of the previous query would be:
?name ?email
David Billington david@work.example.org
Grigoris Antoniou is listed as a lecturer, but he has no e-mail address
30
31. Optional graph patterns(3)
As a solution we can adapt the query to use an optional pattern:
SELECT ?name ?email
WHERE
{ ?x rdf:type uni:Lecturer ;
uni:name ?name .
OPTIONAL { x? uni:email ?email }
}
The meaning is roughly “give us the names of lecturers, and if known also their e-
mail address”
The result looks like this:
?name ?email
Grigoris Antoniou
David Billington david@work.example.org
31