Skip to 0 minutes and 7 secondsBARRY NORTON: The main part of the query, the main part of any query, even though we're looking at SELECT here, this is the body of all of the types of queries that we can ask in SPARQL, is the WHERE part. It's between set brackets because we have a set of patterns. Collectively, they're called a graph pattern. The simplest way to make a graph pattern is out of triple patterns. So the first row here, we see, as we've looked at several times this pattern, the Beatles made something, some resource.

Skip to 0 minutes and 46 secondsJust like Turtle, we can either use a full stop here and introduce a new triple pattern or, as we'll see on the next row, having written a triple, we can also use, for instance, a semicolon to say we carry on with the same subject. So we've got one pattern, the Beatles made something. A second triple pattern, that something is in the class of records. And then after the semicolon, that something, still of the subject, has a title that we want to know. And then we can order by title because that's a string, it's a literal, so it's a good basis for ordering things. And so we look for the match, we get a table full of bindings.

Skip to 1 minute and 37 secondsWe order the bindings, and then we do what's called a projection. We select which columns we're interested in from that table to give back to the person who issued the query. There's a small note, modifies at the end, as well as ordering. We can also limit. So say we only want so many rows, we can offset, we can say according to that order, skip a few. And then either give me the rest or give me the rest up to a limit. And we can also, we'll see later, do some interesting things like instead of just giving the rows back, we can do a grouping of those rows.

Skip to 2 minutes and 15 secondsSo we can form a new table based on having a row for groups between the rows in the bindings. So to do, as we'll see later, things called aggregates, that's very useful.

Queries using SELECT

Unlike ASK queries, SELECT queries let you receive tabulated results of the variables you request.

To show the basic syntax of a SELECT query, let us return to an example given earlier:

PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX mo: <http://purl.org/ontology/mo/>

SELECT ?album
WHERE { dbpedia:The_Beatles foaf:made ?album .
        ?album a mo:SignalGroup
      } 

Note the following:

  • After the (optional) PREFIX statements, the query proper begins with SELECT, which specifies the query type.
  • After SELECT you list all the variables that you would like to see tabulated in the response. Variables should be separated by spaces if there are more than one. Alternatively, you can simply put an asterisk after SELECT, meaning that all variables should be tabulated. In the example, this would yield the same result.
  • As before, WHERE introduces a graph pattern including one or more variables.
  • As before, layout is free provided that terms are separated by white space.

Exercise

Try running the query in the Euclid Exercise 2 endpoint.

In response to comments from learners, we have created a video screencast walkthrough of how to approach this activity. It is also strongly recommended that you read and / or print out the attached Exercise 2 worksheet before you try to run any queries.

This will let you run queries on a SPARQL endpoint, so that you can try them out for yourself as the course progresses. You should try running each of the queries discussed in the course, and you will get the most out of the exercise by modifying the queries afterwards to apply the theory to new examples.

Hints

Note: This exercise will work on mobile devices, but typing the code will be much less prone to errors using a keyboard, and a larger screen will make it easier to manage your code. We recommend using a PC or laptop if possible.

When accessing the exercise, you will first need to connect to a repository, so that you can access the data. Select ‘Choose repository’ at the top-right of the screen, and then select ‘MusicBrainz’ from the drop-down list.

To try out SPARQL queries on the MusicBrainz dataset, you then need to head to ‘SPARQL’ on the menu at the top of the page. Then, in the query box, you can type in a SPARQL query, such as the example discussed above. Try entering it now, and selecting ‘Run’.

You should obtain in response a list of MusicBrainz URIs that use arbitrary codes rather than recognisable words. However, we can exploit some further triples associating albums with their titles (encoded as string literals) in order to obtain more understandable output, as follows:

PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX mo: <http://purl.org/ontology/mo/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>

SELECT ?album ?title
WHERE { dbpedia:The_Beatles foaf:made ?album .
        ?album a mo:SignalGroup .
        ?album dc:title ?title
      } 

We’ve now covered the basics of ASK and SELECT queries. The remaining steps in this activity will show you how to expand these queries to format or filter the responses you receive.


Share this video:

This video is from the free online course:

Introduction to Linked Data and the Semantic Web

University of Southampton

Get a taste of this course

Find out what this course is like by previewing some of the course steps before you join: