Back
Close

An introduction to RDF querying in SPARQL

Zwifi
4,475 views

Run your first queries

To begin this tutorial, we will run some simple queries on a cinema dataset available here, described with the YACO ontology. Basically, this datast contains a bunch of movies, and of people related to these movies (mainly actors and directors, but also producers or writers).

Searching for movies

The first query we might want to run is: What are the available movies ? You may examine an instance of movie provided for reference. In SPARQL, it is formulated as follows:

What are the available movies?
PREFIX yaco: <https://www.irit.fr/recherches/MELODI/ontologies/cinema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?movie
WHERE {
?movie rdf:type yaco:Movie.
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Only one variable (?movie) is present in the query, and you will notice that only one element is present in each result row.

Searching for actors and birth years

Now, let's ask: What actors are we talking about, and when were they born?

What actors are we talking about, and when were they born?
PREFIX yaco: <https://www.irit.fr/recherches/MELODI/ontologies/cinema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?actor ?year
WHERE {
?actor rdf:type yaco:Actor;
yaco:birthYear ?year.
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

In this case, the query has two variables to bound, and the result set is constituted of pairs of potential instances.

Any part of the triple may be a variable

Even the predicate ? You bet.

How is Tom Hanks involved in 'That Thing You Do'?
PREFIX yaco: <https://www.irit.fr/recherches/MELODI/ontologies/cinema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX : <https://www.irit.fr/recherches/MELODI/ontologies/cinema-catalog#>
SELECT ?relation
WHERE {
:TomHanks ?relation :ThatThingYouDo.
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

An actor AND a director? Somebody stop that man! He actually also wrote the movie and played music in it, but we did not put that in the dataset because it would be a lot for a single person.

Multiple variables in a single triple

And even further, multiple elements of the same triple may be variables.

What do we know about Lilli Wachowski?
PREFIX yaco: <https://www.irit.fr/recherches/MELODI/ontologies/cinema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX : <https://www.irit.fr/recherches/MELODI/ontologies/cinema-catalog#>
SELECT ?predicate ?object
WHERE {
:LillyWachowski ?predicate ?object.
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Why stop there ? We can list every single triple of the knowledge base if we want, by using a completely unbound triple.

What is in the knowledge base?
SELECT ?subject ?predicate ?object
WHERE {
?subject ?predicate ?object.
} LIMIT 10
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

In this case however, we limit the size of the result set, to avoid an overload.

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants