SPARQL Query Language for RDF

Editors working draft.
Live Draft - version:
$Revision: 1.692 $ of $Date: 2006/07/03 12:33:46 $
Eric Prud'hommeaux, W3C <eric@w3.org>
Andy Seaborne, Hewlett-Packard Laboratories, Bristol <andy.seaborne@hp.com>
published W3C Technical Report Version:
Latest published version; see also public-rdf-dawg-comments@w3.org Mail Archives


RDF is a flexible and extensible way to represent information about World Wide Web resources. It is used to represent, among other things, personal information, social networks, metadata about digital artifacts, as well as provide a means of integration over disparate sources of information. A standardized query language for RDF data with multiple implementations offers developers and end users a way to write and to consume the results of queries across this wide range of information. Used with a common protocol, applications can access and combine information from across the Web.

This document describes the query language part of the SPARQL Protocol And RDF Query Language for easy access to RDF stores. It is designed to meet the requirements and design objectives described in RDF Data Access Use Cases and Requirements [UCNR]

Status of This document

This is a live document and is subject to change without notice. It reflects the best effort of the editors to reflect implementation experience and incorporate input from various members of the WG, but is not yet endorsed by the WG as a whole.

The change log enumerates changes since the 21 July 2005 Working Draft. Also, since that draft, we have been tracking threads in the public-rdf-dawg-comments archive more closely. A status report is updated every week or so.

Comments that the chair would like the editors of this spec to consider include:

For the definitions, we have an XSLT transformation, defns.xsl, that extracts them from this document. A live version of the output is available via the W3C XSLT service.

See also: SPARQL Test Cases, in progress.

Table of Contents


In addition, the collected formal definitions are collected into a single document "SPARQL Query Language for RDF - Formal Definitions".

1 Introduction

An RDF graph is a set of triples; each triple consists of a subject, a predicate and an object. RDF graphs are defined in RDF Concepts and Abstract Syntax [CONCEPTS]. These triples can come from a variety of sources. For instance, they may come directly from an RDF document; they may be inferred from other RDF triples; or they may be the RDF expression of data stored in other formats, such as XML or relational databases. The RDF graph may be virtual, in that it is not fully materialized, only doing the work needed for each query to execute.

SPARQL is a query language for getting information from such RDF graphs. It provides facilities to:

As a data access language, it is suitable for both local and remote use. The companion SPARQL Protocol for RDF document [SPROT] describes the remote access protocol.

1.1 Document Conventions

In this document, examples assume the following namespace prefix bindings unless otherwise stated:

rdf http://www.w3.org/1999/02/22-rdf-syntax-ns#
rdfs http://www.w3.org/2000/01/rdf-schema#
xsd http://www.w3.org/2001/XMLSchema#
fn http://www.w3.org/2005/xpath-functions#

2 Making Simple Queries

The SPARQL query language is based on matching graph patterns. The simplest graph pattern is the triple pattern, which is like an RDF triple, but with the possibility of a variable instead of an RDF term in the subject, predicate or object positions. Combining triple patterns gives a basic graph pattern, where an exact match to a graph is needed to fulfill a pattern.

Later sections of this document describe how other graph patterns can be built using the graph operators OPTIONAL and UNION; how graph patterns can be grouped together; how queries can extract information from more than one graph, and how it is also possible to restrict the values allowed in matching a pattern.

2.1 Writing a Simple Query

The example below shows a SPARQL query to find the title of a book from the information in the given RDF graph. The query consists of two parts, the SELECT clause and the WHERE clause. The SELECT clause identifies the variables to appear in the query results, and the WHERE clause has one triple pattern.


<http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> "SPARQL Tutorial" .


SELECT ?title
  <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> ?title .

Query Result:

"SPARQL Tutorial"

2.1.1 Syntax for IRIs

The terms delimited by "<>" are IRI references [RFC3987]. They stand for IRIs, either directly, or relative to a base IRI.  IRIs are a generalization of URIs [RFC3986] and are fully compatible with URIs and URLs.

The SPARQL syntax provides two abbreviation mechanisms for IRIs, prefixed names and relative IRIs.

Prefixed names

The PREFIX keyword associates a prefix label with an IRI. A prefixed name is a prefix label and a local part, separated by a colon ":". It is mapped to an IRI by concatenating the local part to the IRI corresponding to the prefix.

Relative IRIs

Relative IRIs are combined with base IRIs as per Uniform Resource Identifier (URI): Generic Syntax [RFC3986] using only the basic algorithm in Section 5.2 . Neither Syntax-Based Normalization nor Scheme-Based Normalization (described in sections 6.2.2 and 6.2.3 of RFC3986) is performed. Characters additionally allowed in IRI references are treated in the same way that unreserved characters are treated in URI references, per section 6.5 of Internationalized Resource Identifiers (IRIs) [RFC3987].

The BASE keyword defines the Base IRI used to resolve relative IRIs per RFC3986 section 5.1.1, "Base URI Embedded in Content". Section 5.1.2, "Base URI from the Encapsulating Entity" defines how the Base IRI may come from an encapsulating document, such as a SOAP envelope with an xml:base directive, or a mime multipart document with a Content-Location header. The "Retrieval URI" identified in 5.1.3, Base "URI from the Retrieval URI", is the URL from which a particular SPARQL query was retrieved. If none of the above specifies the Base URI, the default Base URI (section 5.1.4, "Default Base URI") is used.

The following fragments are different ways to write the same IRI:

BASE <http://example.org/book/>
PREFIX book: <http://example.org/book/>

2.1.2 Syntax for Literals

The general syntax for literals is a string (enclosed in quotes, either double quotes "" or single quotes '' ), with either an optional language tag (introduced by @) or an optional datatype IRI or prefixed name (introduced by ^^).

As a convenience, integers can be written directly and are interpreted as typed literals of datatype xsd:integer; decimal numbers, where there is '.' in the number but no exponent, are interpreted as xsd:decimal and a number with an exponent is interpreted as an xsd:double. Values of type xsd:boolean can also be written as true or false.

2.1.3 Syntax for Variables

Variables in SPARQL queries have global scope; use of a given name anywhere in a query identifies the same variable. Variables are indicated by "?"; the "?" does not form part of the variable name. "$" is an alternative to "?". In a query, $abc and ?abc are the same variable. The possible names for variables are given in the SPARQL grammar.

2.1.4 Syntax for Blank Nodes

A blank node can appear in a query pattern and will take part in the pattern matching.  Blank nodes are indicated by either the form "_:a" or use of "[ ]".  Further syntactic forms involving blank nodes are described below.

2.1.5 Syntax for Triple Patterns

Triple Patterns are written as a list of subject, predicate, object; there are abbreviated ways of writing some common triple pattern constructs.

2.1.6 Examples of Query Syntax

The following examples express the same query:

PREFIX  dc: <http://purl.org/dc/elements/1.1/>
SELECT  ?title
WHERE   { <http://example.org/book/book1> dc:title ?title }
PREFIX  dc: <http://purl.org/dc/elements/1.1/>
PREFIX  : <http://example.org/book/>
SELECT  $title
WHERE   { :book1  dc:title  $title }
BASE    <http://example.org/book/>
PREFIX  dc: <http://purl.org/dc/elements/1.1/>
SELECT  $title
WHERE   { <book1>  dc:title  ?title }

Prefixes are syntactic: the prefix name does not affect the query, nor do prefix names in queries need to be the same prefixes as used in a serialization of the data. The following query is equivalent to the previous examples and will give the same results when applied to the same data:

BASE    <http://example.org/book/>
PREFIX  dcore:  <http://purl.org/dc/elements/1.1/>
SELECT  ?title
WHERE   { <book1> dcore:title ?title }

2.1.7 Data descriptions used in this document

The data format used in this document is Turtle [TURTLE], used to show each triple explicitly. Turtle allows URIs to be abbreviated with prefixes:

@prefix dc:   <http://purl.org/dc/elements/1.1/> .
@prefix :     <http://example.org/book/> .
:book1  dc:title  "SPARQL Tutorial" .

2.1.8 Result Descriptions used in this document

The term "binding" is used as a descriptive term to refer to a pair of (variable, RDF term). In this document, we illustrate results in tabular form. If variable x is bound to "Alice" and variable y is bound to <http://example/a>, we show this as:

x y
"Alice" <http://example/a>

Not every binding needs to exist in every row of the table. Optional matches and alternative matches may leave some variables unbound.

Results can be returned in XML using the SPARQL Query Results XML Format [RESULTS].

2.2 Initial Definitions

The following terms are used from RDF Concepts and Abstract Syntax [CONCEPTS]

RDF Concepts and Abstract Syntax "anticipates an RFC on Internationalized Resource Identifiers. Implementations may issue warnings concerning the use of RDF URI References that do not conform with [IRI draft] or its successors."

SPARQL is defined in terms of IRIs, a subset of RDF URI References that omits spaces.

Definition: RDF Term

let I be the set of all IRIs.
let RDF-L be the set of all RDF Literals
let RDF-B be the set of all blank nodes in RDF graphs

The set of RDF Terms, RDF-T, is I union RDF-L union RDF-B.

This definition of RDF Term collects together several basic notions from the RDF data model,  but updated to refer to IRIs rather than RDF URI references.

Note that all IRIs are absolute; they may or may not include a fragment identifier [RFC3987, section 3.1]. IRIs include URIs [RFC3986] and URLs. The abbreviated forms (relative IRIs and prefixed names) in the SPARQL syntax are resolved to produce absolute IRIs.

Definition: Query Variable

A query variable is a member of the set V where V is infinite and disjoint from RDF-T.

Definition: Graph Pattern

A Graph Pattern is one of:

Definition: SPARQL Query

A SPARQL query is a tuple (GP, DS, SM, R) where:

The graph pattern of a query is called the query pattern.

The graph pattern may be the empty pattern. The set of solution modifiers may be the empty set.

2.3 Triple Patterns

The building blocks of queries are triple patterns. The following triple pattern has a subject variable (the variable book), a predicate  dc:title and an object variable (the variable title).

 ?book dc:title ?title .

Definition: Triple Pattern

A triple pattern is member of the set:
    (RDF-T union V) x (I union V) x (RDF-T union V)

This definition of Triple Pattern includes literal subjects. This has been noted by RDF-core.

"[The RDF core Working Group] noted that it is aware of no reason why literals should not
  be subjects and a future WG with a less restrictive charter may
  extend the syntaxes to allow literals as the subjects of statements."

Any SPARQL triple pattern with a literal as subject will fail to match on any RDF graph.

2.4 Pattern Solutions

Definition: Pattern Solution

A variable substitution is a substitution function from a subset of V, the set of variables, to the set of RDF terms, RDF-T.

A pattern solution, S, is a variable substitution whose domain includes all the variables in V and whose range is a subset of the set of RDF terms.

The result of replacing every member v of V in a graph pattern P by S(v) is written S(P).

If v is not in the domain of S then S(v) is defined to be v.

The term "solution" is used for "pattern solution" where it is unambiguous.

Graph patterns match against the default graph of an RDF dataset, except for the RDF Dataset Graph Pattern. In this section, all matching is described for a single graph, being the default graph of the RDF dataset being queried.

For example, the query:

PREFIX  dc: <http://purl.org/dc/elements/1.1/>
SELECT  ?book ?title
WHERE   { ?book dc:title ?title }

has a single triple pattern as the query pattern. It matches a graph of a single triple:

<http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> "SPARQL" .

with solution:

book title
<http://example.org/book/book1> "SPARQL"

2.5 Basic Graph Patterns

A basic graph pattern is a set of triple patterns and forms the basis of SPARQL query matching. Matching a basic graph pattern is defined in terms of generic entailment to allow for future extension of the language.

Definition: Basic Graph Pattern

A Basic Graph Pattern is a set of Triple Patterns.

Definition: E-entailment Regime

An E-entailment regime is a binary relation between subsets of RDF graphs.

A graph in the range of an E-entailment is called well-formed for the E-entailment.

This specification covers only simple entailment [RDF-MT] as E-entailment. Examples of other E-entailment regimes are RDF entailment [RDF-MT], RDFS entailment [RDF-MT], OWL entailment [OWL-Semantics].

Definition: Basic Graph Pattern equivalence

Two basic graph patterns are equivalent if there is a bijection M between the terms of the triple patterns that maps blank nodes to blank nodes and maps variables, literals and IRIs to themselves, such that a triple ( s, p, o ) is in the first pattern if and only if the triple ( M(s), M(p) M(o) ) is in the second.

This definition extends that for RDF graph-equivalence to basic graph patterns by preserving variable names across equivalent graphs.

2.5.1 General Framework

Definition: Scoping Set

A Scoping Set B is some set of RDF terms.

The scoping set restricts the values of variable assignments in a solution. The scoping set may be characterized differently by different entailment regimes.

Definition: Scoping Graph

The Scoping Graph G' for RDF graph G, is an RDF Graph that is graph-equivalent to G

The scoping graph makes the graph to be matched independent of the chosen blank node names.

The same scoping set and scoping graph is used for all basic graph pattern matching in a single SPARQL query request.

Definition: Basic Graph Pattern E-matching

Given an entailment regime E, a basic graph pattern BGP, and RDF graph G, with scoping graph G', then BGP E-matches with pattern solution S on graph G with respect to scoping set B if:

The introduction of the basic graph pattern BGP' in the above definition makes the query basic graph pattern independent of the choice of blank node names in the basic graph pattern.

2.5.2 SPARQL Basic Graph Pattern Matching

These definitions allow for future extensions to SPARQL. This document defines SPARQL for simple entailment and the scoping set B is the set of all RDF terms in G'.

When using simple entailment, the operation of querying an RDF graph provides access to the graph structure, up to blank node renaming; nothing that is not already in the graph G needs to be inferred or constructed, even implicitly.

A pattern solution can then be defined as follows: to match a basic graph pattern under simple entailment, it is possible to proceed by finding a mapping from blank nodes and variables in the basic graph pattern to terms in the graph being matched; a pattern solution is then a mapping restricted to just the variables, possibly with blank nodes renamed. Moreover, a uniqueness property guarantees the interoperability between SPARQL systems: given a graph and a basic graph pattern, the set of all the pattern solutions is unique up to blank node renaming.

2.5.3 Example of Basic Graph Pattern Matching

As an example of a Basic Graph Pattern:


@prefix foaf:    <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name   "Johnny Lee Outlaw" .
_:a  foaf:mbox   <mailto:outlaw@example.com> .

_:b  foaf:name   "A. N. Other" .
_:b  foaf:mbox   <mailto:other@example.com> .

There is a blank node [CONCEPTS] in this dataset, identified by _:a. The label is only used within the file for encoding purposes. The label information is not in the RDF graph.


PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
SELECT ?mbox
  { ?x foaf:name "Johnny Lee Outlaw" .
    ?x foaf:mbox ?mbox }

Query Result:


This query contains a basic graph pattern of two triple patterns, each of which must match with the same solution for the graph pattern to match. The pattern solution matching the basic graph pattern maps the variable 'x' to blank node _:a and variable 'mbox' to the IRI mailto:outlaw@example.com. The query only returns the variable 'mbox'

2.5.4 Basic Graph Patterns in the SPARQL Syntax

In the SPARQL syntax,  Basic Graph Patterns are sequences of triple patterns mixed with value constraints. Other graph patterns separate basic patterns. The two query fragments below each contain the same basic graph pattern of

{ _:x :p ?v . _:x :q ?w . }

with the scope of the blank node label being the basic graph pattern.

{ _:x :p ?v .
  FILTER (?v < 3) .
  _:x :q ?w .
{ _:x :p ?v .
  _:x :q ?w .
  FILTER (?v < 3) .

2.6 Multiple Matches

The results of a query is the set of all pattern solutions that match the query pattern, giving all the ways a query can match the graph being queried. Each result is one solution to the query and there may be zero, one or multiple results to a query.


@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name   "Johnny Lee Outlaw" .
_:a  foaf:mbox   <mailto:jlow@example.com> .
_:b  foaf:name   "Peter Goodguy" .
_:b  foaf:mbox   <mailto:peter@example.org> .


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

Query Result:

name mbox
"Johnny Lee Outlaw" <mailto:jlow@example.com>
"Peter Goodguy" <mailto:peter@example.org>

The results enumerate the RDF terms to which the selected variables can be bound in the query pattern. In the above example, the following two subsets of the data provided the two matches.

 _:a foaf:name  "Johnny Lee Outlaw" .
 _:a foaf:box   <mailto:jlow@example.com> .
 _:b foaf:name  "Peter Goodguy" .
 _:b foaf:box   <mailto:peter@example.org> .

This is a basic graph pattern match, and all the variables used in the query pattern must be bound in every solution.

2.7 Blank Nodes in Query Results

The presence of blank nodes in query results can be indicated by labels in the serialization of query results.

Blank nodes in the results of a query are from the scoping set, but this information cannot be used by an application or client which receives these results, since all blank nodes in subsequent queries are treated as being local to that query. In effect, this means that information about co-occurrences of blank nodes may be treated as scoped to the results as defined in " SPARQL Query Results XML Format" or the CONSTRUCT result form.


@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name   "Alice" .
_:b  foaf:name   "Bob" .


PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
SELECT ?x ?name
WHERE  { ?x foaf:name ?name }
x name
_:c "Alice"
_:d "Bob"

The results above could equally be given with different blank node labels because the labels in the results only indicate whether RDF terms in the solutions were the same or different.

x name
_:r "Alice"
_:s "Bob"

These two results have the same information: the blank nodes used to match the query are different in the two solutions. There would be no relation if the label _:a were used in the results and the blank node label in the data graph.

2.8 Other Syntactic Forms

There are a number of syntactic forms that abbreviate some common sequences of triples. These syntactic forms do not change the meaning of the query.

2.8.1 Predicate-Object Lists

Triple patterns with a common subject can be written so that the subject is only written once, and used for more than one triple pattern by employing the ";" notation.

    ?x  foaf:name  ?name ;
        foaf:mbox  ?mbox .

This is the same as writing the triple patterns:

    ?x  foaf:name  ?name .
    ?x  foaf:mbox  ?mbox .

2.8.2 Object Lists

If triple patterns share both subject and predicate, then these can be written using the "," notation.

    ?x foaf:nick  "Alice" , "Alice_" .

is the same as writing the triple patterns:

   ?x  foaf:nick  "Alice" .
   ?x  foaf:nick  "Alice_" .

Note that both the triple patterns involving foaf:nick will need to match, not that one or the other should match.

Object lists can be combined with predicate-object lists:

   ?x  foaf:name ?name ; foaf:nick  "Alice" , "Alice_" .


   ?x  foaf:name  ?name .
   ?x  foaf:nick  "Alice" .
   ?x  foaf:nick  "Alice_" .

2.8.3 Blank Nodes in Queries

Blank nodes have labels which are scoped to the basic graph pattern. They are written as "_:a" for a blank node with label "a".

A blank node that is used in only one place in the query syntax can be abbreviated with []. A unique blank node will be created and used to form the triple pattern.

The [:p :v] construct can be used in triple patterns. It creates a blank node label which is used as the subject of all contained predicate-object pairs.  The created blank node can also be used in further triple patterns in the subject and object positions.

The following two forms

[ :p "v" ] .
[] :p "v" .

allocate a unique blank node label (here "b57") and are equivalent to writing:

_:b57 :p "v" .

This allocated blank node label can be used as the subject or object of further triple patterns. For example, as a subject:

[ :p "v" ] :q "w" .

is equivalent to the two triples:

_:b57 :p "v" .
_:b57 :q "w" .

and as an object:

:x :q [ :p "v" ] .

is equivalent to the two triples:

:x  :q _:b57 .
_:b57 :p "v" .

Abbreviated blank node syntax can be combined with other abbreviations for common subjects and predicates.

  [ foaf:name  ?name ;
    foaf:mbox  <mailto:alice@example.org> ]

This is the same as writing the following basic graph pattern for some uniquely allocated blank node:

  _:b18  foaf:name  ?name .
  _:b18  foaf:mbox  <mailto:alice@example.org> .

2.8.4 RDF Collections

RDF collections can be written in triple patterns using the syntax "(  )". The form () is an alternative for the IRI rdf:nil which is  http://www.w3.org/1999/02/22-rdf-syntax-ns#nil. When used with collection elements, such as (1 ?x 3 4), triple patterns and blank nodes are allocated for the collection and the blank node at the head of the collection can be used as a subject or object in other triple patterns.

(1 ?x 3 4) :p "w" .

is a short form for:

    _:b0  rdf:first  1 ;
          rdf:rest   _:b1 .
    _:b1  rdf:first  ?x ;
          rdf:rest   _:b2 .
    _:b2  rdf:first  3 ;
          rdf:rest   _:b3 .
    _:b3  rdf:first  4 ;
          rdf:rest   rdf:nil .
    _:b0  :p         "w" .  

RDF collections can be nested and can involve other syntactic forms:

(1 [:p :q] ( 2 ) ) .

is a short form for:

    _:b0  rdf:first  1 ;
          rdf:rest   _:b1 .
    _:b1  rdf:first  _:b2 .
    _:b2  :p         :q .
    _:b1  rdf:rest   _:b3 .
    _:b3  rdf:first  _:b4 .
    _:b4  rdf:first  2 ;
          rdf:rest   rdf:nil .
    _:b3  rdf:rest   rdf:nil .

2.8.5 rdf:type

The keyword "a" can be used as a predicate in a triple pattern and is an alternative for the IRI rdf:type ,  which is http://www.w3.org/1999/02/22-rdf-syntax-ns#type. This keyword is case-sensitive.

  ?x  a  :Class1 .
  [ a :appClass ] :p "v" .
  ?x    rdf:type  :Class1 .
  _:b0  rdf:type  :appClass .
  _:b0  :p        "v" .

2.9 Querying Reification Vocabulary

RDF defines a reification vocabulary which provides for describing RDF statements without stating them. These descriptions of statements can be queried by using the defined vocabulary.

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix dc:  <http://purl.org/dc/elements/1.1/> .
@prefix :    <http://example/ns#> .

_:a   rdf:subject   <http://example.org/book/book1> .
_:a   rdf:predicate dc:title .
_:a   rdf:object    "SPARQL" .
_:a   :saidBy       "Alice" .

_:b   rdf:subject   <http://example.org/book/book1> .
_:b   rdf:predicate dc:title .
_:b   rdf:object    "SPARQL Tutorial" .
_:b   :saidBy       "Bob" .

In this example data, there is no RDF triple giving the title of the book; there are triples that describe two such RDF statements but the statements themselves are not asserted in the graph. A query asking for any titles of any book returns nothing.

PREFIX dc: <http://purl.org/dc/elements/1.1/>

SELECT ?book ?title
{ ?book dc:title ?title }

Query Result:

book title

There are no triples in the graph with dc:title in the property position (it appears in the object position in the data).

A query can ask about descriptions of statements made by "Bob":

PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dc:   <http://purl.org/dc/elements/1.1/>
PREFIX :     <http://example/ns#>

SELECT ?book ?title
{ ?t rdf:subject    ?book  .
  ?t rdf:predicate  dc:title .
  ?t rdf:object     ?title .
  ?t :saidBy        "Bob" .

and there is one such description for a statement made by Bob:

book title
<http://example.org/book/book1> "SPARQL Tutorial"

3 Working with RDF Literals

An RDF Literal is written in SPARQL as a string containing the lexical form of the literal, followed by an optional language tag or an optional datatype. There are convenience forms for numeric-types literals which are of type xsd:integer, xsd:decimal, xsd:double and also for  xsd:boolean.

Examples of literal syntax in SPARQL include:

3.1 Matching RDF Literals

The data below contains a number of RDF literals:

@prefix dt:   <http://example.org/datatype#> .
@prefix ns:   <http://example.org/ns#> .
@prefix :     <http://example.org/ns#> .
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#> .

:x   ns:p     "42"^^xsd:integer .
:y   ns:p     "abc"^^dt:specialDatatype .
:z   ns:p     "cat"@en .

This RDF data is the target for query examples in the following sections.

3.1.1 Matching Integers

The pattern in the following query has a solution :x because 42 is syntax for "42"^^<http://www.w3.org/2001/XMLSchema#integer>.

SELECT ?v WHERE { ?v ?p 42 }

3.1.2 Matching Arbitrary Datatypes

The following query has a solution with variable v being :y. The query processor does not have to have any understanding of the values in the space of the datatype because, in this case, lexical form and datatype IRI both match exactly.

SELECT ?v WHERE { ?v ?p "abc"^^<http://example.org/datatype#specialDatatype> }

3.1.3 Matching Language Tags

This following query has no solution because "cat" is not the same RDF literal as "cat"@en:

SELECT ?x WHERE { ?x ?p "cat" }

but this does find a solution where variable x is substituted by :z:

SELECT ?x WHERE { ?x ?p "cat"@en }

3.2 Value Constraints

Graph pattern matching creates bindings of variables. It is possible to further restrict solutions by constraining the allowable bindings of variables to RDF Terms. Value constraints take the form of boolean-valued expressions; the language also allows application-specific constraints on the values in a solution.


@prefix dc:   <http://purl.org/dc/elements/1.1/> .
@prefix :     <http://example.org/book/> .
@prefix ns:   <http://example.org/ns#> .

:book1  dc:title  "SPARQL Tutorial" .
:book1  ns:price  42 .
:book2  dc:title  "The Semantic Web" .
:book2  ns:price  23 .


PREFIX  dc:  <http://purl.org/dc/elements/1.1/>
PREFIX  ns:  <http://example.org/ns#>
SELECT  ?title ?price
WHERE   { ?x ns:price ?price .
          FILTER (?price < 30) .
          ?x dc:title ?title . }

Query Result:

title price
"The Semantic Web" 23

By having a constraint on the "price" variable, only book2 matches the query because there is a restriction on the allowable values of "price".

3.3 Value Constraints – Definition

Definition: Value Constraint

A value constraint is a boolean-valued expression of variables and RDF Terms.

For value constraint C, a solution S matches C if S(C) is true, where S(C) is the boolean-valued expression obtained by substitution of the variables mentioned in C.

Constraints may be restrictions of the value of an RDF Term or they may be restrictions on some part of an RDF term, such as its lexical form. There is a set of functions & operators in SPARQL for constraints. In addition, there is an extension mechanism to provide access to functions that are not defined in the SPARQL language.

A constraint may lead to an error condition when testing some RDF term. The exact error will depend on the constraint: for example, in numeric operations, solutions with variables bound to a non-number or a blank node will lead to an error. Any potential solution that causes an error condition in a constraint will not form part of the final results, but does not cause the query to fail.

3.4 Matching Values and RDF D-entailment

RDF defines D-Entailment where extra semantic conditions are allowed for datatypes. When matching RDF literals in graph patterns, the datatype lexical-to-value mapping may be reflected into the underlying RDF graph, leading to additional matches where it is known that two literals are the same value. RDF semantics does not require this of all RDF graphs.

4 Graph Patterns

Complex graph patterns can be made by combining simpler graph patterns. The ways of creating graph patterns are:

4.1 Group Graph Patterns

Definition: Group Graph Pattern

A group graph pattern GP is a set of graph patterns, GPi.

A solution of Group Graph Pattern GP on graph G is any solution S such that, for every element GPi of GP, S is a solution of GPi.

For any solution, the same variable is given the same value everywhere in the set of graph patterns making up the group graph pattern. For example, this query has a group graph pattern of one basic graph pattern as the query pattern.

In a SPARQL query string, a group graph pattern is delimited with braces: {}.

PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
          ?x foaf:name ?name .
          ?x foaf:mbox ?mbox .
The same solutions would be obtained from a query that grouped the triple patterns in basic graph patterns as below:
PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE  { { ?x foaf:name ?name . }
         { ?x foaf:mbox ?mbox . }

4.2 Empty Group Pattern

The group pattern:

{ }

matches any graph (including the empty graph).

@@ Example of SELECT * {}

4.3 Unbound variables

Solutions to graph patterns do not necessarily have to have every variable bound in every solution. SPARQL query patterns are built up from basic patterns which do associate RDF terms with each variable mentioned in the pattern; OPTIONAL and UNION graph patterns can lead to query results where a variable may be bound in some solutions, but not in others.

4.3 Order of Evaluation

There is no implied order of graph patterns within a Group Graph Pattern. Any solution for the group graph pattern that can satisfy all the graph patterns in the group is valid, independently of the order that may be implied by the lexical order of the graph patterns in the group.

5 Including Optional Values

Basic graph patterns allow applications to make queries where the entire query pattern must match for there to be a solution. For every solution of the query, every variable is bound to an RDF Term in a pattern solution. However, regular, complete structures cannot be assumed in all RDF graphs and it is useful to be able to have queries that allow information to be added to the solution where the information is available, but not to have the solution rejected because some part of the query pattern does not match. Optional matching provides this facility; if the optional part does not lead to any solutions, it creates no bindings.

5.1 Optional Pattern Matching

Optional parts of the graph pattern may be specified syntactically with the OPTIONAL keyword applied to a graph pattern:

pattern OPTIONAL { pattern }


@prefix foaf:       <http://xmlns.com/foaf/0.1/> .
@prefix rdf:        <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

_:a  rdf:type        foaf:Person .
_:a  foaf:name       "Alice" .
_:a  foaf:mbox       <mailto:alice@example.com> .
_:a  foaf:mbox       <mailto:alice@work.example> .

_:b  rdf:type        foaf:Person .
_:b  foaf:name       "Bob" .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE  { ?x foaf:name  ?name .
         OPTIONAL { ?x  foaf:mbox  ?mbox }

With the data above, the query result is:

name mbox
"Alice" <mailto:alice@example.com>
"Alice" <mailto:alice@work.example>

There is no value of mbox in the solution where the name is "Bob". It is unbound.

This query finds the names of people in the data. If there is a triple with predicate mbox and same subject, a solution will contain the object of that triple as well. In the example, only a single triple pattern is given in the optional match part of the query but, in general, it is any graph pattern. The whole graph pattern of an optional graph pattern must match for the optional graph pattern to affect the query solution.

The OPTIONAL keyword is left-associative :

pattern OPTIONAL { pattern } OPTIONAL { pattern }

matches the same as:

{ pattern OPTIONAL { pattern } } OPTIONAL { pattern }

5.2 Constraints in Optional Pattern Matching

Constraints can be given in an optional graph pattern as this example shows:

@prefix dc:   <http://purl.org/dc/elements/1.1/> .
@prefix :     <http://example.org/book/> .
@prefix ns:   <http://example.org/ns#> .

:book1  dc:title  "SPARQL Tutorial" .
:book1  ns:price  42 .
:book2  dc:title  "The Semantic Web" .
:book2  ns:price  23 .
PREFIX  dc:  <http://purl.org/dc/elements/1.1/>
PREFIX  ns:  <http://example.org/ns#>
SELECT  ?title ?price
WHERE   { ?x dc:title ?title .
          OPTIONAL { ?x ns:price ?price . FILTER (?price < 30) }
title price
"SPARQL Tutorial"
"The Semantic Web" 23

No price appears for the book with title "SPARQL Tutorial" because the optional graph pattern did not lead to a solution involving the variable "price".

5.3 Multiple Optional Graph Patterns

Graph patterns are defined recursively. A graph pattern may have zero or more optional graph patterns, and any part of a query pattern may have an optional part. In this example, there are two optional graph patterns.


@prefix foaf:       <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name       "Alice" .
_:a  foaf:homepage   <http://work.example.org/alice/> .

_:b  foaf:name       "Bob" .
_:b  foaf:mbox       <mailto:bob@work.example> .


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

Query result:

name mbox hpage
"Alice" <http://work.example.org/alice/>
"Bob" <mailto:bob@work.example>

5.4 Optional Matching – Formal Definition

In an optional match, either an additional graph pattern matches a graph, thereby defining one or more pattern solutions; or it passes the solution without adding any additional bindings.

Definition: Optional Graph Pattern

An optional graph pattern is a combination of a pair of graph patterns. The second pattern modifies pattern solutions of the first pattern but does not fail matching of the overall optional graph pattern.

If Opt(A, B) is an optional graph pattern, where A and B are graph patterns, then S is a solution of optional graph pattern if S is a pattern solution of A and of B otherwise if S is a solution to A, but not to A and B.

The syntactic form:

{ OPTIONAL { pattern } }

is defined to be

{ { } OPTIONAL { pattern } }

5.5 Nested Optional Graph Patterns

Optional patterns can occur inside any group graph pattern, including a group graph pattern which itself is optional, forming a nested pattern. The outer optional graph pattern must match for any nested optional pattern to be matched.


@prefix foaf:       <http://xmlns.com/foaf/0.1/> .
@prefix vcard:      <http://www.w3.org/2001/vcard-rdf/3.0#> .

_:a  foaf:name     "Alice" .
_:a  foaf:mbox     <mailto:alice@work.example> .
_:a  vcard:N       _:x .

_:x  vcard:Family  "Hacker" .
_:x  vcard:Given   "Alice" .

_:b  foaf:name     "Bob" .
_:b  foaf:mbox     <mailto:bob@work.example> .
_:b  foaf:N        _:z .

_:z  vcard:Family  "Hacker" .

_:e  foaf:name     "Ella" .
_:e  vcard:N       _:y .

_:y  vcard:Given   "Eleanor" .


PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?foafName ?mbox ?gname ?fname
  {  ?x foaf:name ?foafName .
     OPTIONAL { ?x foaf:mbox ?mbox } .
     OPTIONAL {  ?x vcard:N ?vc .
                 ?vc vcard:Given ?gname .
                 OPTIONAL { ?vc vcard:Family ?fname }

Query result:

foafName mbox gname fname
"Alice" <mailto:alice@work.example> "Alice" "Hacker"
"Bob" <mailto:bob@work.example>
"Ella" "Eleanor"

This query finds the name, optionally the mbox, and also the vCard given name; further, if there is a vCard Family name as well as the Given name, the query finds that as well.

By nesting the optional pattern involving vcard:Family, the query only matches these if there are appropriate vcard:N and vcard:Given triples in the data. Here the expression is a simple triple pattern on vcard:N but it could be a complex graph pattern with value constraints.

6 Matching Alternatives

SPARQL provides a means of combining graph patterns so that one of several alternative graph patterns may match. If more than one of the alternatives matches, all the possible pattern solutions are found.

6.1 Joining Patterns with UNION

The UNION keyword is the syntax for pattern alternatives.


@prefix dc10:  <http://purl.org/dc/elements/1.0/> .
@prefix dc11:  <http://purl.org/dc/elements/1.1/> .

_:a  dc10:title     "SPARQL Query Language Tutorial" .
_:a  dc10:creator   "Alice" .

_:b  dc11:title     "SPARQL Protocol Tutorial" .
_:b  dc11:creator   "Bob" .

_:c  dc10:title     "SPARQL" .
_:c  dc11:title     "SPARQL (updated)" .


PREFIX dc10:  <http://purl.org/dc/elements/1.0/>
PREFIX dc11:  <http://purl.org/dc/elements/1.1/>

SELECT ?title
WHERE  { { ?book dc10:title  ?title } UNION { ?book dc11:title  ?title } }

Query result:

"SPARQL Protocol Tutorial"
"SPARQL (updated)"
"SPARQL Query Language Tutorial"

This query finds titles of the books in the data, whether the title is recorded using Dublin Core properties from version 1.0 or version 1.1. If the application wishes to know how exactly the information was recorded, then the query:

PREFIX dc10:  <http://purl.org/dc/elements/1.0/>
PREFIX dc11:  <http://purl.org/dc/elements/1.1/>

SELECT ?x ?y
WHERE  { { ?book dc10:title ?x } UNION { ?book dc11:title  ?y } }
x y
"SPARQL (updated)"
"SPARQL Protocol Tutorial"
"SPARQL Query Language Tutorial"

will return results with the variables x or y bound depending on which way the query processor matches the pattern to the data. Note that, unlike an OPTIONAL pattern, if neither part of the UNION pattern matched, then the graph pattern would not match.

The UNION operator combines graph patterns, so more than one triple pattern can be given in each alternative possibility:

PREFIX dc10:  <http://purl.org/dc/elements/1.1/>
PREFIX dc11:  <http://purl.org/dc/elements/1.0/>

SELECT ?title ?author
WHERE  { { ?book dc10:title ?title .  ?book dc10:creator ?author }
         { ?book dc11:title ?title .  ?book dc11:creator ?author }
author title
"Alice" "SPARQL Protocol Tutorial"
"Bob" "SPARQL Query Language Tutorial"

This query will only match a book if it has both a title and creator predicate from the same version of Dublin Core.

6.2 Union Matching – Formal Definition

Definition: Union Graph Pattern

A union graph pattern is a set of graph patterns GPi.

A union graph pattern matches a graph G with solution S if there is some GPi such that GPi matches G with solution S.

Query results involving a pattern containing GP1 and GP2 will include separate solutions for each match where GP1 and GP2 give rise to different sets of bindings.

7 RDF Dataset

The RDF data model expresses information as graphs, consisting of triples with subject, predicate and object.  Many RDF data stores hold multiple RDF graphs, and record information about each graph, allowing an application to make queries that involve information from more than one graph.

A SPARQL query is executed against an RDF Dataset which represents such a collection of graphs. Different parts of the query may be matched against different graphs as described in the next section. There is one graph, the default graph, which does not have a name, and zero or more named graphs, each identified by IRI.

Definition: RDF Dataset

An RDF dataset is a set:
     { G, (<u1>, G1), (<u2>, G2), . . . (<un>, Gn) }
where G and each Gi are graphs, and each <ui> is an IRI. Each <ui> is distinct.

G is called the default graph. (<ui>, Gi) are called named graphs.

There may be no named graphs.

In the previous sections, all queries have been shown executed against a single graph, being the default graph of an RDF dataset. A query does not need to involve the default graph; the query can just involve matching named graphs.

7.1 Examples of RDF Datasets

The definition of RDF Dataset does not restrict the relationships of named and default graphs. Two useful arrangements are:

Example 1:

# Default graph
@prefix dc: <http://purl.org/dc/elements/1.1/> .

<http://example.org/bob>    dc:publisher  "Bob" .
<http://example.org/alice>  dc:publisher  "Alice" .
# Named graph: http://example.org/bob
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Bob" .
_:a foaf:mbox <mailto:bob@oldcorp.example.org> .
# Named graph: http://example.org/alice
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example.org> .

In this example, the default graph contains the names of the publishers of two named graphs. The triples in the named graphs are not visible in the default graph in this example.

Example 2:

RDF data can be combined by RDF merge [RDF-MT] of graphs so that the default graph can be made to include the RDF merge of some or all of the information in the named graphs.

In this next example, the named graphs contain the same triples as before. The RDF dataset includes an RDF merge of the named graphs in the default graph, re-labeling blank nodes to keep them distinct.

# Default graph
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:x foaf:name "Bob" .
_:x foaf:mbox <mailto:bob@oldcorp.example.org> .

_:y foaf:name "Alice" .
_:y foaf:mbox <mailto:alice@work.example.org> .
# Named graph: http://example.org/bob
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Bob" .
_:a foaf:mbox <mailto:bob@oldcorp.example.org> .
# Named graph: http://example.org/alice
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example> .

8 Querying the Dataset

When querying a collection of graphs, the GRAPH keyword is used to match patterns against named graphs. This is by either using an IRI to select a graph or using a variable to range over the IRIs naming graphs.

Definition: RDF Dataset Graph Pattern

If D is a dataset {G, (<u1>, G1), ... }, and P is a graph pattern then S is a pattern solution of RDF Dataset Graph Pattern GRAPH(g, P) if either of:

  1. g is an IRI where g = <ui> for some i, and S is pattern solution of P on dataset {Gi, (<u1>, G1), ...}
  2. g is a variable, S maps the variable g to <uj>, where <uj> is an IRI from a named graph of D, and S is a pattern solution of P on dataset {Gj, (<u1>, G1), ...}

The following two graphs will be used in examples:

# Named graph: http://example.org/foaf/aliceFoaf
@prefix  foaf:  <http://xmlns.com/foaf/0.1/> .
@prefix  rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix  rdfs:   <http://www.w3.org/2000/01/rdf-schema#> .

_:a  foaf:name     "Alice" .
_:a  foaf:mbox     <mailto:alice@work.example> .
_:a  foaf:knows    _:b .

_:b  foaf:name     "Bob" .
_:b  foaf:mbox     <mailto:bob@work.example> .
_:b  foaf:nick     "Bobby" .
_:b  rdfs:seeAlso  <http://example.org/foaf/bobFoaf> .

     rdf:type      foaf:PersonalProfileDocument .
# Named graph: http://example.org/foaf/bobFoaf
@prefix  foaf:  <http://xmlns.com/foaf/0.1/> .
@prefix  rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix  rdfs:   <http://www.w3.org/2000/01/rdf-schema#> .

_:z  foaf:mbox     <mailto:bob@work.example> .
_:z  rdfs:seeAlso  <http://example.org/foaf/bobFoaf> .
_:z  foaf:nick     "Robert" .
     rdf:type      foaf:PersonalProfileDocument .

8.1 Accessing Graph Names

The query below matches the graph pattern on each of the named graphs in the dataset and forms solutions which have the src variable bound to IRIs of the graph being matched.

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?src ?bobNick
    GRAPH ?src
    { ?x foaf:mbox <mailto:bob@work.example> .
      ?x foaf:nick ?bobNick

The query result gives the name of the graphs where the information was found and the value for Bob's nick:

src bobNick
<http://example.org/foaf/aliceFoaf> "Bobby"
<http://example.org/foaf/bobFoaf> "Robert"

8.2 Restricting by Graph IRI

The query can restrict the matching applied to a specific graph by supplying the graph IRI.  This query looks for Bob's nick as given in the graph http://example.org/foaf/bobFoaf.

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX data: <http://example.org/foaf/>

SELECT ?nick
     GRAPH data:bobFoaf {
         ?x foaf:mbox <mailto:bob@work.example> .
         ?x foaf:nick ?nick }

which yields a single solution:


8.3 Restricting by Bound Variables

A variable used in the GRAPH clause may also be used in another GRAPH clause or in a graph pattern matched against the default graph in the dataset.

This can be used to find information in one part of a query, and thus restrict the graphs matched in another part of the query. The query below uses the graph with IRI http://example.org/foaf/aliceFoaf to find the profile document for Bob; it then matches another pattern against that graph. The pattern in the second GRAPH clause finds the blank node (variable w) for the person with the same mail box (given by variable mbox) as found in the first GRAPH clause (variable whom), because the blank node used to match for variable whom from Alice's FOAF file is not the same as the blank node in the profile document (they are in different graphs).

PREFIX  data:  <http://example.org/foaf/>
PREFIX  foaf:  <http://xmlns.com/foaf/0.1/>
PREFIX  rdfs:  <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?mbox ?nick ?ppd
  GRAPH data:aliceFoaf
    ?alice foaf:mbox <mailto:alice@work.example> ;
           foaf:knows ?whom .
    ?whom  foaf:mbox ?mbox ;
           rdfs:seeAlso ?ppd .
    ?ppd  a foaf:PersonalProfileDocument .
  } .
  GRAPH ?ppd
      ?w foaf:mbox ?mbox ;
         foaf:nick ?nick
mbox nick ppd
<mailto:bob@work.example> "Robert" <http://example.org/foaf/bobFoaf>

Any triple in Alice's FOAF file giving Bob's nick is not used to provide a nick for Bob because the pattern involving variable nick is restricted by ppd to a particular Personal Profile Document.

8.4 Named and Default Graphs

Query patterns can involve both the default graph and the named graphs. In this example, an aggregator has read in a Web resource on two different occasions.  Each time a graph is read into the aggregator, it is given an IRI by the local system. The graphs are nearly the same but the email address for "Bob" has changed.

The default graph is being used to record the provenance information and the RDF data actually read is kept in two separate graphs, each of which is given a different IRI by the system. The RDF dataset consists of two named graphs and the information about them.

RDF Dataset:

# Default graph
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix g:  <tag:example.org,2005-06-06:> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

g:graph1 dc:publisher "Bob" .
g:graph1 dc:date "2004-12-06"^^xsd:date .

g:graph2 dc:publisher "Bob" .
g:graph2 dc:date "2005-01-10"^^xsd:date .
# Graph: locally allocated IRI: tag:example.org,2005-06-06:graph1
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example> .

_:b foaf:name "Bob" .
_:b foaf:mbox <mailto:bob@oldcorp.example.org> .
# Graph: locally allocated IRI: tag:example.org,2005-06-06:graph2
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example> .

_:b foaf:name "Bob" .
_:b foaf:mbox <mailto:bob@newcorp.example.org> .

This query finds email addresses, detailing the name of the person and the date the information was discovered.

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

SELECT ?name ?mbox ?date
  {  ?g dc:publisher ?name ;
        dc:date ?date .
    GRAPH ?g
      { ?person foaf:name ?name ; foaf:mbox ?mbox }

The results show that the email address for "Bob" has changed.

name mbox date
"Bob" <mailto:bob@oldcorp.example.org> "2004-12-06"^^xsd:date
"Bob" <mailto:bob@newcorp.example.org> "2005-01-10"^^xsd:date

The IRI for the date datatype has been abbreviated in the results for clarity.

9 Specifying RDF Datasets

A SPARQL query may specify the dataset to be used for matching.  The FROM clauses give IRIs that the query processor can use to create the default graph and the FROM NAMED clause can be used to specify named graphs. The RDF dataset may also be specified in a SPARQL protocol request, in which case the protocol description overrides any description in the query itself. A query service may refuse a query request if the dataset description is not acceptable to the service.

A query processor may use these IRIs in any way to associate an RDF Dataset with a query. For example, it could use IRIs to retrieve documents, parse them and use the resulting triples as one of the graphs; alternatively, it might only service queries that specify IRIs of graphs that it already has stored.

The FROM and FROM NAMED keywords allow a query to specify an RDF dataset by reference; they indicate that the dataset should include graphs that are obtained from representations of the resources identified by the given IRIs (i.e. the absolute form of the given IRI references). The dataset resulting from a number of FROM and FROM NAMED clauses is:

If a query provides such a dataset description, then it is used in place of any dataset that the query service would use if no dataset description is provided in a query.

9.1 Specifying the Default Graph

Each FROM clause contains an IRI that indicates the graph to be used to form the default graph. This does not put the graph in as a named graph; a query can do this by also specifying the graph in the FROM NAMED clause.

In this example, there is a single default graph:

# Default graph (stored at http://example.org/foaf/aliceFoaf)
@prefix  foaf:  <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name     "Alice" .
_:a  foaf:mbox     <mailto:alice@work.example> .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT  ?name
FROM    <http://example.org/foaf/aliceFoaf>
WHERE   { ?x foaf:name ?name }

If a query provides more than one FROM clause, providing more than one IRI to indicate the default graph, then the default graph is based on the RDF merge of the graphs obtained from representations of the resources identified by the given IRIs.

9.2 Specifying Named Graphs

A query can supply IRIs for the named graphs in the RDF Dataset using the FROM NAMED clause.  Each IRI is used to provide one named graph in the RDF Dataset. Using an IRI in two or more FROM NAMED clauses results in one named graph with that IRI appearing in the dataset.

# Graph: http://example.org/bob
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Bob" .
_:a foaf:mbox <mailto:bob@oldcorp.example.org> .
# Graph: http://example.org/alice
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example> .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?src ?name

FROM NAMED <http://example.org/alice>
FROM NAMED <http://example.org/bob>

{ GRAPH ?src { ?x foaf:name ?name } }
src name
<http://example.org/bob> "Bob"
<http://example.org/alice> "Alice"

The FROM NAMED syntax suggests that the IRI identifies the corresponding graph, but actually the relationship between a URI and a graph in an RDF dataset is indirect. The IRI identifies a resource, and the resource is represented by a graph (or, more precisely: by a document that serializes a graph). For further details see [WEBARCH].

9.3 Combining FROM and FROM NAMED

The FROM clause and FROM NAMED clauses can be used in the same query.

# Default graph (stored at http://example.org/dft.ttl)
@prefix dc: <http://purl.org/dc/elements/1.1/> .

<http://example.org/bob>    dc:publisher  "Bob Hacker" .
<http://example.org/alice>  dc:publisher  "Alice Hacker" .
# Named graph: http://example.org/bob
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Bob" .
_:a foaf:mbox <mailto:bob@oldcorp.example.org> .
# Named graph: http://example.org/alice
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example.org> .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>

SELECT ?who ?g ?mbox
FROM <http://example.org/dft.ttl>
FROM NAMED <http://example.org/alice>
FROM NAMED <http://example.org/bob>
   ?g dc:publisher ?who .
   GRAPH ?g { ?x foaf:mbox ?mbox }
who g mbox
"Bob Hacker" <http://example.org/bob> <mailto:bob@oldcorp.example.org>
"Alice Hacker" <http://example.org/alice> <mailto:alice@work.example.org>

This query finds the  mbox together with the information in the default graph about the publisher. <http://example.org/dft.ttl> is just the IRI used to form the default graph, not it's name.

10 Query Result Forms

SPARQL has four query result forms. These result forms use the solutions from pattern matching to form result sets or RDF graphs. The query result forms are:

Returns all, or a subset of, the variables bound in a query pattern match.
Returns an RDF graph constructed by substituting variables in a set of triple templates.
Returns an RDF graph that describes the resources found.
Returns a boolean indicating whether a query pattern matches or not.

The SPARQL Variable Binding Results XML Format can be used to serialize result sets from a SELECT query or the boolean result of an ASK query.

10.1 Solution Sequences and Result Forms

Query patterns generate an unordered collection of solutions, each solution being a function from variables to RDF terms. These solutions are then treated as a sequence, initially in no specific order; any sequence modifiers are then applied to create another sequence. Finally, this latter sequence is used to generate one of the SPARQL result forms.

Definition: Solution Sequence

A solution sequence S is a list of solutions.

    S = ( S0, S1, . . . , Sn)

The solution sequence from matching the query pattern is a collection formed from the solutions of the query pattern with no defined order.

Definition: Solution Sequence Modifier

A solution sequence modifier is one of:

If SM is the set of modifiers, and QS is the collection of solutions of a query, we write SM(QS) for the sequence formed by applying SM to the solution sequence formed from QS.

Definition: Result Forms

The result form of a query is one of

The elements of a sequence of solutions can be modified by:

  1. ORDER BY: put the solutions in order
  2. Projection
  3. DISTINCT: ensure solutions in the sequence are unique
  4. OFFSET: control where the solutions processed start from in the overall sequence of solutions
  5. LIMIT: restrict the number of solutions processed for query results

applied in the order given by the list.

10.1.1 ORDER BY

The ORDER BY clause takes a solution sequence and applies ordering conditions. An ordering condition can be a variable, a function call or an expression. The direction of ordering is ascending by default. It can be explicitly set to ascending or descending by enclosing the condition in ASC() or DESC() respectively. If multiple conditions are given, then they are applied in turn until one gives the indication of the ordering.

PREFIX foaf:    <http://xmlns.com/foaf/0.1/>

SELECT ?name
WHERE { ?x foaf:name ?name }
ORDER BY ?name
PREFIX     :    <http://example.org/ns#>
PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
PREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>

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

SELECT ?name
WHERE { ?x foaf:name ?name ; :empId ?emp }
ORDER BY ?name DESC(?emp)

Using ORDER BY on a solution sequence for a result form other than SELECT has no direct effect because only SELECT returns a sequence of results. In combination with LIMIT and OFFSET, it can be used to return partial results.

Definition: Ordered Solution Sequence

An ordered solution sequence is a solution sequence where the sequence is partially ordered with respect to some ordering condition.

A solution sequence S = ( S0, S1, . . . , Sn) is ordered with respect to an ordering condition C if, for Si, Sj, then i < j if C orders Si before Sj.

An ordering condition need not give a total ordering of a solution sequence.

The "<" operator (see the Operator Mapping) defines the relative order of pairs of numerics, xsd:dateTimes and xsd:strings.

IRIs are ordered by comparing the character strings making up each IRI using the "<" operator.

SPARQL also defines a fixed, arbitrary order between some kinds of RDF terms that would not otherwise be ordered. This arbitrary order is necessary to provide slicing of query solutions by use of LIMIT and OFFSET.

  1. (Lowest) no value assigned to the variable or expression in this solution.
  2. Blank nodes
  3. IRIs
  4. RDF literals
  5. A plain literal is lower than an RDF literal with type xsd:string of  the same lexical form.

If the ordering criteria do not specify the order of values, then the ordering in the solution sequence is undefined.

Ordering a sequence of solutions always results in a sequence with the same number of solutions in it, even if the ordering criteria does not differentiate between two solutions.

10.1.2 Projection

The solution sequence can be transformed into one involving only a subset of the variables. For each solution in the sequence, a new solution is formed using a specified selection of the variables.

Definition: Projection

The projection of solution QS over a set of variables VS is the solution
      project(QS, VS) = { (v, QS(v)) | v in VS }

For a solution sequence S = ( S0, Sa, . . . , Sn) and a finite set of variables VS,
    project(S, VS) = { (project(Si, VS) | i = 0, 1, . . . n }

The following example shows a query to extract just the names of people described in an RDF graph using FOAF properties.

@prefix foaf:        <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name       "Alice" .
_:a  foaf:mbox       <mailto:alice@work.example> .

_:b  foaf:name       "Bob" .
_:b  foaf:mbox       <mailto:bob@work.example> .
PREFIX foaf:       <http://xmlns.com/foaf/0.1/>
SELECT ?name
 { ?x foaf:name ?name }


The solution sequence can be modified by adding the DISTINCT keyword which ensures that every combination of variable bindings (i.e. each solution) in the sequence is unique.

@prefix  foaf:  <http://xmlns.com/foaf/0.1/> .

_:a    foaf:name   "Alice" .
_:a    foaf:mbox   <mailto:alice@org> .

_:z    foaf:name   "Alice" .
_:z    foaf:mbox   <mailto:smith@work> .
PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?name WHERE { ?x foaf:name ?name }

If DISTINCT and LIMIT or OFFSET are specified, then duplicates are eliminated before the limit or offset is applied.

Definition: Distinct Solution Sequence

A Distinct Solution Sequence is a solution sequence in which no two solutions are the same.

10.1.4 OFFSET

OFFSET causes the solutions generated to start after the specified number of solutions. An OFFSET of zero has no effect.

The order in which solutions are returned is initially undefined. Using LIMIT and OFFSET to select different subsets of the query solutions will not be useful unless the order is made predictable by using ORDER BY.

PREFIX foaf:    <http://xmlns.com/foaf/0.1/>

SELECT  ?name
WHERE   { ?x foaf:name ?name }
ORDER BY ?name
Definition: Offset Solution Sequence

An Offset Solution Sequence with respect to another solution sequence S, is one which starts at a given index of S.

For solution sequence S = (S0, S1, . . . , Sn), the offset solution sequence
offset(S, k), k >= 0  is
     (Sk, Sk+1, . . ., Sn) if n >= k
     (), the empty sequence, if k > n

10.1.5 LIMIT

The LIMIT form puts an upper bound on the number of solutions returned. If the number of actual solutions is greater than the limit, then at most the limit number of solutions will be returned.

PREFIX foaf:    <http://xmlns.com/foaf/0.1/>

SELECT ?name
WHERE { ?x foaf:name ?name }

A limit of 0 would cause no results to be returned. A limit may not be negative.

Definition: Limited Solution Sequence

A Limited Solution Sequence has at most a given, fixed number of members.

The limit of solution sequence S = (S0, S1, . . . , Sn) is

limit(S, m) =
        (S0, S1, . . . , Sm-1) if n > m
        (S0, S1, . . . , Sn)  if n <= m-1

10.2 Selecting Variables

The SELECT form of results returns the variables directly. The syntax SELECT * is an abbreviation that selects all of the variables.

@prefix  foaf:  <http://xmlns.com/foaf/0.1/> .

_:a    foaf:name   "Alice" .
_:a    foaf:knows  _:b .
_:a    foaf:knows  _:c .

_:b    foaf:name   "Bob" .

_:c    foaf:name   "Clare" .
_:c    foaf:nick   "CT" .

PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
SELECT ?nameX ?nameY ?nickY
  { ?x foaf:knows ?y ;
       foaf:name ?nameX .
    ?y foaf:name ?nameY .
    OPTIONAL { ?y foaf:nick ?nickY }
nameX nameY nickY
"Alice" "Bob"
"Alice" "Clare" "CT"

Results can be thought of as a table or result set, with one row per query solution. Some cells may be empty because a variable is not bound in that particular solution.

Result sets can be accessed by the local API but also can be serialized into either XML or an RDF graph. An XML format is described in SPARQL Query Results XML Format, and this gives:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
    <variable name="nameX"/>
    <variable name="nameY"/>
    <variable name="nickY"/>
      <binding name="nameX">
      <binding name="nameY">
      <binding name="nameX">
      <binding name="nameY">
      <binding name="nickY">

Definition: SELECT

Given Q = (GP, DS, SM, SELECT VS) where

then, if QS is the set of solutions formed by matching dataset DS with graph pattern GP, the SELECT result is project(SM(QS), VS)

10.3 Constructing an Output Graph

The CONSTRUCT result form returns a single RDF graph specified by a graph template. The result is an RDF graph formed by taking each query solution in the solution sequence, substituting for the variables into the graph template and combining the triples into a single RDF graph by set union.

If any such instantiation produces a triple containing an unbound variable, or an illegal RDF construct (such as a literal in subject or predicate position), then that triple is not included in the output RDF graph. The graph template can contain ground or explicit triples, that is, triples with no variables, and these also appear in the output RDF graph returned by the CONSTRUCT query form.

@prefix  foaf:  <http://xmlns.com/foaf/0.1/> .

_:a    foaf:name   "Alice" .
_:a    foaf:mbox   <mailto:alice@example.org> .
PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
PREFIX vcard:   <http://www.w3.org/2001/vcard-rdf/3.0#>
CONSTRUCT   { <http://example.org/person#Alice> vcard:FN ?name }
WHERE       { ?x foaf:name ?name }

creates vcard properties from the FOAF information:

@prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> .

<http://example.org/person#Alice> vcard:FN "Alice" .

10.3.1 Templates with Blank Nodes

A template can create an RDF graph containing blank nodes. The blank node labels are scoped to the template for each solution. If the same label occurs twice in a template, then there will be one blank node created for each query solution but there will be different blank nodes across triples generated by different query solutions.

@prefix  foaf:  <http://xmlns.com/foaf/0.1/> .

_:a    foaf:givenname   "Alice" .
_:a    foaf:family_name "Hacker" .

_:b    foaf:firstname   "Bob" .
_:b    foaf:surname     "Hacker" .
PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
PREFIX vcard:   <http://www.w3.org/2001/vcard-rdf/3.0#>

CONSTRUCT { ?x  vcard:N _:v .
            _:v vcard:givenName ?gname .
            _:v vcard:familyName ?fname }
    { ?x foaf:firstname ?gname } UNION  { ?x foaf:givenname   ?gname } .
    { ?x foaf:surname   ?fname } UNION  { ?x foaf:family_name ?fname } .

creates vcard properties corresponding to the FOAF information:

@prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> .

_:v1 vcard:N         _:x .
_:x vcard:givenName  "Alice" .
_:x vcard:familyName "Hacker" .

_:v2 vcard:N         _:z .
_:z vcard:givenName  "Bob" .
_:z vcard:familyName "Hacker" .

The use of variable ?x in the template, which in this example will be bound to blank nodes (which have labels _:a and _:b in the data) causes different blank node labels (_:v1 and _:v2) as shown by the results.

10.3.2 Accessing Graphs in the RDF Dataset

Using CONSTRUCT it is possible to extract parts or the whole of graphs from the target RDF dataset. This first example returns the graph (if it is in the dataset) with IRI label http://example.org/aGraph; otherwise, it returns an empty graph.

CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/aGraph> { ?s ?p ?o } . }

The access to the graph can be conditional on other information.  Suppose the default graph contains metadata about the named graphs in the dataset, then a query like the following one can extract one graph based on information about the named graph:

PREFIX  dc: <http://purl.org/dc/elements/1.1/>
PREFIX app: <http://example.org/ns#>
CONSTRUCT { ?s ?p ?o } WHERE
   GRAPH ?g { ?s ?p ?o } .
   { ?g dc:publisher <http://www.w3.org/> } .
   { ?g dc:date ?date } .
   FILTER ( app:customDate(?date) > "2005-02-28T00:00:00Z"^^xsd:dateTime ) .

where app:customDate identified an extension function to turn the data format into an xsd:dateTime RDF Term.

Definition: Graph Template

A graph template is a set of triple patterns.

If T = { tj | j = 1,2 ... m } is a graph template and S is a pattern solution then SC(S, tj) is a set of one RDF triple S(tj) if all variables in tj are in the domain of S. SC(S, tj) is the empty set otherwise.

Write SC(S, T) for the union of SC(S, tj).

Definition: CONSTRUCT

Let Q = (GP, DS, SM, CONSTRUCT T) where

then, if QS is the set of solutions formed by matching dataset DS with graph pattern GP, then write SM(QS) = { Si | i = 1,2 ... n }.

Let Ti be a sequence of sets of triple patterns, such that each Ti is basic graph pattern equivalent to T and no Ti have a blank node in common and no Ti has a blank node in common with any blank node in SM(QS).

Let Ri be the RDF graph formed from SC(Si, Ti).

The CONSTRUCT result is the RDF graph formed by the union of Ri.

10.3.3 Solution Modifiers and CONSTRUCT

The solution modifiers of a query affect the results of a CONSTRUCT query. In this example, the output graph from the CONSTRUCT template is formed from just 2 of the solutions from graph pattern matching. The query outputs a graph with the names of the people with the top 2 sites, rated by hits.  The triples in the RDF graph are not ordered.

@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix site: <http://example.org/stats#> .

_:a foaf:name "Alice" .
_:a site:hits 2349 .

_:b foaf:name "Bob" .
_:b site:hits 105 .

_:c foaf:name "Eve" .
_:c site:hits 181 .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX site: <http://example.org/stats#>

CONSTRUCT { [] foaf:name ?name }
{ [] foaf:name ?name ;
     site:hits ?hits .
ORDER BY desc(?hits)
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:x foaf:name "Alice" .
_:y foaf:name "Eve" .

10.4 Descriptions of Resources (Non-normative)

Current conventions for DESCRIBE return an RDF graph without any specified constraints. Future SPARQL specifications may further constrain the results of DESCRIBE, rendering some currently valid DESCRIBE responses invalid. As with any query, a service may refuse to serve a DESCRIBE query.

The DESCRIBE form returns a single result RDF graph containing RDF data about resources. This data is not prescribed by a SPARQL query, where the query client would need to know the structure of the RDF in the data source, but, instead, is determined by the SPARQL query processor. The query pattern is used to create a result set. The DESCRIBE form takes each of the resources identified in a solution, together with any resources directly named by IRI, and assembles a single RDF graph by taking a "description" from the target knowledge base. The description is determined by the query service.

10.4.1 Explicit IRIs

The DESCRIBE clause itself can take IRIs to identify the resources. The simplest DESCRIBE query is just an IRI in the DESCRIBE clause:

DESCRIBE <http://example.org/>

10.4.2 Identifying Resources

The resources can also be a query variable from a result set. This enables description of resources whether they are identified by IRI or by blank node in the dataset:

PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
WHERE    { ?x foaf:mbox <mailto:alice@org> }

The property foaf:mbox is defined as being an inverse function property in the FOAF vocabulary. If treated as such, this query will return information about at most one person. If, however, the query pattern has multiple solutions, the RDF data for each is the union of all RDF graph descriptions.

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

More than one IRI or variable can be given:

PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
DESCRIBE ?x ?y <http://example.org/>
WHERE    {?x foaf:knows ?y}

10.4.3 Descriptions of Resources

The RDF returned is determined by the information publisher. It is the useful information the service has about a resource. It may include information about other resources: the RDF data for a book may also include details about the author.

A simple query such as

PREFIX ent:  <http://org.example.com/employees#>
DESCRIBE ?x WHERE { ?x ent:employeeId "1234" }

might return a description of the employee and some other potentially useful details:

@prefix foaf:   <http://xmlns.com/foaf/0.1/> .
@prefix vcard:  <http://www.w3.org/2001/vcard-rdf/3.0> .
@prefix exOrg:  <http://org.example.com/employees#> .
@prefix rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix owl:    <http://www.w3.org/2002/07/owl#>

_:a     exOrg:employeeId    "1234" ;
        foaf:mbox_sha1sum   "ABCD1234" ;
         [ vcard:Family       "Smith" ;
           vcard:Given        "John"  ] .

foaf:mbox_sha1sum  rdf:type  owl:InverseFunctionalProperty .

which includes the blank node closure for the vcard vocabulary vcard:N. Other possible mechanisms for deciding what information to return include Concise Bounded Descriptions [CBD].

For a vocabulary such as FOAF, where the resources are typically blank nodes, returning sufficient information to identify a node such as the InverseFunctionalProperty foaf:mbox_sha1sum as well information like name and other details recorded would be appropriate. In the example, the match to the WHERE clause was returned but this is not required.

Definition: DESCRIBE

Let Q = (GP, DS, SM, DESCRIBE V) where

then, if QS is the set of solutions formed by matching dataset DS with graph pattern GP, the DESCRIBE result is an RDF graph formed by information relating elements of
distinct(U union project(SM(QS), VS)).

This definition intentionally does not prescribe the nature of the relevant information.

10.5 Asking "yes or no" questions

Applications can use the ASK form to test whether or not a query pattern has a solution. No information is returned about the possible query solutions, just whether the server can find one or not.

@prefix foaf:       <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name       "Alice" .
_:a  foaf:homepage   <http://work.example.org/alice/> .

_:b  foaf:name       "Bob" .
_:b  foaf:mbox       <mailto:bob@work.example> .
PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
ASK  { ?x foaf:name  "Alice" }

The SPARQL Query Results XML Format form of this result set gives:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">

On the same data, the following returns no match because Alice's mbox is not mentioned.

PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
ASK  { ?x foaf:name  "Alice" ;
          foaf:mbox  <mailto:alice@work.example> }

Definition: ASK

Let Q = (GP, DS, SM, ASK) where

and QS is the set of solutions formed by matching dataset DS with graph pattern GP then the ASK result is true if SM(QS) is not empty, otherwise it is false.

11 Testing Values

SPARQL FILTERs restrict the set of solutions according to a given expression. Specifically, FILTERs eliminate any solutions that, when substituted into the expression, result in either an effective boolean value of false or produce an error. Effective boolean values are defined in section 11.2.2 Effective Boolean Value, errors are defined in XQuery 1.0: An XML Query Language [XQUERY] section 2.3.1, Kinds of Errors.

SPARQL expressions are constructed according to the grammar and provide access to functions (named by IRI) and operations (keywords in the SPARQL grammar). The datatypes of the operands of these functions and operators are the subset of the XML Schema datatypes {xsd:string, xsd:decimal, xsd:float, xsd:double, xsd:boolean, xsd:dateTime} and types derived from xsd:decimal {xsd:integer; xsd:nonPositiveInteger; xsd:negativeInteger; xsd:long; xsd:int; xsd:short; xsd:byte; xsd:nonNegativeInteger; xsd:unsignedLong; xsd:unsignedInt; xsd:unsignedShort; xsd:unsignedByte; xsd:positiveInteger}.

The SPARQL operations are listed in section 11.3 and are associated with their productions in the grammar. In addition, SPARQL imports a subset of the XPath casting functions, listed in section 11.5, which are invokable by name within a SPARQL query. These functions and operators are taken from the XQuery 1.0 and XPath 2.0 Functions and Operators [FUNCOP].

As stated above, RDF Terms are made of IRIs, Literals and Blank Nodes. RDF Literals may have a datatype IRI:

@prefix a:          <http://www.w3.org/2000/10/annotation-ns#> .
@prefix dc:         <http://purl.org/dc/elements/1.1/> .

_:a   a:annotates   <http://www.w3.org/TR/rdf-sparql-query/> .
_:a   dc:date       "2004-12-31T19:00:00-05:00" .

_:b   a:annotates   <http://www.w3.org/TR/rdf-sparql-query/> .
_:b   dc:date       "2004-12-31T19:01:00-05:00"^^<http://www.w3.org/2001/XMLSchema#dateTime> .

The first dc:date arc has no type information. The second has the datatype xsd:dateTime. SPARQL operators compare the values of typed literals:

PREFIX a:      <http://www.w3.org/2000/10/annotation-ns#>
PREFIX dc:     <http://purl.org/dc/elements/1.1/>
PREFIX xsd:    <http://www.w3.org/2001/XMLSchema#>

SELECT ?annot
WHERE { ?annot  a:annotates  <http://www.w3.org/TR/rdf-sparql-query/> .
        ?annot  dc:date      ?date .
        FILTER ( ?date > "2005-01-01T00:00:00Z"^^xsd:dateTime ) }

Literals may be cast to typed literals in order to use the SPARQL operators.

... FILTER ( xsd:dateTime(?date) < xsd:dateTime("2005-01-01T00:00:00Z") ) ...

The following typographical conventions are used in this section:

11.1 Operand Data Types

SPARQL imports a subset of the XPath functions and operators. RDF typed literals passed as arguments to these functions and operators are mapped to XML Schema typed values with a string value of the lexical form and an atomic datatype corresponding to the datatype IRI. The returned typed values are mapped back to RDF typed literals.

SPARQL introduces additional operators which operate on RDF terms. The following terms are imported from Resource Description Framework (RDF): Concepts and Abstract Syntax [CONCEPTS]:

When referring to a type, the following terms denote a typed literal with the corresponding XML Schema [XSDT] datatype IRI:

When referring to a type, the following terms denote a typed literal with the corresponding RDF Schema [RDFS] datatype IRI:

The following terms identify additional types used in SPARQL value tests:

11.2 Filter Evaluation

SPARQL provides a subset of the functions and operators defined by XQuery Operator Mapping. XQuery 1.0 section 2.2.3 Expression Processing describes the invocation of XPath functions. The following rules accommodate the differences in the data and execution models between XQuery and SPARQL:

The logical-and and logical-or truth table for true (T), false (F), and error (E) is as follows:

ABA || BA && B

11.2.1 Invocation

SPARQL defines a syntax for invoking functions and operators on a list of arguments. These are invoked as follows:

If any of these steps fails, the invocation generates an error. The effects of errors are defined in Filter Evaluation.

11.2.2 Effective Boolean Value (EBV)

The XQuery Effective Boolean Value rules rely on the definition of XPath's fn:boolean. The following rules reflect the rules for fn:boolean applied to the argument types present in SPARQL Queries:

An EBV of true is represented as a typed literal with a datatype of xsd:boolean and a lexical value of "true"; an EBV of false is represented with a lexical value of "false".

[Informative: Effective boolean value is used to calculate the arguments to the logical functions logical-and, logical-or, and fn:not, as well as evaluate the result of a filter.]

11.3 Operator Mapping

The SPARQL grammar identifies a set of operators (for instance, &&, *, isIRI) used to construct constraints. The following table associates each of these grammatical productions with an operator defined either by XPath or the SPARQL operators specified in section 11.4. When selecting the operator definition for a given set of parameters, the definition with the most specific parameters applies. For instance, when evaluating xsd:integer = xsd:signedInt, the definition for = with two numeric parameters applies, rather than the one with two RDF terms. The table is arranged so that upper-most viable candiate is the most specific.

See the XPath Operator Mapping for the definition of all operators under the heading "XQuery Tests", respecting numeric type promotions and subtype substitution for arguments to numeric operators. Some of the operators are associated with nested function expressions, e.g. fn:not(op:numeric-equal(A, B)). Note that per the XPath definitions, fn:not and op:numeric-equal produce an error if their argument is an error.

The collation for fn:compare is defined by XPath and identified by http://www.w3.org/2005/xpath-functions/collation/codepoint. This collation allows for string comparison based on code point values. Codepoint string equivilence can be tested with RDF term equivilence.

SPARQL Unary Operators
Operator Type(A)FunctionResult type
XQuery Tests
! A xsd:booleanfn:not(A)xsd:boolean
+ A numericop:numeric-unary-plus(A)numeric
- A numericop:numeric-unary-minus(A)numeric
SPARQL Tests, defined in section 11.4
BOUND(A) variableisBound(A)xsd:boolean
RDF termisIRI(A)xsd:boolean
isBLANK(A) RDF termisBlank(A)xsd:boolean
isLITERAL(A) RDF termisLiteral(A)xsd:boolean
SPARQL Accessors
STR(A) literalstr(A)simple literal
STR(A) IRIstr(A)simple literal
LANG(A) literallang(A)simple literal
DATATYPE(A) typed literaldatatype(A)rdfs:Datatype
SPARQL Binary Operators
Operator Type(A)Type(B)FunctionResult type
Logical Connectives, defined in section 11.4
A || B xsd:booleanxsd:booleanlogical-or(A, B)xsd:boolean
A && B xsd:booleanxsd:booleanlogical-and(A, B)xsd:boolean
XPath Tests
A = B numericnumericop:numeric-equal(A, B)xsd:boolean
A = B xsd:booleanxsd:booleanop:boolean-equal(A, B)xsd:boolean
A = B xsd:dateTimexsd:dateTimeop:dateTime-equal(A, B)xsd:boolean
A != B numericnumericfn:not(op:numeric-equal(A, B))xsd:boolean
A != B xsd:booleanxsd:booleanfn:not(op:boolean-equal(A, B))xsd:boolean
A != B xsd:dateTimexsd:dateTimefn:not(op:dateTime-equal(A, B))xsd:boolean
A < B numericnumericop:numeric-less-than(A, B)xsd:boolean
A < B simple literalsimple literalop:numeric-equal(fn:compare(A, B), -1)xsd:boolean
A < B xsd:stringxsd:stringop:numeric-equal(fn:compare(STR(A), STR(B)), -1)xsd:boolean
A < B xsd:booleanxsd:booleanop:boolean-less-than(A, B)xsd:boolean
A < B xsd:dateTimexsd:dateTimeop:dateTime-less-than(A, B)xsd:boolean
A > B numericnumericop:numeric-greater-than(A, B)xsd:boolean
A > B simple literalsimple literalop:numeric-equal(fn:compare(A, B), 1)xsd:boolean
A > B xsd:stringxsd:stringop:numeric-equal(fn:compare(STR(A), STR(B)), 1)xsd:boolean
A > B xsd:booleanxsd:booleanop:boolean-greater-than(A, B)xsd:boolean
A > B xsd:dateTimexsd:dateTimeop:dateTime-greater-than(A, B)xsd:boolean
A <= B numericnumericlogical-or(op:numeric-less-than(A, B), op:numeric-equal(A, B))xsd:boolean
A <= B simple literalsimple literalfn:not(op:numeric-equal(fn:compare(A, B), 1))xsd:boolean
A <= B xsd:stringxsd:stringfn:not(op:numeric-equal(fn:compare(STR(A), STR(B)), 1))xsd:boolean
A <= B xsd:booleanxsd:booleanfn:not(op:boolean-greater-than(A, B))xsd:boolean
A <= B xsd:dateTimexsd:dateTimefn:not(op:dateTime-greater-than(A, B))xsd:boolean
A >= B numericnumericlogical-or(op:numeric-greater-than(A, B), op:numeric-equal(A, B))xsd:boolean
A >= B simple literalsimple literalfn:not(op:numeric-equal(fn:compare(A, B), -1))xsd:boolean
A >= B xsd:stringxsd:stringfn:not(op:numeric-equal(fn:compare(STR(A), STR(B)), -1))xsd:boolean
A >= B xsd:booleanxsd:booleanfn:not(op:boolean-less-than(A, B))xsd:boolean
A >= B xsd:dateTimexsd:dateTimefn:not(op:dateTime-less-than(A, B))xsd:boolean
A * B numericnumericop:numeric-multiply(A, B)numeric
A / B numericnumericop:numeric-divide(A, B)numeric; but xsd:decimal if both operands are xsd:integer
A + B numericnumericop:numeric-add(A, B)numeric
A - B numericnumericop:numeric-subtract(A, B)numeric
SPARQL Tests, defined in section 11.4
A = B RDF termRDF termRDFterm-equal(A, B)xsd:boolean
A != B RDF termRDF termfn:not(RDFterm-equal(A, B))xsd:boolean
langMATCHES(A, B) simple literalsimple literallangMatches(A, B)xsd:boolean
REGEX(STRING, PATTERN) simple literalsimple literalfn:matches(STRING, PATTERN)xsd:boolean
SPARQL Trinary Operators
Operator Type(A)Type(B)Type(C)FunctionResult type
SPARQL Tests, defined in section 11.4
REGEX(STRING, PATTERN, FLAGS) simple literalsimple literalsimple literalfn:matches(STRING, PATTERN, FLAGS)xsd:boolean

11.4 Operators Definitions

This section defines the operators introduced by the SPARQL Query language. The examples show the behavior of the operators as invoked by the appropriate grammatical constructs.

11.4.1 bound

 xsd:boolean   bound (variable var)

Returns true if a var is bound to a value. Returns false otherwise. Variables with the value NaN or INF are considered bound. See 4.2 Unbound Variables for a discussion of why variables may be unbound.


@prefix foaf:        <http://xmlns.com/foaf/0.1/> .
@prefix dc:          <http://purl.org/dc/elements/1.1/> .
@prefix xsd:          <http://www.w3.org/2001/XMLSchema#> .

_:a  foaf:givenName  "Alice".

_:b  foaf:givenName  "Bob" .
_:b  dc:date         "2005-04-04T04:04:04Z"^^xsd:dateTime .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc:   <http://purl.org/dc/elements/1.1/>
PREFIX xsd:   <http://www.w3.org/2001/XMLSchema#>
SELECT ?name
 WHERE { ?x foaf:givenName  ?givenName .
         OPTIONAL { ?x dc:date ?date } .
         FILTER ( bound(?date) ) }

Query result:


One may test that a graph pattern is not expressed by specifying an optional graph pattern that introduces a variable and testing to see that the variable is not bound. This is called Negation as Failure in logic programming.

This query matches the people with a name but no expressed date:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc:   <http://purl.org/dc/elements/1.1/>
SELECT ?name
 WHERE { ?x foaf:givenName  ?name .
         OPTIONAL { ?x dc:date ?date } .
         FILTER (!bound(?date)) }

Query result:


Because Bob's dc:date was known, "Bob" was not a solution to the query.

11.4.2 isIRI

 xsd:boolean   isIRI (RDF term term)
 xsd:boolean   isURI (RDF term term)

Returns true if term is an IRI. Returns false otherwise. isURI is an alternate spelling for the isIRI operator.

@prefix foaf:       <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name       "Alice".
_:a  foaf:mbox       <mailto:alice@work.example> .

_:b  foaf:name       "Bob" .
_:b  foaf:mbox       "bob@work.example" .

This query matches the people with a name and an mbox which is an IRI:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
 WHERE { ?x foaf:name  ?name ;
            foaf:mbox  ?mbox .
         FILTER isIRI(?mbox) }

Query result:

name mbox
"Alice" <mailto:alice@work.example>

11.4.3 isBlank

 xsd:boolean   isBlank (RDF term term)

Returns true if term is a blank node. Returns false otherwise.

@prefix a:          <http://www.w3.org/2000/10/annotation-ns#> .
@prefix dc:         <http://purl.org/dc/elements/1.1/> .
@prefix foaf:       <http://xmlns.com/foaf/0.1/> .

_:a   a:annotates   <http://www.w3.org/TR/rdf-sparql-query/> .
_:a   dc:creator    "Alice B. Toeclips" .

_:b   a:annotates   <http://www.w3.org/TR/rdf-sparql-query/> .
_:b   dc:creator    _:c .
_:c   foaf:given    "Bob".
_:c   foaf:family   "Smith".

This query matches the people with a dc:creator which uses predicates from the FOAF vocabulary to express the name.

PREFIX a:      <http://www.w3.org/2000/10/annotation-ns#>
PREFIX dc:     <http://purl.org/dc/elements/1.1/>
PREFIX foaf:   <http://xmlns.com/foaf/0.1/>

SELECT ?given ?family
 WHERE { ?annot  a:annotates  <http://www.w3.org/TR/rdf-sparql-query/> .
         ?annot  dc:creator   ?c .
         OPTIONAL { ?c  foaf:given   ?given ; foaf:family  ?family } .
         FILTER isBlank(?c)

Query result:

given family
"Bob" "Smith"

In this example, there were two objects of foaf:knows predicates, but only one (_:c) was a blank node.

11.4.4 isLiteral

 xsd:boolean   isLiteral (RDF term term)

Returns true if term is a literal. Returns false otherwise.

@prefix foaf:       <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name       "Alice".
_:a  foaf:mbox       <mailto:alice@work.example> .

_:b  foaf:name       "Bob" .
_:b  foaf:mbox       "bob@work.example" .

This query is similar to the one in 11.4.2 except that is matches the people with a name and an mbox which is a literal. This could be used to look for erroneous data (foaf:mbox should only have an IRI as its object).

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
 WHERE { ?x foaf:name  ?name ;
           foaf:mbox  ?mbox .
         FILTER isLiteral(?mbox) }

Query result:

name mbox
"Bob" "bob@work.example"

11.4.5 str

 simple literal   str (literal ltrl)
 simple literal   str (IRI rsrc)

Returns the lexical form of ltrl, a literal; returns the codepoint representation of rsrc (an IRI). This is useful for examining parts of an IRI, for instance, the host-name.

@prefix foaf:       <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name       "Alice".
_:a  foaf:mbox       <mailto:alice@work.example> .

_:b  foaf:name       "Bob" .
_:b  foaf:mbox       <mailto:bob@home.example> .

This query selects the set of people who use their work.example address in their foaf profile:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
 WHERE { ?x foaf:name  ?name ;
            foaf:mbox  ?mbox .
         FILTER regex(str(?mbox), "@work.example") }

Query result:

name mbox
"Alice" <mailto:alice@work.example>

11.4.6 lang

 simple literal   lang (literal ltrl)

Returns the language tag of ltrl, if it has one. It returns "" if ltrl has no language tag.

@prefix foaf:       <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name       "Robert"@EN.
_:a  foaf:name       "Roberto"@ES.
_:a  foaf:mbox       <mailto:bob@work.example> .

This query finds the Spanish foaf:name and foaf:mbox:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
 WHERE { ?x foaf:name  ?name ;
            foaf:mbox  ?mbox .
         FILTER ( lang(?name) = "ES" ) }

Query result:

name mbox
"Roberto"@ES <mailto:bob@work.example>

11.4.7 datatype

 rdfs:Datatype   datatype (typed literal ltrl)

Returns the datatype IRI of ltrl.

@prefix foaf:       <http://xmlns.com/foaf/0.1/> .
@prefix eg:         <http://biometrics.example/ns#> .
@prefix xsd:        <http://www.w3.org/2001/XMLSchema#> .

_:a  foaf:name       "Alice".
_:a  eg:shoeSize     "9.5"^^xsd:float .

_:b  foaf:name       "Bob".
_:b  eg:shoeSize     "42"^^xsd:integer .

This query finds everyone's foaf:name and integer foaf:shoeSize:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX xsd:  <http://www.w3.org/2001/XMLSchema#>
PREFIX eg:   <http://biometrics.example/ns#>
SELECT ?name ?shoeSize
 WHERE { ?x foaf:name  ?name ; eg:shoeSize  ?shoeSize .
         FILTER ( datatype(?shoeSize) = xsd:integer ) }

Query result:

name shoeSize
"Bob" 42

11.4.8 logical-or

 xsd:boolean   xsd:boolean left || xsd:boolean right

Returns a logical OR of left and right. As with other functions and operators with boolean arguments, logical-or operates on the effective boolean value of its arguments.

Note: see section 11.2, Filter Evaluation, for the || operator's treatment of errors.

11.4.9 logical-and

 xsd:boolean   xsd:boolean left && xsd:boolean right

Returns a logical AND of left and right. As with other functions and operators with boolean arguments, logical-and operates on the effective boolean value of its arguments.

Note: see section 11.2, Filter Evaluation, for the && operator's treatment of errors.

11.4.10 RDFterm-equal

 xsd:boolean   RDF term term1 = RDF term term2

Returns TRUE if term1 and term2 are the same RDF term as defined in Resource Description Framework (RDF): Concepts and Abstract Syntax [CONCEPTS]. term1 and term2 are the same if any of the following is true:

@prefix foaf:       <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name       "Alice".
_:a  foaf:mbox       <mailto:alice@work.example> .

_:b  foaf:name       "Ms A.".
_:b  foaf:mbox       <mailto:alice@work.example> .

This query finds the people who have multiple foaf:name arcs:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name1 ?name2
 WHERE { ?x foaf:name  ?name1 ;
            foaf:mbox  ?mbox1 .
         ?y foaf:name  ?name2 ;
            foaf:mbox  ?mbox2 .
         FILTER (?mbox1 = ?mbox2 && ?name1 != ?name2)

Query result:

name1 name2
"Alice" "Ms A."
"Ms A." "Alice"

In this query for documents that were annotated on New Year's Day (2004 or 2005), the RDF terms are not the same, but have equivalent values:

@prefix a:          <http://www.w3.org/2000/10/annotation-ns#> .
@prefix dc:         <http://purl.org/dc/elements/1.1/> .

_:b   a:annotates   <http://www.w3.org/TR/rdf-sparql-query/> .
_:b   dc:date       "2004-12-31T19:00:00-05:00"^^<http://www.w3.org/2001/XMLSchema#dateTime> .
PREFIX a:      <http://www.w3.org/2000/10/annotation-ns#>
PREFIX dc:     <http://purl.org/dc/elements/1.1/>
PREFIX xsd:    <http://www.w3.org/2001/XMLSchema#>

SELECT ?annotates
WHERE { ?annot  a:annotates  ?annotates .
        ?annot  dc:date      ?date .
        FILTER ( ?date = xsd:dateTime("2004-01-01T00:00:00Z") || ?date = xsd:dateTime("2005-01-01T00:00:00Z") ) }

11.4.11 langMatches

 xsd:boolean   langMatches (simple literal language-tag, simple literal language-range)

Returns true if language-range (second argument) matches language-tag (first argument) per Tags for the Identification of Languages [RFC3066] section 2.5. RFC3066 defines a case-insensitive, hierarchical matching algorithm which operates on ISO-defined subtags for language and country codes, and user defined subtags. In SPARQL, a language-range of "*" matches any non-empty language-tag string.

@prefix dc:       <http://purl.org/dc/elements/1.1/> .

_:a  dc:title         "That Seventies Show"@en .
_:a  dc:title         "Cette Série des Années Soixante-dix"@fr .
_:a  dc:title         "Cette Série des Années Septante"@fr-BE .
_:b  dc:title         "Il Buono, il Bruto, il Cattivo" .

This query uses langMatches and lang (described in section to find the French titles for the show known in English as "That Seventies Show":

PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?title
 WHERE { ?x dc:title  "That Seventies Show"@en ;
            dc:title  ?title .
         FILTER langMatches( lang(?title), "FR" ) }

Query result:

"Cette Série des Années Soixante-dix"@fr
"Cette Série des Années Septante"@fr-BE

The idiom langMatches( lang( ?v ), "*" ) will not match literals without a language tag as lang( ?v ) will return an empty string, so

PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?title
 WHERE { ?x dc:title  ?title .
         FILTER langMatches( lang(?title), "*" ) }

will report all of the titles with a language tag:

"That Seventies Show"@en
"Cette Série des Années Soixante-dix"@fr
"Cette Série des Années Septante"@fr-BE

11.4.12 regex

 xsd:boolean   regex (simple literal text, simple literal pattern)
 xsd:boolean   regex (simple literal text, simple literal pattern, simple literal flags)

Invokes the XPath fn:matches function to match text against a regular expression pattern. The regular expression language is defined in XQuery 1.0 and XPath 2.0 Functions and Operators section 7.6.1 Regular Expression Syntax [FUNCOP].

@prefix foaf:       <http://xmlns.com/foaf/0.1/> .

_:a  foaf:name       "Alice".
_:b  foaf:name       "Bob" .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
 WHERE { ?x foaf:name  ?name
         FILTER regex(?name, "^ali", "i") }

Query result:


11.5 Constructor Functions

SPARQL imports a subset of the XPath constructor functions defined in XQuery 1.0 and XPath 2.0 Functions and Operators [FUNCOP] in section 17.1 Casting from primitive types to primitive types. SPARQL constructors include all of the XPath constructors for the SPARQL operand data types plus the additional datatypes imposed by the RDF data model. Casting in SPARQL is performed by calling a constructor function for the target type on an operand of the source type.

XPath defines only the casts from one XML Schema datatype to another. The remaining casts are defined as follows:

The table below summarizes the casting operations that are always allowed (Y), never allowed (N) and dependent on the lexical value (M). For example, a casting operation from an xsd:string (the first row) to an xsd:float (the second column) is dependent on the lexical value (M).

bool = xsd:boolean
dbl = xsd:double
flt = xsd:float
dec = xsd:decimal
int = xsd:integer
dT = xsd:dateTime
str = xsd:string
ltrl = simple literal

From \ To str flt dbl dec int dT bool
str Y M M M M M M
flt Y Y Y M M N Y
dbl Y Y Y M M N Y
dec Y Y Y Y Y N Y
int Y Y Y Y Y N Y
dT Y N N N N Y N
bool Y Y Y Y Y N Y
ltrl Y M M M M M M

11.6 Extensible Value Testing

A PrimaryExpression (see the grammar, production [55]) can be a call to an extension function named by an IRI. An extension function takes some number of RDF terms as arguments and returns an RDF term. The semantics of these functions are identified by the IRI that identifies the function.

SPARQL queries using extension functions are likely to have limited interoperability.

As an example, consider a function called func:even:

 xsd:boolean   func:even (numeric value)

This function would be invoked in a FILTER as such:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX func: <http://example.org/functions#>
SELECT ?name ?id
WHERE { ?x foaf:name  ?name ;
           func:empId   ?id .
        FILTER (func:even(?id)) }

For a second example, consider a function aGeo:distance that calculates the distance between two points, which is used here to find the places near Grenoble:

 xsd:boolean   aGeo:distance (numeric x, numeric y)
PREFIX aGeo: <http://example.org/geo#>

SELECT ?neighbor
WHERE { ?a aGeo:placeName "Grenoble" .
        ?a aGeo:location ?axLoc .
        ?a aGeo:location ?ayLoc .

        ?b aGeo:placeName ?neighbor .
        ?b aGeo:location ?bxLoc .
        ?b aGeo:location ?byLoc .

        FILTER ( aGeo:distance(?axLoc, ?ayLoc, ?bxLoc, ?byLoc) < 10 ) .

An extension function might be used to test some application datatype not supported by the core SPARQL specification, it might be a transformation between datatype formats, for example into an XSD dateTime RDF term from another date format.

A. SPARQL Grammar

A SPARQL query string is a Unicode character string (c.f. section 6.1 String concepts of [CHARMOD]) in the language defined by the following grammar, starting with the Query production. For compatibility with future versions of Unicode, the characters in this string may include Unicode codepoints that are unassigned as of the date of this publication (see Identifier and Pattern Syntax [UNIID] section 4 Pattern Syntax). For productions with excluded character classes (for example [^<>'{}|^`]), the characters are excluded from the range #x0 - #x10FFFF.

The EBNF notation used in the grammar is defined in Extensible Markup Language (XML) 1.1 [XML11] section 6 Notation.

In addition, rules A.1 to A.5 apply.

A.1 IRI References

Text matched by the Q_IRI_REF production and QName (after prefix expansion) production, after escape processing, must be conform to the generic syntax of IRI references in section 2.2 of RFC 3987 "ABNF for IRI References and IRIs" [RFC3987]. For example, the Q_IRI_REF <abc#def> may occur in a SPARQL query string, but the Q_IRI_REF <abc##def> must not.

Base IRIs declared with the BASE keyword must be absolute IRIs. A prefix declared with the PREFIX keyword may not be re-declared in the same query. See see section 2.1.1, Syntax of IRI Terms, for a description of BASE and PREFIX.

A.2 White Space

White space (production WS) is used to separate two terminals which would otherwise be (mis-)recognized as one terminal. Rule names below in capitals indicate where whitespace is significant; these form a possible choice of terminals for constructing a SPARQL parser. White space is significant in strings.

A.3 Keywords

Keywords are matched in a case-insensitive manner.

The exception is the keyword 'a' which, in line with Turtle and N3, is used in place of the IRI rdf:type (in full,  http://www.w3.org/1999/02/22-rdf-syntax-ns#type).

A.4 Comments

Comments in SPARQL queries take the form of '#', outside an IRI or string, and continue to the end of line (marked by characters 0x0D or 0x0A) or end of file if there is no end of line after the comment marker. Comments are treated as white space.

A.5 Escape sequences in strings

The following escape sequences may be used in any string production (e.g. STRING_LITERAL1, STRING_LITERAL2, STRING_LITERAL_LONG1, STRING_LITERAL_LONG2):

Escape Unicode code point
'\u' HEX HEX HEX HEX A Unicode code point in the range U+0 to U+FFFF inclusive corresponding to the encoded hexadecimal value.
'\U' HEX HEX HEX HEX HEX HEX HEX HEX A Unicode code point in the range U+10000 to U+10FFFF inclusive corresponding to the encoded hexadecimal value.
'\t' U+0009 (tab)
'\n' U+000A (line feed)
'\r' U+000D (carriage return)
'\b' U+0008 (backspace)
'\f' U+000C (form feed)
'\"' U+0022 (quotation mark, double quote mark)
"\'" U+0027 (apostrophe-quote, single quote mark)
'\\' U+005C (backslash)

where HEX is a hexadecimal character

HEX ::= [0-9] | [A-F] | [a-f]

Any escaped character in the range #x0 - #x10FFFF may appear in any string production. For instance, "\n" may appear in a STRING_LITERAL1 even though the unescaped form is not valid in that production.



A.6 Escape sequences in IRI references, prefixed names and variable names

The following escape sequences can be used in variable names, IRI references, that is, quoted IRI references, qnames and namespace prefixes.

Escape Unicode code point
'\u' HEX HEX HEX HEX A Unicode code point in the range U+0 to U+FFFF inclusive corresponding to the encoded hexadecimal value.
'\U' HEX HEX HEX HEX HEX HEX HEX HEX A Unicode code point in the range U+10000 to U+10FFFF inclusive corresponding to the encoded hexadecimal value.



These escape sequences are not included in the grammar below. Only escape sequences for characters that would be legal at that point in the grammar may be given.  For example, the variable "?x\u0020y" is not legal (\u0020 is a space and is not permitted in a variable name).

A.7 Grammar

When choosing a rule to match, the longest match is chosen. Matching is case-sensitive except as noted above for keywords.


[1]   Query   ::=   Prolog
( SelectQuery | ConstructQuery | DescribeQuery | AskQuery )
[2]   Prolog   ::=   BaseDecl? PrefixDecl*
[3]   BaseDecl   ::=   'BASE' Q_IRI_REF
[4]   PrefixDecl   ::=   'PREFIX' QNAME_NS Q_IRI_REF
[5]   SelectQuery   ::=   'SELECT' 'DISTINCT'? ( Var+ | '*' ) DatasetClause* WhereClause SolutionModifier
[6]   ConstructQuery   ::=   'CONSTRUCT' ConstructTemplate DatasetClause* WhereClause SolutionModifier
[7]   DescribeQuery   ::=   'DESCRIBE' ( VarOrIRIref+ | '*' ) DatasetClause* WhereClause? SolutionModifier
[8]   AskQuery   ::=   'ASK' DatasetClause* WhereClause
[9]   DatasetClause   ::=   'FROM' ( DefaultGraphClause | NamedGraphClause )
[10]   DefaultGraphClause   ::=   SourceSelector
[11]   NamedGraphClause   ::=   'NAMED' SourceSelector
[12]   SourceSelector   ::=   IRIref
[13]   WhereClause   ::=   'WHERE'? GroupGraphPattern
[14]   SolutionModifier   ::=   OrderClause? LimitClause? OffsetClause?
[15]   OrderClause   ::=   'ORDER' 'BY' OrderCondition+
[16]   OrderCondition   ::=   ( ( 'ASC' | 'DESC' ) BrackettedExpression )
| ( FunctionCall | Var | BrackettedExpression )
[17]   LimitClause   ::=   'LIMIT' INTEGER
[18]   OffsetClause   ::=   'OFFSET' INTEGER
[19]   GroupGraphPattern   ::=   '{' GraphPattern '}'
[20]   GraphPattern   ::=   FilteredBasicGraphPattern ( GraphPatternNotTriples '.'? GraphPattern )?
[21]   FilteredBasicGraphPattern   ::=   BlockOfTriples? ( Constraint '.'? FilteredBasicGraphPattern )?
[22]   BlockOfTriples   ::=   TriplesSameSubject ( '.' TriplesSameSubject? )*
[23]   GraphPatternNotTriples   ::=   OptionalGraphPattern | GroupOrUnionGraphPattern | GraphGraphPattern
[24]   OptionalGraphPattern   ::=   'OPTIONAL' GroupGraphPattern
[25]   GraphGraphPattern   ::=   'GRAPH' VarOrBlankNodeOrIRIref GroupGraphPattern
[26]   GroupOrUnionGraphPattern   ::=   GroupGraphPattern ( 'UNION' GroupGraphPattern )*
[27]   Constraint   ::=   'FILTER' ( BrackettedExpression | BuiltInCall | FunctionCall )
[28]   FunctionCall   ::=   IRIref ArgList
[29]   ArgList   ::=   ( NIL | '(' Expression ( ',' Expression )* ')' )
[30]   ConstructTemplate   ::=   '{' ConstructTriples '}'
[31]   ConstructTriples   ::=   ( TriplesSameSubject ( '.' ConstructTriples )? )?
[32]   TriplesSameSubject   ::=   VarOrTerm PropertyListNotEmpty | TriplesNode PropertyList
[33]   PropertyList   ::=   PropertyListNotEmpty?
[34]   PropertyListNotEmpty   ::=   Verb ObjectList ( ';' PropertyList )?
[35]   ObjectList   ::=   GraphNode ( ',' ObjectList )?
[36]   Verb   ::=   VarOrIRIref | 'a'
[37]   TriplesNode   ::=   Collection | BlankNodePropertyList
[38]   BlankNodePropertyList   ::=   '[' PropertyListNotEmpty ']'
[39]   Collection   ::=   '(' GraphNode+ ')'
[40]   GraphNode   ::=   VarOrTerm | TriplesNode
[41]   VarOrTerm   ::=   Var | GraphTerm
[42]   VarOrIRIref   ::=   Var | IRIref
[43]   VarOrBlankNodeOrIRIref   ::=   Var | BlankNode | IRIref
[44]   Var   ::=   VAR1 | VAR2
[45]   GraphTerm   ::=   IRIref | RDFLiteral | ( '-' | '+' )? NumericLiteral | BooleanLiteral | BlankNode | NIL
[46]   Expression   ::=   ConditionalOrExpression
[47]   ConditionalOrExpression   ::=   ConditionalAndExpression ( '||' ConditionalAndExpression )*
[48]   ConditionalAndExpression   ::=   ValueLogical ( '&&' ValueLogical )*
[49]   ValueLogical   ::=   RelationalExpression
[50]   RelationalExpression   ::=   NumericExpression ( '=' NumericExpression | '!=' NumericExpression | '<' NumericExpression | '>' NumericExpression | '<=' NumericExpression | '>=' NumericExpression )?
[51]   NumericExpression   ::=   AdditiveExpression
[52]   AdditiveExpression   ::=   MultiplicativeExpression ( '+' MultiplicativeExpression | '-' MultiplicativeExpression )*
[53]   MultiplicativeExpression   ::=   UnaryExpression ( '*' UnaryExpression | '/' UnaryExpression )*
[54]   UnaryExpression   ::=     '!' PrimaryExpression
| '+' PrimaryExpression
| '-' PrimaryExpression
| PrimaryExpression
[55]   PrimaryExpression   ::=   BrackettedExpression | BuiltInCall | IRIrefOrFunction | RDFLiteral | NumericLiteral | BooleanLiteral | BlankNode | Var
[56]   BrackettedExpression   ::=   '(' Expression ')'
[57]   BuiltInCall   ::=     'STR' '(' Expression ')'
| 'LANG' '(' Expression ')'
| 'LANGMATCHES' '(' Expression ',' Expression ')'
| 'DATATYPE' '(' Expression ')'
| 'BOUND' '(' Var ')'
| 'isIRI' '(' Expression ')'
| 'isURI' '(' Expression ')'
| 'isBLANK' '(' Expression ')'
| 'isLITERAL' '(' Expression ')'
| RegexExpression
[58]   RegexExpression   ::=   'REGEX' '(' Expression ',' Expression ( ',' Expression )? ')'
[59]   IRIrefOrFunction   ::=   IRIref ArgList?
[60]   RDFLiteral   ::=   String ( LANGTAG | ( '^^' IRIref ) )?
[61]   NumericLiteral   ::=   INTEGER | DECIMAL | DOUBLE
[62]   BooleanLiteral   ::=   'true' | 'false'
[64]   IRIref   ::=   Q_IRI_REF | QName
[65]   QName   ::=   QNAME | QNAME_NS
[66]   BlankNode   ::=   BLANK_NODE_LABEL | ANON
[67]   Q_IRI_REF   ::=   '<' ([^<>'{}|^`]-[#x00-#x20])* '>'
[68]   QNAME_NS   ::=   NCNAME_PREFIX? ':'
[69]   QNAME   ::=   NCNAME_PREFIX? ':' NCNAME?
[70]   BLANK_NODE_LABEL   ::=   '_:' NCNAME
[71]   VAR1   ::=   '?' VARNAME
[72]   VAR2   ::=   '$' VARNAME
[73]   LANGTAG   ::=   '@' [a-zA-Z]+ ('-' [a-zA-Z0-9]+)*
[74]   INTEGER   ::=   [0-9]+
[75]   DECIMAL   ::=   [0-9]+ '.' [0-9]* | '.' [0-9]+
[76]   DOUBLE   ::=   [0-9]+ '.' [0-9]* EXPONENT | '.' ([0-9])+ EXPONENT | ([0-9])+ EXPONENT
[77]   EXPONENT   ::=   [eE] [+-]? [0-9]+
[78]   STRING_LITERAL1   ::=   "'" ( ([^#x27#x5C#xA#xD]) | ECHAR | UCHAR )* "'"
[79]   STRING_LITERAL2   ::=   '"' ( ([^#x22#x5C#xA#xD]) | ECHAR | UCHAR )* '"'
[80]   STRING_LITERAL_LONG1   ::=   "'''" ( ( "'" | "''" )? ( [^'\] | ECHAR | UCHAR ) )* "'''"
[81]   STRING_LITERAL_LONG2   ::=   '"""' ( ( '"' | '""' )? ( [^"\] | ECHAR | UCHAR ) )* '"""'
[82]   ECHAR   ::=   '\' [tbnrf\"']
[83]   UCHAR   ::=   '\' ( 'u' HEX HEX HEX HEX | 'U' HEX HEX HEX HEX HEX HEX HEX HEX )
[84]   HEX   ::=   [0-9] | [A-F] | [a-f]
[85]   NIL   ::=   '(' WS* ')'
[86]   WS   ::=   #x20 | #x9 | #xD | #xA
[87]   ANON   ::=   '[' WS* ']'
[88]   NCCHAR1p   ::=     [A-Z]
| [a-z]
| [#x00C0-#x00D6]
| [#x00D8-#x00F6]
| [#x00F8-#x02FF]
| [#x0370-#x037D]
| [#x037F-#x1FFF]
| [#x200C-#x200D]
| [#x2070-#x218F]
| [#x2C00-#x2FEF]
| [#x3001-#xD7FF]
| [#xF900-#xFDCF]
| [#xFDF0-#xFFFD]
| [#x10000-#xEFFFF]
[89]   NCCHAR1   ::=   NCCHAR1p | '_'
[90]   VARNAME   ::=   ( NCCHAR1 | [0-9] ) ( NCCHAR1 | [0-9] | #x00B7 | [#x0300-#x036F] | [#x203F-#x2040] )*
[91]   NCCHAR   ::=   NCCHAR1 | '-' | [0-9] | #x00B7 | [#x0300-#x036F] | [#x203F-#x2040]
[92]   NCNAME_PREFIX   ::=   NCCHAR1p ((NCCHAR|'.')* NCCHAR)?
[93]   NCNAME   ::=   NCCHAR1 ((NCCHAR|'.')* NCCHAR)?

The SPARQL grammar is LL(1) when the rules with uppercased names are used as terminals.

Some grammar files for some commonly used tools are avaiable here (parsers/).

B. Conformance

See appendix A SPARQL Grammar regarding conformance of SPARQL Query strings, and section 10 Query Result Forms for conformance of query results. See appendix E. Internet Media Type for conformance to the application/sparql-query media type.

This specification is intended for use in conjunction with the SPARQL Protocol [SPROT] and the SPARQL Query Results XML Format [RESULTS]. See those specifications for their conformance criteria.

Note that the SPARQL protocol describes an abstract interface as well as a network protocol, and the abstract interface may apply to APIs as well as network interfaces.

C. Security Considerations

SPARQL queries using FROM, FROM NAMED, or GRAPH may cause the specified URI to be dereferenced. This may cause additional use of network, disk or CPU resources along with associated secondary issues such as denial of service. The security issues of Uniform Resource Identifier (URI): Generic Syntax [RFC3986] Section 7 should be considered. In addition, the contents of file: URIs can in some cases be accessed, processed and returned as results, providing unintended access to local resources.

The SPARQL language permits extensions, which will have their own security implications.

Multiple IRIs may have the same appearance. Characters in different scripts may look similar (a Cyrillic "о" may appear similar to a Latin "o"). A character followed by combining characters may have the same visual representation as another character (LATIN SMALL LETTER E followed by COMBINING ACUTE ACCENT has the same visual representation as LATIN SMALL LETTER E WITH ACUTE). Users of SPARQL must take care to construct queries with IRIs that match the IRIs in the data. Further information about matching of similar characters can be found in Unicode Security Considerations [UNISEC] and Internationalized Resource Identifiers (IRIs) [RFC3987] Section 8.

D. Collected Formal Definitions

The collected formal definitions are collected into a single document "SPARQL Query Language for RDF - Formal Definitions".

E. Internet Media Type, File Extension and Macintosh File Type (Normative)

Eric Prud'hommeaux
See also:
How to Register a Media Type for a W3C Specification
Internet Media Type registration, consistency of use
TAG Finding 3 June 2002 (Revised 4 September 2002)

The Internet Media Type / MIME Type for the SPARQL Query Language is "application/sparql-query".

It is recommended that sparql query files have the extension ".rq" (all lowercase) on all platforms.

It is recommended that sparql query files stored on Macintosh HFS file systems be given a file type of "TEXT".

This information that follows is intended to be submitted to the IESG for review, approval, and registration with IANA.

Type name:
Subtype name:
Required parameters:
Optional parameters:
Encoding considerations:
The syntax of the SPARQL Query Language is expressed over code points in Unicode [UNICODE]. The encoding is always UTF-8 [RFC3629].
Unicode code points may also be expressed using an \uXXXX (U+0 to U+FFFF) or \UXXXXXXXX syntax (for U+10000 onwards) where X is a hexadecimal digit [0-9A-F]
Security considerations:
See SPARQL Query appendix C, Security Considerations as well as RFC 3629 [RFC3629] section 7, Security Considerations.
Interoperability considerations:
There are no known interoperability issues.
Published specification:
This specification.
Applications which use this media type:
No known applications currently use this media type.
Additional information:
Magic number(s):
A SPARQL query may have the string 'PREFIX' (case independent) near the beginning of the document.
File extension(s):
Base URI:
The SPARQL 'BASE <IRIref>' term can change the current base URI for relative IRIrefs in the query language that are used sequentially later in the document.
Macintosh file type code(s):
Person & email address to contact for further information:
Intended usage:
Restrictions on usage:
Author/Change controller:
The SPARQL specification is a work product of the World Wide Web Consortium's RDF Data Access Working Group. The W3C has change control over these specifications.

F. References

Normative References

Character Model for the World Wide Web 1.0: Fundamentals , R. Ishida, F. Yergeau, M. J. Düst, M. Wolf, T. Texin, Editors, W3C Recommendation, 15 February 2005, http://www.w3.org/TR/2005/REC-charmod-20050215/ . Latest version available at http://www.w3.org/TR/charmod/ .
Resource Description Framework (RDF): Concepts and Abstract Syntax , G. Klyne, J. J. Carroll, Editors, W3C Recommendation, 10 February 2004, http://www.w3.org/TR/2004/REC-rdf-concepts-20040210/ . Latest version available at http://www.w3.org/TR/rdf-concepts/ .
XQuery 1.0 and XPath 2.0 Functions and Operators , J. Melton, A. Malhotra, N. Walsh, Editors, W3C Working Draft (work in progress), 4 April 2005, http://www.w3.org/TR/2005/WD-xpath-functions-20050404/ . Latest version available at http://www.w3.org/TR/xpath-functions/ .
RDF Semantics , P. Hayes, Editor, W3C Recommendation, 10 February 2004, http://www.w3.org/TR/2004/REC-rdf-mt-20040210/ . Latest version available at http://www.w3.org/TR/rdf-mt/ .
RFC 3629 UTF-8, a transformation format of ISO 10646, F. Yergeau November 2003
RFC 3066 Tags for the Identification of Languages, H. Alvestrand January 2001
RFC 3986 Uniform Resource Identifier (URI): Generic Syntax, T. Berners-Lee, R. Fielding, L. Masinter January 2005
RFC 3987, "Internationalized Resource Identifiers (IRIs)", M. Dürst , M. Suignard
The Unicode Standard, Version 4. ISBN 0-321-18578-1, as updated from time to time by the publication of new versions. The latest version of Unicode and additional information on versions of the standard and of the Unicode Character Database is available at http://www.unicode.org/unicode/standard/versions/.
Extensible Markup Language (XML) 1.1 , J. Cowan, J. Paoli, E. Maler, C. M. Sperberg-McQueen, F. Yergeau, T. Bray, Editors, W3C Recommendation, 4 February 2004, http://www.w3.org/TR/2004/REC-xml11-20040204/ . Latest version available at http://www.w3.org/TR/xml11/ .
XML Path Language (XPath) 2.0 , M. F. Fern?dez, D. Chamberlin, M. Kay, S. Boag, J. Robie, J. Sim?n, A. Berglund, Editors, W3C Working Draft (work in progress), 4 April 2005, http://www.w3.org/TR/2005/WD-xpath20-20050404/ . Latest version available at http://www.w3.org/TR/xpath20/ .
XQuery 1.0: An XML Query Language , S. Boag, M. F. Fernández, D. Chamberlin, D. Florescu, J. Robie, J. Siméon, Editors, W3C Candidate Recommendation 3 November 2005, http://www.w3.org/TR/2005/CR-xquery-20051103/. Latest version available at http://www.w3.org/TR/xquery/ .
XML Schema Part 2: Datatypes Second Edition, P. V. Biron, A. Malhotra, Editors, W3C Recommendation, 28 October 2004, http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/ . Latest version available at http://www.w3.org/TR/xmlschema-2/ .

Informative References

CBD - Concise Bounded Description, Patrick Stickler, Nokia, W3C Member Submission, 3 June 2005.
Expressing Simple Dublin Core in RDF/XML Dublin Core Dublin Core Metadata Initiative Recommendation 2002-07-31.
OWL Web Ontology Language Semantics and Abstract Syntax, Peter F. Patel-Schneider, Patrick Hayes, Ian Horrocks, Editors, W3C Recommendation http://www.w3.org/TR/2004/REC-owl-semantics-20040210/. Latest version at http://www.w3.org/TR/owl-semantics/.
RDF Vocabulary Description Language 1.0: RDF Schema, Dan Brickley, R.V. Guha, Editors. W3C Recommendation http://www.w3.org/TR/2004/REC-rdf-schema-20040210/. Latest version at http://www.w3.org/TR/rdf-schema/.
SPARQL Query Results XML Format , D. Beckett, Editor, W3C Working Draft (work in progress), 27 May 2005, http://www.w3.org/TR/2005/WD-rdf-sparql-XMLres-20050527/ . Latest version available at http://www.w3.org/TR/rdf-sparql-XMLres/ .
SPARQL Protocol for RDF, K. Clark, Editor, W3C Working Draft (work in progress), 27 May 2005, http://www.w3.org/TR/2005/WD-rdf-sparql-protocol-20050527/ . Latest version available at http://www.w3.org/TR/rdf-sparql-protocol/ .
"Turtle - Terse RDF Triple Language, Dave Beckett.
RDF Data Access Use Cases and Requirements , K. Clark, Editor, W3C Working Draft (work in progress), 25 March 2005, http://www.w3.org/TR/2005/WD-rdf-dawg-uc-20050325/ . Latest version available at http://www.w3.org/TR/rdf-dawg-uc/ .
Unicode Security Considerations, Mark Davis, Michel Suignard
Representing vCard Objects in RDF/XML W3C Note 22 February 2001 Renato Iannella. latest version is available at http://www.w3.org/TR/vcard-rdf
Architecture of the World Wide Web, Volume One, Editors: Ian Jacobs, Norman Walsh
Identifier and Pattern Syntax 4.1.0, Mark Davis, Unicode Standard Annex #31, 25 March 2005, http://www.unicode.org/reports/tr31/tr31-5.html . Latest version available at http://www.unicode.org/reports/tr31/ .

G. Acknowledgements

The SPARQL RDF Query Language is a product of the whole of the W3C RDF Data Access Working Group, and our thanks for discussions, comments and reviews go to all present and past members.

In addition, we have had comments and discussions with many people through the working group comments list.  All comments go to making a better document.  Andy would also like to particularly thank Geoff Chappell, Bob MacGregor, Yosi Scharf and Richard Newman for exploring specific issues related to SPARQL. Eric would like to acknowledge the invaluable help of Björn Höhrmann.

H. Change Log

Changes since the 21 July 2005 Working Draft include design changes, for which the WG has or intends to have corresponding test cases:

and significant editorial changes:

Raw CVS log:

$Log: Overview.html,v $
Revision 1.692  2006/07/03 12:33:46  aseaborne
Fixup offset/limit in list order (10.1)

Revision 1.690  2006/06/12 16:18:22  aseaborne
Editorial changes:

1/ Solution Sequence Modifiers
   + All examples are now S0, S1, ....
   + Processing order is OFFSET the LIMIT

2/ Clarify use of an IRI in two FROM NAMED clauses
  (it's idempotent - one graph in the dataset).

Revision 1.688  2006/06/10 11:52:03  aseaborne
Fix fn: URI for xpath-functions (as per planned update)

Revision 1.687  2006/06/10 10:58:35  aseaborne
Fix fn: URI for xpath-functions

Revision 1.686  2006/06/07 15:39:36  aseaborne
Editorial changes in response to
WG message 2006MayApr/0114

Revision 1.685  2006/06/07 14:13:51  eric
~ [EDITORIAL] clarify small points in Optional Pattern Matching

Revision 1.684  2006/06/01 15:30:55  aseaborne
Editorial wording changes

Revision 1.683  2006/06/01 10:51:21  aseaborne
Wording improvements/corrections

Revision 1.682  2006/06/01 09:39:21  aseaborne
In edits in response to

Revision 1.681  2006/05/30 06:10:39  eric
~ [editorial] update manual change log to reflect the boolean additions in 1.680

Revision 1.680  2006/05/30 06:07:52  eric
~ [editorial] consistent use of curly braces for sets of supported datatypes.
~ [editorial] clarify that SPARQL tests are SPARQL value tests.
~ [editorial] EBV of a boolean argument is the value of the argument.
+ add boolean = != < > <= >= to OperatorMapping per DAWG resolution.

Revision 1.679  2006/05/15 16:22:07  aseaborne
In edits in response to 2006Apr/0022

Revision 1.678  2006/05/02 16:12:11  aseaborne
Removed note that blank nodes can appear as predicates because they can't any more

Revision 1.677  2006/04/19 10:46:06  aseaborne
Fix examples to make data/query/results agree:

2.9 (second query)
Remove space:
s/" Bob"/"Bob"

Fix result : mailto URL was wrong

Add dc:creator information to data for DC 10 and DC 11

s/?name/?givenName/ in query

s/?size/?shoeSize/ in query

Revision 1.676  2006/04/12 09:50:09  aseaborne
Changes in response to 2006Mar/0075

Section 1:
Replace s/plain and typed literals/literals/ in
"""* ... URIs, blank nodes and literals."""

Section 2.1.3:
  Clarify the global scope of varaibles
  Clarify that "?" is not part of the variable name.

Section 2.1.2 and section 2.2
  Clarify that relative IRIs and prefixed named and merely syntax for absolute IRIs

Section 2.8.2:
  s/one of the other/one or the other/

Section 2.6.4:
s/then triple patterns/triple patterns/

Revision 1.675  2006/04/04 15:11:13  eric
2006-04-04T15:08:52Z note: approved 26 July 2005

Revision 1.674  2006/04/04 14:52:22  eric
per 2006-04-04T14:49:31Z <DanC> "Current conventions for DESCRIBE return an RDF graph without any specified constraints."

Revision 1.673  2006/04/04 14:51:24  eric
per 2006-04-04T14:49:31Z <DanC> "Current conventions for DESCRIBE return an RDF graph without any specified constraints."

Revision 1.672  2006/04/03 19:18:52  eric
make section 10.4 non-normative

Revision 1.671  2006/04/03 18:06:38  eric
removed blink tag

Revision 1.670  2006/04/03 18:05:10  eric
change conservative DESCRIBE behavior to queryRefused

Revision 1.669  2006/04/03 14:49:41  eric
~ made DESCRIBE non-normative per DanC's sustained objection to DESCRIBE

Revision 1.668  2006/03/29 05:29:30  connolly
"is" to "are" in sentence about parsers/
tx Karl D.

Revision 1.667  2006/03/23 19:09:29  eric
+ the example in 2.4 had data that i didn't notice

Revision 1.666  2006/03/23 16:25:41  eric
~ couple typos in example data

Revision 1.665  2006/03/22 12:50:59  eric
~ normalize results div structure
- redundant attributes on result tables

Revision 1.664  2006/03/21 10:19:30  aseaborne
Changes in response to 2006Jan/0049

10.1 : Put ORDER BY at start of modifier sequence order

Revision 1.663  2006/03/16 17:04:47  eric
~ updated SPARQL Grammar sections A. SPARQL Grammar and A.5 Escape sequences in strings per DAWG decision
~ s/UIR/IRI/ in A.1 IRI References
~ fixed href change log

Revision 1.662  2006/03/14 16:46:30  aseaborne
Changes in response to 2006Feb/0032


~ Section 2 : s/Combining triple/Combining triple patterns/
~ Section 2.9 : s/refication vocabulary/a refication vocabulary/
~ Section 5.4 : s/any solution/the solution/
~ Section 5.5 : remove confusing sentence
~ Section 5.5 : reworded last para

Some s/URI/IRI/:
11.4.4 (isLiteral), A.1

Revision 1.661  2006/03/13 18:12:44  aseaborne
Changes in response to 2006Feb/0030

~ Section 2.4 : change mention of target graph to "dataset"
~ Section 7 : remove definition of target dataset (not used anywhere now)
~ section 8: correct definition as per comment

Rebuilt sparql-defns.html with these changes.

Revision 1.660  2006/03/13 14:30:22  aseaborne

~ Intro: s/this is/RDF Graphs are/

Revision 1.659  2006/03/09 12:27:32  eric
follow the production number for PrimaryExpression

Revision 1.658  2006/03/08 20:06:37  eric
fixed "Arguments expressions are" typo per Arjohn Kampman's 23 Feb 2006 "Some typos in SPARQL spec" comments

Revision 1.657  2006/03/08 19:59:55  eric
s/can not/cannot/ per Reto Krummenacher's 24 Feb 2006 "Comments on Feb 20 Working Draft of SPARQL" comments

Revision 1.656  2006/03/08 19:33:20  eric
corrected typos in Olivier Corby's 24 Feb 2006 "Typos in SPARQL" comments

Revision 1.655  2006/03/08 18:26:34  eric
s/tripe/triple/ per Robin Berjon's "Typo in SPARQL" comment

Revision 1.654  2006/02/23 10:53:54  aseaborne

~ 9.1: s/The FROM clause/Each FROM clause/
~ Removed <unbound> from XML results example.
~ s/though/through/ in appendix G

Revision 1.653  2006/02/22 21:51:49  connolly
- built-in calls comment under WG discussion
- PFPS comment on intro assigned to this spec

Revision 1.652  2006/02/22 16:11:21  connolly
assigning "built-in calls to generic" comment to this spec

Revision 1.651  2006/02/21 19:45:10  eric
[editorial] linkchecker feedback

Revision 1.650  2006/02/21 18:35:03  aseaborne
Fix a broken links (defn_BGPequiv)

Revision 1.649  2006/02/21 18:32:33  aseaborne
Fix a broken links defn_BGPequiv

Revision 1.648  2006/02/21 18:23:32  aseaborne
Fix a broken links (defn_SPARQLQueryString, CVS email)

Revision 1.647  2006/02/21 18:01:01  aseaborne
Fix a broken link (rHEX)

Revision 1.646  2006/02/18 00:51:13  eric
~ s/type error/error/ per ACTION: EricP to fix /0 stuff in rq23

Revision 1.645  2006/02/17 23:13:35  connolly
truncated CVS log at fifth WD

Revision 1.644  2006/02/17 23:11:11  connolly
struck question about alternative design for datatype()
due to lack of feedback

Revision 1.643  2006/02/17 23:04:04  connolly
fix langMatches order of args and example, per
DB 5Jan

Revision 1.642  2006/02/17 21:56:56  connolly
deleted obsolete comment status info
removed RFC2119 reference and fixed conformance section
markup per BH's suggestions

Revision 1.641  2006/02/16 17:05:18  aseaborne
Fixed appendix titles : put in appendix D

Revision 1.640  2006/02/14 17:50:08  aseaborne

~ Removed "occurring in G" from defn text for Pattern Solution
~ Corrected defn of E-entailment regime

Revision 1.639  2006/02/14 09:22:37  eric
added xsd:string <= >= xsd:string to Operator Mapping perl DAWG decision RESOLVED: add xsd:string to 11.3 Operator Mapping and hence < and and ORDER BY

Revision 1.638  2006/02/08 15:00:47  aseaborne
@@ removal:

+ Added (at end of TOC) a link to "sparql-defns.html"
  This will be the tidied up definitions consolidated into
  one place.  For now, this contains a XSLT-produced version.

~ Expanded on "virtual graphs" in intro.

- Removed alternative, duplicate defn text for BGP equivalence

- Removed note to check sec 11 vs sec 2 as check now done.

+ Reference (informative) to RDFS added as per @@ 11.1
  Updated XQuery reference to CR version

+ parsers/ directory

Revision 1.637  2006/02/07 15:39:11  eric
added xsd:string < > xsd:string to Operator Mapping perl DAWG decision RESOLVED: add xsd:string to 11.3 Operator Mapping and hence < and and ORDER BY

Revision 1.636  2006/02/06 12:14:22  aseaborne
Links in grammar got broken - fixed

Revision 1.635  2006/02/06 11:51:35  aseaborne
Editorial changes in response to 2006Jan/0061
(part 4)

~ Edits in sec 10.4 (Describe)
~ A.2 Whitespace: states where whitespace matters more clearly.
~ Reordered grammar rules
~ Removed defn "query solution" as no longer used anywhere
~ Sec 11 intro : changed
  "The operands of these functions and operators"
  "The datatypes of the operands of these functions and operators"

Revision 1.634  2006/02/03 18:51:47  aseaborne
Editorial changes in response to 2006Jan/0061
(part 3)

~ ensured its says "group graph pattern" everywhere, not "group pattern"
~ 4.1. - mention braces earlier
~ 5. Removed "semi-structured" text
~ 7 "comprising of" / "consisting of"
~ s/named variables/variables/g

Revision 1.633  2006/02/03 14:53:19  aseaborne
(cat log.txt)

Revision 1.632  2006/02/02 10:15:41  aseaborne
Correct CONSTRUCT defn to ensure bNodes don't class with solution

Revision 1.631  2006/02/01 15:17:15  aseaborne
Editorial changes in response to 2006Jan/0061 (part 1)

~ Changed titles of 2.1.1 and 2.1.2 to not use the word "term"
~ Chnaged 2.1.X from "Syntax of" to "Syntax for"
~ Explictly say prefixes do not need to correspond with the serialization
  of the data
~ Link to VARNAME in "2.1.3 Syntax of Variables"
~ Edited intro sentence in 2.6

Revision 1.630  2006/01/31 10:48:52  aseaborne
~ Simple statement that the scoping set is a set of RDF terms.
~ Sentence about the role of BGP' in BGP matching
~ Tidy text about restriction to simple entailment
~ BGP equivalence : short and long versions (to be decided which)

Revision 1.629  2006/01/30 17:38:14  aseaborne
- Removed bnodes from triple pattern predciate position
  Definition and syntax
~ Fixed escape sequence text and put UCHAR and ECHAR into the grammar.

Revision 1.628  2006/01/30 13:28:21  aseaborne
Informative reference to OWL Semantics

Revision 1.627  2006/01/30 12:37:59  aseaborne
Map p in BGP-equiv defn

Revision 1.626  2006/01/30 12:35:52  aseaborne
~ More formal defn of BGP-equivalence
+ Added definition of E-entailment regime
+ Added a separate definition of basic graph pattern
~ Rename to defn to "basic graph pattern E-matching"

Revision 1.624  2006/01/28 17:22:51  aseaborne
~ Changed CONSTRUCT definition in line with email
~ Finished BGP equivalence definition

Revision 1.623  2006/01/26 16:52:30  aseaborne
Edit during telecon (5)

Revision 1.618  2006/01/26 11:09:51  aseaborne
Drafted text for basic graph pattern matching

Revision 1.617  2006/01/25 17:42:13  aseaborne
Typos in matching text

Revision 1.616  2006/01/25 15:49:46  aseaborne
Removed @@ about IRIs and RDF URI references.
Note RDF concepts anticipation of this.

More updates in line with 2006JanMar/0257

Revision 1.615  2006/01/25 15:08:42  aseaborne
Updated BGP matching in line with 2006JanMar/0257

Revision 1.614  2006/01/24 20:07:56  aseaborne
Use rule name FilteredBasicGraphPattern

Revision 1.613  2006/01/23 13:13:08  aseaborne
More drafting for BGP matching

Revision 1.612  2006/01/21 21:16:08  aseaborne
~ Grammar change to be clearer abou tbasic graph patterns
  There are no language changes.
  + Rule for a BasicGraphPattern (triples and constaints)
  ~ Triples1 renamed TriplesSameSubject

Revision 1.611  2006/01/20 17:29:13  aseaborne
Scoping Graph defn word fix

Revision 1.610  2006/01/20 12:06:13  aseaborne
Drafting for 2.5

Revision 1.609  2006/01/19 11:55:23  aseaborne
First draft of basic graph pattern matching.

Revision 1.608  2006/01/18 01:33:37  eric
~ clarify value of an EBV per message to DAWG

Revision 1.607  2006/01/18 01:04:15  eric
+ fixed an ommision: xsd:integer is supported by SPARQL and derived from xsd:decimal
+ additional BASE text responding to Richard Newman — BASE absolute IRIs
+ invented PREFIX re-declaration constraint text at the same time

Revision 1.606  2006/01/17 12:57:16  aseaborne
In response to 2006Jan/0056
added text to A.7 to note matching is case-sensitive.

In response to 2006Jan/0052
added distinct() to set of things to be DESCRIBEd.

In response to 2006Jan/0051
removed text about possible warning in CONSTRUCT on unbound variable.  Left
to implementations to decide what to do.

In response to 2006Jan/0057
clarified that comments end in 0x#A 0x#D and are treated as whitespace.

Revision 1.605  2006/01/16 17:58:09  aseaborne
~ Fixed the Modifier/Construct example.  Again.
  Maybe the changes will stick this time.

Revision 1.604  2006/01/12 23:54:36  connolly
assign comment on  5.4 Optional matching - formal definition to this spec

Revision 1.603  2006/01/12 11:22:19  aseaborne
+ Aded text to say a query service/processor can reject a query if
  it does not like the dataset description (case of serving
  queries on one dataset only)

Revision 1.602  2006/01/09 14:18:06  aseaborne
In response to: 2005Oct/0068
~ Changed the order of list in Defn Graph Pattern to be in document order.
~ Edited the definition of DISTNICT to be clearer in response to:
~ Definition of CONSTRUCT template: union works on sets.

Revision 1.601  2006/01/09 11:12:22  aseaborne
+ Add informtive reference to Concise Bounded Descriptions for comment 2005Nov/0042

Revision 1.600  2006/01/05 18:18:41  aseaborne
Comment: 2005Dec/0049

Fix for long literals.

Revision 1.599  2005/12/22 11:35:23  aseaborne
~ Edited the target graph definition (removed unnecessary text).
  Linked to it from sec 2.
- Removed red "please comment" text in sec 3.
- Remove sentence referring to triple patterns - we
  now use basic patterns as the building block.

Revision 1.598  2005/12/21 15:19:53  aseaborne
+ Remove told bNode text
+ Remove 3.1.4 which is duplicated by 3.4

Revision 1.597  2005/12/21 09:39:17  eric
~ applied all class="add" and class="remove" changes in section 11

Revision 1.596  2005/12/19 15:14:39  eric
~ (editorial) gathered redundant results

Revision 1.595  2005/12/19 09:28:35  aseaborne
Note that OPTIONAL is left-associative

Revision 1.594  2005/12/19 00:53:07  eric
+ (editorial) add resultSet style to something in the XML ResultSet

Revision 1.593  2005/12/18 21:31:42  eric
+ added support for alternate report formats in examples extraction

Revision 1.592  2005/12/16 23:05:15  eric
+ created prototype and dataExcerpt classes for rq23
- removed extraneous <div class="exampleOuter exampleInner">s
+ grouped all complete examples in the spec with their data and results
+ labeled all incomplete examples with the untested class
+ added examples.css for examples document
~ tweaked examples-extract.xsl to show examples structure in boxes

Revision 1.591  2005/12/16 17:29:42  aseaborne
Use term 'partial pattern rename'

Revision 1.589  2005/12/16 15:34:00  eric
~ working on an XSLT for extracting examples

Revision 1.588  2005/12/16 13:48:39  aseaborne
~ Put in, and highlight, alternative appraochs for told/no-told bnodes.

~ Editorial corrections based on message 2005Dec/0016

- Removed [NAMESPACE] as no longer used

Revision 1.587  2005/12/15 18:42:42  aseaborne
Example tidying in sec 4 due to defining all matching as starting
from basic pattern, not triple patterns.

Revision 1.585  2005/12/15 18:27:41  aseaborne
Fix CVS logs for last 3 versions

Revision 1.584  2005/12/15 18:23:02  aseaborne
~ Fixed 11.4.1 (?name/?givenName inconsistency

Revision 1.582  2005/12/15 16:37:06  aseaborne

~ Changes based on message 2005Dec/0018

~ A.2 Explicitly indicate terminals (correct whitespace handling)

+ Added defintion of "target graph" and incorporated it into the
  matching for GRAPH.

Revision 1.581  2005/12/15 13:35:24  aseaborne
Changes based on message 2005Dec/0018
~ Editorial as noted in the message
~ Changed definition pattern solution and basic graph pattern (in progress)

Revision 1.580  2005/12/13 18:32:39  aseaborne
Added comment about longest rule match wins

Revision 1.579  2005/12/13 14:10:44  eric
~ s/datatype URI/datatype IRI/ in section 11
+ todo to move imported RDF terms to earlier in the document
- removed alternate presentation of extra RDF terms

Revision 1.578  2005/12/13 13:48:00  eric
~ use function class

Revision 1.577  2005/12/13 10:33:14  eric

Revision 1.576  2005/12/12 18:38:53  aseaborne
Changes in response to: 2005Dec/0004
~ Fix TOC
~ Sec 2.8.2: fix examples ; add mailto:
~ Sec 11.4.5: fix example ; add mailto:

Revision 1.574  2005/12/12 18:12:23  eric
+ langMatches(, "*") example per Bjoern's suggestion (response)
~ re-arranged presentation for working group decision on changes

Revision 1.573  2005/12/09 23:08:06  eric
~ moved derived subtypes into intro to Testing Values
+ flagged subtype substitution and numeric type promotion as relevent to SPARQL XQuery operators
+ defined semantics of XPath casts on RDF Terms
- removed casts to IRI and ltrl
+ tied Extensible Value Testing to PrimaryExpression grammar production.
~ reworked examples in Extensible Value Testing

Revision 1.572  2005/11/30 19:51:29  eric
s/xsd:string/simple literal/g — announcement to dawg

Revision 1.571  2005/11/28 13:32:11  eric
- removed sentence mis-defining the promotion of literal to string or numeric.
- removed sentence asserting that "SPARQL has no numeric type test operators".
- (follow-on to substantive) removed a reference to collation in REGEX operator description.
  (should have been done in V1.569)

Revision 1.570  2005/11/27 21:47:56  eric
~ (editorial) fixed link text in pointers to SPARQL operators

Revision 1.569  2005/11/27 18:53:20  eric
~ clarified fn:collation

Revision 1.568  2005/11/25 19:29:42  eric
~ change "sop" class to the more descriptive "SPARQLoperator"

Revision 1.567  2005/11/25 16:49:23  eric
~ moved fn: namespace definition to join the other prefix definitions.
~ updated typographical conventions for chapter 11.
+ point Operator Mapping "XQuery Tests" at the XPath Operator Mapping.

Revision 1.566  2005/11/25 13:51:43  eric
+ mapping of RDF typed literal to XML Schema typed value for XPath functions and operators
~ corrected use of the term RDF URI reference
+ imported definition of rdfs:Datatype
~ clarified definition of numeric
~ reused term RDF term from earlier in the document

Revision 1.565  2005/11/25 01:19:21  eric
~ reformatted 11 Testing Values
+ clarified Operand Data Types

! published fifth WD