Introduction #
This is really the atomic query. It contains all of the parts of the triple formula, and yields real (albeit not really usable) results: but it won’t get you far, on its own. In fact, the SPO (standing for “subject, predicate, object”) is probably very similar to what you will see if you log in to the GPI SPARQL endpoint as a default query.
Why even include this as a SPRQL entry? Well, even the default could use some clarification, if you are coming to SPARQL for the first time. There are a few things to be on the look out for.
First, we’ve left off the normal prefixes—that is, something like PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>, which appears in the true default query. That’s because it will help to see what a query looks like without this (admittedly useful) obfuscation. Feel free to insert the prefix above right at the start of the query, above the SELECT ... line. The results will look different, at the very least with an rdf:type value appearing in the ?predicate column.
Second, the results are more or less indicative of the results you will receive to any SPARQL query. It is possible to ask for other formats (JSON, for instance), but you’ll need to do so through command line (something we won’t be offering any instruction in, here). The results arrive as a table, exportable as a CSV file. But that is really all you will get from the SPARQL endpoint—it’s pretty much that simple.
Within that, there are significant challenges in understanding the results—why they yield duplicates, why no results are returned, etc.—but those aren’t problems for this entry. For a breakdown of the query itself, take a look at the Commentary section, below.
Query #
SELECT * WHERE {
?subject ?predicate ?object.
}
LIMIT 50
Commentary #
- Line 1: This is the basic SELECT statement/method. The SPARQL documentation refers to it as a “form” which returns “variables and their bindings directly”. It originates in form with the SQL relational database query language. It has been noted that other approaches to Linked Open Data query were presented in the 2000s, not all of which followed SQL as closely, or at all.
- Line 1:
*means all or everything, specifically “all of the variables”. In this case, it will return?subject,?predicate, and?object. Try actually replacing it withSELECT ?subject WHERE {. -
- Line 1:
{is the opening brace, with its pair being found below. You could really write this whole query as one line (and you will see that it is present as a single line in the address bar, if you hit “play” on the query: that’s how we pass the query from this site to the SPARQL endpoint). The important thing is not what lines things fall on, or tabs/spacing: the important thing is to make sure that opening brackets, quotes, and other containers get a closer too (see Line 3).
- Line 1:
- Line 2: Most statements end in a period. You will see the semi-colon used too, but that is for another lesson.
- Line 4: A query may return one result, three-hundred thousand results, or none. The LIMIT operator ensures that only the first number (specified by an integer) are returned. Critically, the query will stop running after this, so it is advisable to choose a low number when testing queries: when you’re sure the query is well structured, you can take the training wheels off (which will increase the computational resources, and therefore time, that query takes up—sometimes rendering a query unusable).