RDF Query Language SPARQL


The Simple Protocol and RDF Query Language (SPARQL) is a SQL-like language for querying RDF data. For expressing RDF graphs in the matching part of the query, TURTLE syntax is used.

An example of a SELECT query follows.

    PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
    SELECT ?name ?mbox
    WHERE { ?x foaf:name ?name .
            ?x foaf:mbox ?mbox . }

The first line defines namespace prefix, the last two lines use the prefix to express a RDF graph to be matched. Identifiers beginning with question mark ? identify variables. In this query, we are looking for resource ?x participating in triples with predicates foaf:name and foaf:mbox and want the subjects of these triples. Syntactic shortcuts of TURTLE can be used in the matching part.

In addition to specifying graph to be matched, constraints can be added for values using FILTER construct. An example of string value restriction is FILTER regex(?mbox, "company") that specifies regular expression query. An example of number value restriction is FILTER (?price < 20) that specifies that ?price must be less than 20. A few special operators are defined for the FILTER construct. They include isIRI for testing whether variable is IRI/URI, isLiteral for testing whether variable is literal, bound to test whether variable was bound and others - see the specification.

The matching part of the query may include OPTIONAL triples. If the triple to be matched is optional, it is evaluated when it is present, but the matching does not fail when it is not present. Optional sections may be nested. It is possible to make UNION of multiple matching graphs - if any of the graphs matches, the match will be returned as a result. The FROM part of the query is optional and may specify the RDF dataset on which query is performed.

The sequence of result may be modified using the following keywords with the meaning similar to SQL:

There are four query result forms. In addition to the possibility of getting the list of values found it is also possible to construct RDF graph or to confirm whether a match was found or not.

The CONSTRUCT form specifies a graph to be returned with variables to be substituted from the query pattern, such as in the following example that will return graph saying that Alice knows last two people when ordered by alphabet from the given URI (the result in the RDF graph is not ordered, it is a graph and so the order of triples is not important).

    PREFIX foaf: <http://xmlns.com/foaf/0.1/>
    CONSTRUCT { <http://example.org/person#Alice> foaf:knows ?x }
    FROM <http://example.org/foaf/people>
    WHERE { ?x foaf:name ?name }
    ORDER BY desc(?name)
    LIMIT 2

The DESCRIBE form will return information about matched resources in a form of an RDF graph. The exact form of this information is not standardized yet, but usually a blank node closure like for example Concise Bounded Description (CBD) is expected. In short, all the triples that have the matched resource in the object are returned; when a blank node is in the subject, then the triples in which this node participates as object are recursively added as well.

The ASK form is intended for asking yes/no questions about matching - no information about matched variables is returned, the result is only indicating whether matching exists or not.

The SPARQL specification is in the state of working draft (state at the end of 2006), but it is already implemented in some software packages and it seems that it will become the main RDF querying language for the semantic web. The specification of protocol for a SPARQL web service is available as well - SPARQL then serves as a RDF data access protocol.



Previous - OWL Example with RDF Graph           Next - Reasoning support


(c) Marek Obitko, 2007 - Terms of use