SELECT Expressions


The SELECT form of results returns variables and their bindings directly. The syntax SELECT * is an abbreviation that selects all of the variables in a query.


The SELECT form of results returns variables and their bindings directly. It combines the operations of introducing new variable bindings into a query solution and projecting the required variables.

@@Grammar refers to SPARQL 1.0 only

Grammar rule:
[5] SelectQuery ::= <span>'SELECT'</span> ( <span>'DISTINCT'</span> | <span> 'REDUCED'</span> )? ( Var+ | <span>'*'</span> )
DatasetClause* WhereClause SolutionModifier


Specific variables and their bindings are returned when a list of variable names is given in the SELECT clause. The syntax SELECT * is an abbreviation that selects all of the variables that could be bound in a query. [@@ excludes variables only in FILTERs and (NOT) EXISTS clauses]

@prefix  foaf:  <> .

_: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:    <>
SELECT ?nameX ?nameY ?nickY
  { ?x foaf:knows ?y ;
       foaf:name ?nameX .
    ?y foaf:name ?nameY .
    OPTIONAL { ?y foaf:nick ?nickY }

Result sets can be accessed by a 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 gives for this example:

<?xml version="1.0"?>
<sparql xmlns="">
    <variable name="nameX"/>
    <variable name="nameY"/>
    <variable name="nickY"/>

      <binding name="nameX">

      <binding name="nameY">

      <binding name="nameX">
      <binding name="nameY">

      <binding name="nickY">


SELECT Expressions

As well as choosing which variables from the pattern matching are included in the results, the SELECT clause can also introduce new variables, together with an expression that gives the value of binding for that variable.  The expression combines variable bindings already in the query solution to produce a new value. The new variable is introduced using the keyword AS; it must not already be potentially bound.



@prefix dc:   <> .
@prefix :     <> .
@prefix ns:   <> .

:book1  dc:title  "SPARQL Tutorial" .
:book1  ns:price  42 .
:book1  ns:discount 0.1 .

:book2  dc:title  "The Semantic Web" .
:book2  ns:price  23 .
:book2  ns:discount 0 .	


PREFIX  dc:  <>
PREFIX  ns:  <>
SELECT  ?title (?p*(1-?discount) AS ?price)
   { ?x ns:price ?p .
     ?x dc:title ?title . 
     ?x ns:discount ?discount 


title price
 "The Semantic Web" 23
"SPARQL Tutorial" 37.8

Variables can be also be used in expressions if they are introduced as to the earlier, syntactically, in the same SELCT clause:

PREFIX  dc:  <>
PREFIX  ns:  <>
SELECT  ?title (?p AS ?fullPrice) (?fullPrice*(1-?discount) AS ?customerPrice)
   { ?x ns:price ?p .
     ?x dc:title ?title . 
     ?x ns:discount ?discount 


title fullPrice customerPrice
 "The Semantic Web" 23 23
"SPARQL Tutorial" 42 37.8


Definition: Extend

Let μ be a solution mapping, Ω a multiset of solution mappings, var a variable and expr be an expression [@@link], then we define:

extend(μ, var, expr) = { (var,value) | var not in dom(μ) and value = eval(expr) } union { (x,y) | (x,y) in μ }

extend(μ, var, expr) = μ if var not in dom(μ) and eval(expr) is an error

extend is undefined when var in dom(μ).

extend(Ω , var, term) = { extend(μ, var, term) | μ in Ω }

@@It's a syntax error, see below, to use var if var can be in dom(μ).  But it might be better to define this case as well for generality.


Evaluation rules are a technical artifact needed to ensure that extend is called with the result of evaluating a pattern and not the pattern itself.

Definition: Evaluation of Extend
eval(D(G), extend(var, expr, P)) = extend(var, expr , eval(D(G), P))


Mapping from Abstract Syntax to Algebra

This will replace the translation step in SPARQL 1.0.

We have two forms of the abstract syntax to consider:

SELECT selItem ... { pattern }
SELECT * { pattern }


Let X := algebra from earlier steps
Let VS := list of all variables visible in the pattern,
          so restricted by sub-SELECT projected variables and GROUP BY variables.
    Not visible: only in filter, exists/not exists, masked by a subselect, non-projected GROUP variables.

Let P := [], a list of variable names
Let E := [], a list of pairs of the form (expression, variable)
If  "SELECT *" then P := VS

If  "SELECT selItem ...:" then  
  for each selItem:
    IF selItem is a variable THEN
      P := P append variable 
    IF selItem is (expr AS variable) THEN 
       variable must not appear in VS; if it does then generate a syntax error and stop
       P: = P append variable
 	   E := E append (expr, variable) 

for each pair (var, expr) in E:
  X := extend(X, var, expr)
X := project(X, P)
Result is X  

The syntax error arises for use of a variable in as the target of AS (e.g. ... AS ?x) when the variable is used inside the WHERE clause of the SELECT.