SPARQL Query Format

Created on Feb. 23, 2013, 10:42 a.m. by Hevok & updated by Hevok on May 2, 2013, 5:20 p.m.

SPARQL is inspired by the SQL Query Language where one has a SELECT Statement and in the Select Statement one says that one want to have Information on these Variables (e.g. about ?p and ?o) and then the Graph Pattern will be combined with the Keyword WHERE, the WHERE Clause. In the WHERE Clause one has two braces and with in this braces there comes the Graph Patterns that consists here of a URI of a Subject and two Variables that are already listed in the SELECT Statement. In the end the answer of this Query will be Subjects or Triples that have as Subject the stated Subject and they are connected to Objects via specific Properties. So the Graph is traversed and each matching Triple is selected.

  • inspired by SQL

    SELECT ?p, ?o
    WHERE { <subject> ?p ?o. }
  • Triple in "WHERE" part defines Graph Query with Variables ?p and ?o

  • Query return Table with matching ?p, ?o pairs
     Query answers Subject -?p->?o

Variables in SPARQL Queries are defined by simple strings prefixed with a Question or dollar Mark (e.g. "?x", "$x").

To abbreviate these Graph Patterns of course one can use Prefixes just like in RDF and RDF Schema. In SPARQL one can define some Prefixes that is done in the same way as in RDF with one difference one does not need to put a period these Triples. Then comes the SELECT Statement (e.g. Lecture and Managers) then comes the FROM Statement. The FROM part usually describes the Graph one is referring to. For example in one Knowledge Base there might be different RDF Graphs. One can ask on this question on the basics of a specific Graph. So only this Graph is queried with this SELECT expression. Then comes the WHERE expression were one has all the conditions for the Graph Patterns that have to be fulfilled and that will be matched in the Graph. So the first thing that is ask here is selected for the Variable x something that has the type lecture, then select the label of this lecture, then select somebody from the staff, then select the labels of that staff member and in the end one wants to combine lectures and the staff members, i.e. pleas take into consideration the lectures and the according managers that will be selected in this Statement. So all these five will be put together in the end on will select all managers and lectures that belong together from that Graph, so these things will be combined.

The WHERE clause defines where are the Graph Patterns, the PREFIX specifies one or more namespaces that have to be used, then one has the FROM that specifies the RDF Graph that will be queried and in the end one can define a kind of BASE URI that serves as a base URI when one uses simply a colon as a PREFIX.

Given the same Query as above one can say referring to the answer of this Query which will be Query please put this Table into a specific order via the ORDER BY Statement. Then it is said that these Triples should be ordered in a descending order, according to the name of the Manager. So ORDER BY is for ordering the Output. Then one can limit the Output for instance to the first 10 results. One does this by the keyword limit and number and then one can additional specify one does not want the first one, but lets start at specific number so one can define some kind of an OFFSET.

  • Search all lectures and their managers ordered by managers in descending order and limit the results to the first 10 starting the list at position 10:

    PREFIX events: PREFIX rdfs: SELECT ?lecture ?manager FROM <...> WHERE { ?x rdf:type events:Lecture : rdfs:label ?Lecture . ?y rdf:type events:Staff rdfs:label ?manager ?x hpi:isManagedBy ?y . } ORDER BY DESC (?manager) LIMIT 10 OFFSET 10

Overall SPARQL is a simple Language and is pretty close to SQL.


Tags: form, query, formatting, database, data
Categories: Tutorial
Parent: SPARQL

Update entry (Admin) | See changes

Comment on This Data Unit