03: SPARQL Flashcards
Main Advantages of RDF
- Is a standard model for data interchange on the Web
- Extends the linking structure of the Web to use URIs to name the relationship between things as well as the two ends of the link (e.g. a triplet)
- Facilitates data merging even if the underlying schemas differ
- Specifically supports the evolution of schemas over time without requiring all the data consumers to be changed
- Allows structured and semi-structured data to be mixed, exposed, and shared across different applications
- Is based on an extremely easy concept called “graph view” to comprehend and visualize it
Difference Between XPATH and SPARQL
- XPATH and SPARQL are for searching RDF files
- XPATH works for all XML files, including RDF
- SPARQL cannot search any XML
How does a system process a SPARQL query?
- SPARQL query uses triplet patterns, and each subject, predicate, and object can be a variable
- System finds appropriate values for these variables by matching patterns given to the triplets of the corresponding dataset
- System combines simple patterns into more complex ones
Difference Between SQL and SPARQL Queries
- SQL queries are highly related on how tables have been organized
- SPARQL
- queries focus on what users like to know
- can work on integrated queries
- can concurrently access to several endpoints (data stores)
- is based on HTTP-based transport protocol
Write the following query in SPARQL:
Get all instances of a particular class (e.g. course)

SELECT-FROM-WHERE
- Like SQL SELECT-FROM-WHERE query
- SELECT specifies the project: number and order of retrieved data
- FROM used to specify the source being queried (optional)
- WHERE imposes constraints on possible solutions in the form of graph pattern templates and boolean constraints
Write the following query in SPARQL:
Retrieve all phone numbers of staff members
(SELECT-FROM-WHERE)
SELECT ?x ?y
WHERE
{?x uni:phone ?y .}
(where ?x and ?y are variables, and ?x uni:phone ?y represents a resource-pattern-value triple pattern
Implicit Join
Restricting the second pattern only to those triples, the resource of which is the variable ?x
i.e. Retrieve all lecturers and their phone numbers
SELECT ?x ?y
WHERE {
?x rdf:type uni:Lecturer .
?x uni:phone ?y .
}
Syntax Shortcut in SPARQL
;
A semicolon indicates that the triplet shares its subject with the previous one
i.e. Retrieve all lecturers and their phone numbers
SELECT ?x ?y
WHERE {
?x rdf:type uni:Lecturer ;
uni:phone ?y .
}
is the same as
SELECT ?x ?y
WHERE {
?x rdf:type uni:Lecturer .
?x uni:phone ?y .
}
Write the following query in SPARQL:
Retrieve all lecturers and their phone numbers
(IMPLICIT JOIN)
SELECT ?x ?y
WHERE
{ ?x rdf:type uni:Lecturer ;
uni:phone ?y .}
Write the following query in SPARQL:
Retrieve the name of all courses taught by the lecturer with ID 949352
(EXPLICIT JOIN)
SELECT ?n
WHERE
{ ?x rdf:type uni:Course;
uni:isTaughtBy :949352
?x uni:name ?n .}
Write the following query in SPARQL:
Retrieve the names of lecturers, and if known, also their email address
(OPTIONAL PATTERNS)

SELECT ?name ?email
WHERE
{
?x rdf:type uni:Lecturer ;
uni:name ?name.
OPTIONAL { x? uni:email ?email }
}

Write the following query in SPARQL:
Retrieve the names and emails of every person in the dataset

Write the following query in SPARQL:
Write all the capitals of the countries in Africa

What are the main issues which help developers lower their development cost with respect to using RDF and SPARQL?
- The explicitness of queries
- The techniques used for the fast execution of queries employed by SPARQL
- The flexibility of RDF
- The capability of merging results from multiple data sources in SPARQL
What are the results of these two queries based on the data set with 3 RDF literals?
SELECT ?v WHERE { ?v ?p “cat”@en. }
and
SELECT ?v WHERE { ?v ?p 42 }

v
http://example.org/ns#x
and
v
http://example.org/ns#y
(**but with pointy brackets on each but brainscape sucks and won’t let me make tags)
In an RDF dataset, what does “_:” represent?
blank nodes
Based on the RDF data set, what is the result of the following query?

PREFIX foaf: http://xmlns.com/foaf/0.1
SELECT ?name ?mbox WHERE
{
?x foaf:name ?name .
?x foaf:mbox ?mbox .
}

Based on the RDF data set, what is the result of the following query?

PREFIX foaf: http://xmlns.com/foaf/0.1
SELECT ?x ?name
{
?x foaf:name ?name
}

Based on the RDF data set, what is the result of the following query?

PREFIX dc: http://purl.org/dc/elements/1.1
SELECT ?title WHERE
{
?x dc:title ?title.
FILTER regex(?title, “SPARQL”)
}
title
“SPARQL tutorial”
Based on the RDF data set, what is the result of the following query?

PREFIX dc: http://purl.org/dc/elements/1.1
SELECT ?title WHERE
{
?x dc:title ?title.
FILTER regex(?title, “web”, “i”)
}
title
“The Semantic Web”
Based on the RDF data set, what is the result of the following query?

PREFIX dc: http://purl.org/dc/elements/1.1
PREFIX ns: http://example.org/ns#
SELECT ?title ?price WHERE
{
?x ns:price ?price .
?x dc:title ?title .
FILTER(?price < 30.5)
}
title
“The Semantic Web”
price
23
Based on the RDF data set, what is the result of the following query?
PREFIX foaf: http://xmlns.com/foaf/0.1
SELECT DISTINCT ?name WHERE
{
?x foaf:name ?name .
}
name
“Alice”
Write a SPARQL command for writing the name of all movies and their directors for every movie in which MacWin has played, sorted based on the names of movies.

SELECT ?movie ?director WHERE
{
:MacWin played-in ?movie .
?director directed ?movie .
}
ORDER BY ?movie
(correct, but in practice we usually create a resource for MacWin then set its name-property to MacWin)
Write a SPARQL command for writing the number of movies in which MacWin has played.
(hint: use COUNT)

SELECT (COUNT(?movie) AS ?movieCount) WHERE
{
:MacWin played-in ?movie
}
Write a SPARQL command for writing the number of movies in which each actor has played
(hint: use GROUP BY)

SELECT ?actor (COUNT(?movie) as ?movieCount) WHERE
{
?actor played-in ?movie
}
GROUP BY ?actor
Write a SPARQL command for writing the number of movies in which each actor has played. Your command should only display those who have played in more than 6 movies
(hint: use HAVING)

SELECT ?actor (COUNT(?movie) AS ?movieCount) WHERE
{
?actor played-in ?movie
}
GROUP BY ?actor
HAVING (movieCount > 6)