Skip to 0 minutes and 3 secondsBARRY NORTON: Just a quick word about SELECT because it's the familiar form. It's the one that looks like SQL. And like SQL, we could say SELECT and have a subset of our variables or an asterisks for all of them. We can, if we want, say FROM. In SQL, you always have to say FROM which tables do you want to join. In SPARQL you don't have to because the default thing to do is just to put all your graphs, so all of the graphs that you get from different documents together in one big graph and query across the whole thing. So we have an optional FROM clause. We have a WHERE clause, which in SPARQL is where the graph pattern goes.
Skip to 0 minutes and 48 secondsAnd then finally, you have modifiers like, for instance, ORDER BY, just like in SQL. So a really quick example, if we want to find out about albums that were made by the Beatles, we have this identifier that we've seen throughout for the Beatles. So in the WHERE part of our query, we use that as the subject to a statement.
Skip to 1 minute and 15 secondsThe predicate made from foaf: -- that's one way to relate things that people have made, products, to them. So we said the Beatles made, but instead of specifying the object, we use a variable. We say, you tell me what the Beatles have made. But using that same variable, we say in a second triple, whatever you tell me must also have a relationship defined by this predicate, which is title. So it should have a title. And again, we use a variable to say you tell me what that title is. Then after a semicolon, we say this same thing that the Beatles made has to have tracks on it according to the music ontology.
Skip to 2 minutes and 1 secondSo the predicate is track, but we use a variable for the object. Again, you tell me. And then using a final triple after a full stop, we say that track, it also has to have a title. And then in the head, in the SELECT part, we say, well, actually we're not interested in the URIs for the album and the track, respectively, album and track. We're interested in the titles that are attached to them by these two triples. So please just give me back these literals. And you get back a table, one for each variable where you have literals in each one, the album name and a track on it.
Skip to 2 minutes and 45 secondsAnd album name and a track on it, according to this constraint, the album has to have been made by the Beatles and the album has to have the track on it according to that track relationship.
SPARQL queries allow us to use linked data in semantic applications. You can experiment with SPARQL endpoints to learn the query syntax.
You may find it helpful to re-watch this video from last week before reading the text below.
In developing an application like a music portal, you will need to build queries into your application code. There are APIs (Application Program Interfaces) that help you to do this, like the Jena library for Java. However, before learning to use APIs, you can learn the syntax for queries (and their responses) by using a SPARQL endpoint to enter the query by hand.
Figure 2.3 Typing a query into a SPARQL endpoint
As an example of this procedure Figure 2.3 shows a snapshot of a SPARQL endpoint with a query about the albums made by the Beatles. This is similar to the Euclid SPARQL endpoint which you will get the chance to use later in the week. When you run a query on such a service, you will obtain a results page similar to that shown in Figure 2.4 containing a table giving values for the variables ?album and ?title.
Figure 2.4 Viewing the response to a query
Types of query
SPARQL defines the following query types for data retrieval:
An ASK query is a test of whether there are any resources in the dataset matching the search pattern; the response is either true or false. Intuitively, it poses the question: ‘Are there any X, Y, etc. satisfying the following conditions …?’
A SELECT query returns a table in which columns represent variables and rows represent variable bindings matching the search pattern. Intuitively: ‘Return a table of all X, Y, etc. satisfying the following conditions …’.
A CONSTRUCT query returns an RDF graph (i.e., set of triples) matching a template, using variable bindings obtained from the dataset using a search pattern. Intuitively: ‘Find all X, Y, etc. satisfying the following conditions … and substitute them into the following template in order to generate (possibly new) RDF statements …’.
A DESCRIBE query returns an RDF graph, extracted from the dataset, which provides all available information about a resource (or resources). The resource may be identified by name, or by a variable accompanied by a graph pattern. Intuitively: ‘Find all statements in the dataset that provide information about the following resource(s) … (identified by name or description)’.
In the following steps, we cover the ASK and SELECT queries in more detail. Construct and Describe will be the focus of Week 3.
This work is a derivative of ‘Using Linked Data Effectively’ by The Open University (2014) and licensed under CC by 4.0 International Licence adapted and used by the University of Southampton. http://www.euclid-project.eu/