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. The solution sequence from matching the query pattern is a collection formed from the solutions of the query pattern with no defined order.
A solution sequence modifier is one of:
Modifiers are applied in the order given by the list above.
[5] |
SelectQuery |
::= | 'SELECT' (
'DISTINCT' | 'LOOSE' )? ( Var+ | '*'
) DatasetClause*
WhereClause SolutionModifier |
[14] |
SolutionModifier |
::= | OrderClause?
LimitOffsetClauses? |
[15] |
LimitOffsetClauses |
::= | ( LimitClause
OffsetClause? | OffsetClause
LimitClause? ) |
[16] |
OrderClause |
::= | 'ORDER'
'BY' OrderCondition+ |
[17] |
OrderCondition |
::= | ( ( 'ASC' |
'DESC' ) BrackettedExpression ) |
[18] |
LimitClause |
::= | 'LIMIT'
INTEGER |
[19] |
OffsetClause |
::= | 'OFFSET'
INTEGER |
The ORDER BY clause establishes the order of a solution sequence.
Following the ORDER BY clause is a sequence of order comparators, composed of an expression and an optional order modifier (either ASC() or DESC()). Each ordering comparator is either ASCENDING (indicated by the ASC() modifier, or no modifier) or DESCENDING (indicated by the DESC() modifier).
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 }
ORDER BY DESC(?emp)
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE { ?x foaf:name ?name ; :empId ?emp }
ORDER BY ?name DESC(?emp)
The "<" operator (see the Operator Mapping) defines
the relative order of pairs of numerics, simple literals, xsd:strings, xsd:booleans
and xsd:dateTimes. IRIs are ordered by comparing their codepoint representations with
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
consistent slicing of query solutions using LIMIT and OFFSET.
xsd:string
of the same lexical form.The ASCENDING order of two solutions with respect to an ordering comparator is established by substituting the solution bindings into the expressions and comparing them with the "<" operator. The DESCENDING order is the reverse of the ASCENDING order.
The relative order of two solutions is the relative order of the two solutions with respect to the first ordering comparator in the sequence. For solutions where the substitutions of the solution bindings produce the same RDF term, the order is the relative order of the two solutions with respect to the next ordering comparator. The relative order of two solutions is undefined if no order expression evaluated for the two solutions produces a distinct RDF term.
Ordering a sequence of solutions always results in a sequence with the same number of solutions in it.
Using ORDER BY on a solution sequence for a CONSTRUCT or
DESCRIBE query has no direct effect because only SELECT returns
a sequence of results. Used in combination with LIMIT and OFFSET,
ORDER BY can be used to return results generated from a different slice of the solution sequence.
[16] |
OrderClause |
::= | 'ORDER' 'BY' OrderCondition+ |
[17] |
OrderCondition |
::= | ( ( 'ASC' | 'DESC' ) BrackettedExpression ) |
[18] |
LimitClause |
::= | 'LIMIT' INTEGER |
[19] |
OffsetClause |
::= | 'OFFSET' INTEGER |
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.
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
WHERE
{ ?x foaf:name ?name }
| name |
|---|
| "Bob" |
| "Alice" |
The solution sequence with no DISTINCT or LOOSE modifier is defined by the SPARQL algebra in 12 Definition of SPARQL:
@prefix foaf: <http://xmlns.com/foaf/0.1/> . _:x foaf:name "Alice" . _:x foaf:mbox <mailto:alice@example.com> . _:y foaf:name "Alice" . _:y foaf:mbox <mailto:asmith@example.com> . _:z foaf:name "Alice" . _:z foaf:mbox <mailto:alice.smith@example.com> .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name WHERE { ?x foaf:name ?name }
| name |
|---|
| "Alice" |
| "Alice" |
| "Alice" |
The DISTINCT solution modifier eliminates duplicate solutions. Specifically, each solution that binds the same variables to the same RDF terms as another solution is eliminated from the solution set.
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?name WHERE { ?x foaf:name ?name }
| name |
|---|
| "Alice" |
If DISTINCT and LIMIT or OFFSET
are specified, then duplicates are eliminated before the limit or offset is applied.
While the DISTINCT modifier ensures that duplicate solutions are eliminated from the solution set, LOOSE simply permits them to be eliminated. The cardinality of any set of variable bindings in an LOOSE solution set is at least one and not more than the cardinality of the solution set with no DISTINCT or LOOSE modifier. For example, the query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT LOOSE ?name WHERE { ?x foaf:name ?name }
may have one, two (shown here) or three solutions:
| name |
|---|
| "Alice" |
| "Alice" |
OFFSET causes the solutions generated to start after the specified
number of solutions. An OFFSET of zero has no effect.
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
LIMIT 5
OFFSET 10
The LIMIT clause 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 }
LIMIT 20
A limit of 0 would cause no results to be returned. A limit may not be negative.
The EBNF notation used in the grammar is defined in Extensible Markup Language (XML) 1.1 [XML11] section 6 Notation.
Keywords are matched in a case-insensitive manner with the exception of 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).
Keywords:
| BASE | SELECT | ORDER BY | FROM | GRAPH | STR | isURI |
| PREFIX | CONSTRUCT | LIMIT | FROM NAMED | OPTIONAL | LANG | isIRI |
| DESCRIBE | OFFSET | WHERE | UNION | LANGMATCHES | isLITERAL | |
| ASK | DISTINCT | FILTER | DATATYPE | REGEX | ||
| a | BOUND | true | ||||
| sameTERM | false |
Escape sequences are case sensitive.
When choosing a rule to match, the longest match is chosen.
Notes:
AdditiveExpression grammar rule
allows for this by covering the the two cases of an expression followed by a
signed number. These produce an addition or substraction of the unsigned
number as appropriate.Some grammar files for some commonly used tools are available here (parsers/).