Skip to content

Latest commit

 

History

History
1705 lines (1424 loc) · 33.9 KB

sparql_spec.md

File metadata and controls

1705 lines (1424 loc) · 33.9 KB

#SPARQL 1.1 specification

An attempt to translate all the example queries from the SPARQL 1.1 specification from W3C into Matsu syntax. This document corresponds with the tests in /test/boutros/matsu/w3c_query_test.clj.

The following namespaces are assumed to be registered:

@prefixes
=> {:foaf    "<http://xmlns.com/foaf/0.1/>"
    :org     "<http://example.com/ns#>"
    :dc      "<http://purl.org/dc/elements/1.1/>"
    :ns      "<http://example.org/ns#>"
    :data    "<http://example.org/foaf/>"
    :vcard   "<http://www.w3.org/2001/vcard-rdf/3.0#>"
    :app     "<http://example.org/ns#>"
    :xsd     "<http://www.w3.org/2001/XMLSchema#>"
    :ent     "<http://org.example.com/employees#>"
    :a       "<http://www.w3.org/2000/10/annotation-ns#>"
    :t       "<http://example.org/types#>"
    :eg      "<http://biometrics.example/ns#>"
    :rdf     "<http://www.w3.org/1999/02/22-rdf-syntax-ns#>"}

2 Making Simple Queries (Informative)

2.1 Writing a Simple Query

SELECT ?title
WHERE
{
  <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> ?title .
}
(query
  (select :title)
  (where (URI. "http://example.org/book/book1") (URI. "http://example.org/book/book1") :title) \.))

2.2 Multiple Matches

PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE
  { ?x foaf:name ?name .
    ?x foaf:mbox ?mbox }
(query
  (select :name :mbox)
  (where :x [:foaf :name] :name \.
         :x [:foaf :mbox] :mbox))

2.3 Matching RDF Literals

SELECT ?v WHERE { ?v ?p "cat" }
(query
  (select :v)
  (where :v :p "cat"))
SELECT ?v WHERE { ?v ?p "cat"@en }
(query
  (select :v)
  (where :v :p ["cat" :en]))
SELECT ?v WHERE { ?v ?p 42 }
(query
  (select :v)
  (where :v :p 42))
SELECT ?v WHERE { ?v ?p "abc"^^<http://example.org/datatype#specialDatatype> }
(query
  (select :v)
  (where :v :p ["abc" "<http://example.org/datatype#specialDatatype>"]))

2.4 Blank Node Labels in Query Results

PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
SELECT ?x ?name
WHERE  { ?x foaf:name ?name }
(query
  (select :x :name)
  (where :x [:foaf :name] :name))

2.5 Creating Values with Expressions

PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
SELECT ( CONCAT(?G, " ", ?S) AS ?name )
WHERE  { ?P foaf:givenName ?G ; foaf:surname ?S }
(query
  (select [(concat :G " " :S) :name])
  (where :P [:foaf :givenName] :G
         \; [:foaf :surname] :S))
PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE  {
   ?P foaf:givenName ?G ;
      foaf:surname ?S
   BIND(CONCAT(?G, " ", ?S) AS ?name)
}
(query
  (select :name)
  (where :P [:foaf :givenName] :G
         \; [:foaf :surname] :S
         (bind [(concat :G " " :S) :name])))

2.6 Building RDF Graphs

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

CONSTRUCT { ?x foaf:name ?name }
WHERE  { ?x org:employeeName ?name }
(query
  (construct :x [:foaf :name] :name)
  (where :x [:org :employeeName] :name))

3 RDF Term Constraints (Informative)

3.1 Restricting the Value of Strings

PREFIX  dc:  <http://purl.org/dc/elements/1.1/>
SELECT  ?title
WHERE   { ?x dc:title ?title
          FILTER regex(?title, "^SPARQL")
        }
(query
  (select :title)
  (where :x [:dc :title] :title
         (filter (regex :title "^SPARQL"))))
PREFIX  dc:  <http://purl.org/dc/elements/1.1/>
SELECT  ?title
WHERE   { ?x dc:title ?title
          FILTER regex(?title, "web", "i" )
        }
(query
  (select :title)
  (where :x [:dc :title] :title
         (filter (regex :title "web" "i"))))

3.2 Restricting Numeric Values

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.5)
          ?x dc:title ?title . }
(query
  (select :title :price)
    (where :x [:ns :price] :price \.
           (filter :price < 30.5)
           :x [:dc :title] :title \.))

SPARQL Syntax

4.2 Syntax for Triple Patterns

PREFIX  dc: <http://purl.org/dc/elements/1.1/>
SELECT  ?title
WHERE   { <http://example.org/book/book1> dc:title ?title }
(query
  (select :title)
  (where (URI. "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 }
(query
  ...)

Currently not possible, since Clojure doesn't allow the keyword ::. Use base instead, see below.

Dollar-prefixed variables are not supported either ($title is equal to ?title anyway).

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

SELECT  $title
WHERE   { <book1>  dc:title  ?title }
  (query
    (base (URI. "http://example.org/book"))
    (select :title)
    (where [book1] [:dc :title] :title))

5 Graph Patterns

5.2 Group Graph Patterns

PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE  {
          ?x foaf:name ?name .
          ?x foaf:mbox ?mbox .
       }
(query
  (select :name :mbox)
  (where :x [:foaf :name] :name \.
         :x [:foaf :mbox] :mbox \.))
PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE  { { ?x foaf:name ?name . }
         { ?x foaf:mbox ?mbox . }
       }
(query
  (select :name :mbox)
  (where (group :x [:foaf :name] :name \.)
         (group :x [:foaf :mbox] :mbox \.)))

6 Including Optional Values

6.1 Optional Pattern Matching

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE  { ?x foaf:name  ?name .
         OPTIONAL { ?x  foaf:mbox  ?mbox }
       }
(query
  (select :name :mbox)
  (where :x [:foaf :name] :name \.
         (optional :x [:foaf :mbox] :mbox)))

6.2 Constraints in Optional Pattern Matching

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) }
        }
(query
  (select :title :price)
  (where :x [:dc :title] :title \.
         (optional :x [:ns :price] :price \. (filter :price < 30))))

6.3 Multiple Optional Graph Patterns

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
  (select :name :mbox :hpage)
  (where :x [:foaf :name] :name \.
         (optional :x [:foaf :mbox] :mbox) \.
         (optional :x [:foaf :homepage] :hpage)))

7 Matching Alternatives

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
  (select :title)
  (where (union (group :book [:dc10 :title] :title)
                (group :book [:dc11 :title] :title))))
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 } }
(query
  (select :x :y)
  (where (union (group :book [:dc10 :title] :x)
                (group :book [:dc11 :title] :y))))
PREFIX dc10:  <http://purl.org/dc/elements/1.0/>
PREFIX dc11:  <http://purl.org/dc/elements/1.1/>

SELECT ?title ?author
WHERE  { { ?book dc10:title ?title .  ?book dc10:creator ?author }
         UNION
         { ?book dc11:title ?title .  ?book dc11:creator ?author }
       }
(query
  (select :title :author)
  (where (union (group :book [:dc10 :title] :title \. :book [:dc10 :creator] :author)
                (group :book [:dc11 :title] :title \. :book [:dc11 :creator] :author))))

8 Negation

8.1 Filtering Using Graph Patterns

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

SELECT ?person
WHERE
{
    ?person rdf:type  foaf:Person .
    FILTER NOT EXISTS { ?person foaf:name ?name }
}
(query
  (select :person)
  (where :person [:rdf :type] [:foaf :Person] \.
         (filter-not-exists :person [:foaf :name] :name)))
PREFIX  rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  foaf:   <http://xmlns.com/foaf/0.1/>

SELECT ?person
WHERE
{
    ?person rdf:type  foaf:Person .
    FILTER EXISTS { ?person foaf:name ?name }
}
(query
  (select :person)
  (where :person [:rdf :type] [:foaf :Person] \.
         (filter-exists :person [:foaf :name] :name)))

8.2 Removing Possible Solutions

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

SELECT DISTINCT ?s
WHERE {
   ?s ?p ?o .
   MINUS {
      ?s foaf:givenName "Bob" .
   }
}
(query
  (select-distinct :s)
  (where :s :p :o \.
         (minus :s [:foaf :givenName] "Bob" \.)))

8.3 Relationship and differences between NOT EXISTS and MINUS

SELECT *
{
  ?s ?p ?o
  FILTER NOT EXISTS { ?x ?y ?z }
}

Use the where- function if you want to omit the WHERE keyword (which is optional in SPARQL):

(query
  (select *)
  (where- :s :p :o
         (filter-not-exists :x :y :z)))
SELECT *
{
   ?s ?p ?o
   MINUS
     { ?x ?y ?z }
}
(query
  (select *)
  (where :s :p :o
         (minus :x :y :z)))
PREFIX : <http://example/>
SELECT *
{
  ?s ?p ?o
  FILTER NOT EXISTS { :a :b :c }
}

Using BASE URI instead of :

(query
  (base (URI. "http://example/"))
  (select *)
  (where :s :p :o
         (filter-not-exists [:a] [:b] [:c])))
PREFIX : <http://example/>
SELECT *
{
  ?s ?p ?o
  MINUS { :a :b :c }
}
(query
  (base (URI. "http://example/"))
  (select *)
  (where :s :p :o
         (minus [:a] [:b] [:c])))
PREFIX : <http://example.com/>
SELECT * WHERE {
        ?x :p ?n
        FILTER NOT EXISTS {
                ?x :q ?m .
                FILTER(?n = ?m)
        }
}
(query
  (base (URI. "http://example.com/"))
  (select *)
  (where :x [:p] :n
         (filter-not-exists :x [:q] :m \.
                            (filter :n = :m))))
PREFIX : <http://example/>
SELECT * WHERE {
        ?x :p ?n
        MINUS {
                ?x :q ?m .
                FILTER(?n = ?m)
        }
}
(query
  (base (URI. "http://example.com/"))
  (select *)
  (where :x [:p] :n
         (minus :x [:q] :m \.
                (filter :n = :m))))

9 Property Paths

I haven't decided how to implement the propert path syntax yet.

10 Assignment

10.1 BIND: Assigning to Variables

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

SELECT  ?title ?price
{  { ?x ns:price ?p .
     ?x ns:discount ?discount
     BIND(?p*(1-?discount) AS ?price)
   }
   {?x dc:title ?title . }
   FILTER(?price < 20)
}

Not quite possible yet without resorting to raw:

(query
  (select :title :price)
  (where (group :x [:ns :price] :p \.
                :x [:ns :discount] :discount
                (bind [(raw "?p*(1-?discount)") :price]))
         (group :x [:dc :title] :title \.)
         (filter :price < 20)))
PREFIX dc:   <http://purl.org/dc/elements/1.1/>
PREFIX :     <http://example.org/book/>
PREFIX ns:   <http://example.org/ns#>

SELECT ?book ?title ?price
{
   VALUES ?book { :book1 :book3 }
   ?book dc:title ?title ;
         ns:price ?price .
}
(query ...) ;currently not possible

10.2 VALUES: Providing inline data

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

SELECT ?book ?title ?price
{
   ?book dc:title ?title ;
         ns:price ?price .
   VALUES (?book ?title)
   { (UNDEF "SPARQL Tutorial")
     (:book2 UNDEF)
   }
}
(query ...) ;currently not possible
PREFIX dc:   <http://purl.org/dc/elements/1.1/>
PREFIX :     <http://example.org/book/>
PREFIX ns:   <http://example.org/ns#>

SELECT ?book ?title ?price
{
   ?book dc:title ?title ;
         ns:price ?price .
}
VALUES (?book ?title)
{ (UNDEF "SPARQL Tutorial")
  (:book2 UNDEF)
}
(query ...) ;currently not possible

11 Aggregates

11.1 Aggregate Example

PREFIX : <http://books.example/>
SELECT (SUM(?lprice) AS ?totalPrice)
WHERE {
  ?org :affiliates ?auth .
  ?auth :writesBook ?book .
  ?book :price ?lprice .
}
GROUP BY ?org
HAVING (SUM(?lprice) > 10)
(query
  (base (URI. "http://books.example/"))
  (select [(sum :lprice) :totalPrice])
  (where :org [:affiliates] :auth \.
         :auth [:writesBook] :book \.
         :book [:price] :lprice \.)
  (group-by :org)
  (having (sum :lprice) > 10))

11.2 GROUP BY

SELECT (AVG(?y) AS ?avg)
WHERE {
  ?a :x ?x ;
     :y ?y .
}
GROUP BY ?x
(query
  (select [(avg :y) :avg])
  (where :a [:x] :x
         \; [:y] :y \.)
  (group-by :x))

11.3 HAVING

PREFIX : <http://data.example/>
SELECT (AVG(?size) AS ?asize)
WHERE {
  ?x :size ?size
}
GROUP BY ?x
HAVING(AVG(?size) > 10)
(query
  (base (URI. "http://data.example/"))
  (select [(avg :size) :asize])
  (where :x [:size] :size)
  (group-by :x)
  (having (avg :size) > 10))

11.4 Aggregate Projection Restrictions

PREFIX : <http://example.com/data/#>
SELECT ?x (MIN(?y) * 2 AS ?min)
WHERE {
  ?x :p ?y .
  ?x :q ?z .
} GROUP BY ?x (STR(?z))

Not quite there yet (must rethink the query data structure to better deal with expressions):

(query
  (base (URI. "http://example.com/data/#"))
  (select :x [(raw "MIN(?y) * 2") :min])
  (where :x [:p] :y \. :x [:q] :z \.)
  (group-by :x (raw "(STR(?z))")))

11.5 Aggregate Example (with errors)

PREFIX : <http://example.com/data/#>
SELECT ?g (AVG(?p) AS ?avg) ((MIN(?p) + MAX(?p)) / 2 AS ?c)
WHERE {
  ?g :p ?p .
}
GROUP BY ?g
(query
  (base (URI. "http://example.com/data/#"))
  (select :g [(raw "AVG(?p) AS ?avg) ( (MIN(?p) + MAX(?p)) / 2") :c])
  (where :g [:p] :p \.)
  (group-by :g))

12 Subqueries

PREFIX : <http://people.example/>
PREFIX : <http://people.example/>
SELECT ?y ?minName
WHERE {
  :alice :knows ?y .
  {
    SELECT ?y (MIN(?name) AS ?minName)
    WHERE {
      ?y :name ?name .
    } GROUP BY ?y
  }
}

Not supported yet, use raw.

(query ...)

13 RDF Dataset

13.2 Specifying RDF Datasets

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT  ?name
FROM    <http://example.org/foaf/aliceFoaf>
WHERE   { ?x foaf:name ?name }
(query
  (select :name)
  (from (URI. "http://example.org/foaf/aliceFoaf"))
  (where :x [:foaf :name] :name))
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>
WHERE
{
   ?g dc:publisher ?who .
   GRAPH ?g { ?x foaf:mbox ?mbox }
}
(query
  (select :who :g :mbox)
  (from (URI. "http://example.org/dft.ttl"))
  (from-named (URI. "http://example.org/bob")
              (URI. "http://example.org/alice"))
  (where :g [:dc :publisher] :who \.
         (graph :g :x [:foaf :mbox] :mbox)))

13.3 Querying the Dataset

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

SELECT ?src ?bobNick
FROM NAMED <http://example.org/foaf/aliceFoaf>
FROM NAMED <http://example.org/foaf/bobFoaf>
WHERE
  {
    GRAPH ?src
    { ?x foaf:mbox <mailto:bob@work.example> .
      ?x foaf:nick ?bobNick
    }
  }
(query
  (select :src :bobNick)
  (from-named (URI. "http://example.org/foaf/aliceFoaf")
              (URI. "http://example.org/foaf/bobFoaf"))
  (where (graph :src
                :x [:foaf :mbox] (URI. "mailto:bob@work.example") \.
                :x [:foaf :nick] :bobNick)))
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX data: <http://example.org/foaf/>

SELECT ?nick
FROM NAMED <http://example.org/foaf/aliceFoaf>
FROM NAMED <http://example.org/foaf/bobFoaf>
WHERE
  {
     GRAPH data:bobFoaf {
         ?x foaf:mbox <mailto:bob@work.example> .
         ?x foaf:nick ?nick }
  }
(query
  (select :nick)
  (from-named (URI. "http://example.org/foaf/aliceFoaf")
              (URI. "http://example.org/foaf/bobFoaf"))
  (where (graph [:data :bobFoaf]
                :x [:foaf :mbox] (URI. "mailto:bob@work.example") \.
                :x [:foaf :nick] :nick)))
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
FROM NAMED <http://example.org/foaf/aliceFoaf>
FROM NAMED <http://example.org/foaf/bobFoaf>
WHERE
{
  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
  }
}
(query
  (select :mbox :nick :ppd)
  (from-named (URI. "http://example.org/foaf/aliceFoaf")
              (URI. "http://example.org/foaf/bobFoaf"))
  (where
    (graph [:data :aliceFoaf]
           :alice [:foaf :mbox] (URI. "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)))
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc:   <http://purl.org/dc/elements/1.1/>

SELECT ?name ?mbox ?date
WHERE
  {  ?g dc:publisher ?name ;
        dc:date ?date .
    GRAPH ?g
      { ?person foaf:name ?name ; foaf:mbox ?mbox }
  }
(query
  (select :name :mbox :date)
  (where
    :g [:dc :publisher] :name
    \; [:dc :date] :date \.
    (graph :g
           :person [:foaf :name] :name
           \; [:foaf :mbox] :mbox)))

14 Basic Federated Query

Not supported yet.

15 Solution Sequences and Modifiers

15.1 ORDER BY

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

SELECT ?name
WHERE { ?x foaf:name ?name }
ORDER BY ?name
(query
  (select :name)
  (where :x [:foaf :name] :name)
  (order-by :name))
PREFIX     :    <http://example.org/ns#>
PREFIX foaf:    <http://xmlns.com/foaf/0.1/>

SELECT ?name
WHERE { ?x foaf:name ?name ; :empId ?emp }
ORDER BY DESC(?emp)
(query
  (base (URI. "http://example.org/ns#"))
  (select :name)
  (where :x [:foaf :name] :name
         \; [:empId] :emp)
  (order-by (desc :emp))) ; or (order-by-desc :emp)
PREFIX     :    <http://example.org/ns#>
PREFIX foaf:    <http://xmlns.com/foaf/0.1/>

SELECT ?name
WHERE { ?x foaf:name ?name ; :empId ?emp }
ORDER BY ?name DESC(?emp)
(query
  (base (URI. "http://example.org/ns#"))
  (select :name)
  (where :x [:foaf :name] :name
         \; [:empId] :emp)
  (order-by :name (desc :emp)))

15.2 Projection

PREFIX foaf:       <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE
 { ?x foaf:name ?name }
(query
  (select :name)
  (where :x [:foaf :name] :name))

15.3 Duplicate Solutions

PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?name WHERE { ?x foaf:name ?name }
(query
  (select-distinct :name)
  (where :x [:foaf :name] :name))
PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
SELECT REDUCED ?name WHERE { ?x foaf:name ?name }
(query
  (select-reduced :name)
  (where :x [:foaf :name] :name))

15.4 OFFSET

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

SELECT  ?name
WHERE   { ?x foaf:name ?name }
ORDER BY ?name
LIMIT   5
OFFSET  10
(query
  (select :name)
  (where :x [:foaf :name] :name)
  (order-by :name)
  (limit 5)
  (offset 10))

15.5 LIMIT

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

SELECT ?name
WHERE { ?x foaf:name ?name }
LIMIT 20
(query
  (select :name)
  (where :x [:foaf :name] :name)
  (limit 20))

16 Query Forms

16.1 SELECT

PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
SELECT ?nameX ?nameY ?nickY
WHERE
  { ?x foaf:knows ?y ;
       foaf:name ?nameX .
    ?y foaf:name ?nameY .
    OPTIONAL { ?y foaf:nick ?nickY }
  }
(query
  (select :nameX :nameY :nickY)
  (where :x [:foaf :knows] :y
         \; [:foaf :name] :nameX \.
         :y [:foaf :name] :nameY \.
         (optional :y [:foaf :nick] :nickY)))
PREFIX  dc:  <http://purl.org/dc/elements/1.1/>
PREFIX  ns:  <http://example.org/ns#>
SELECT  ?title (?p*(1-?discount) AS ?price)
{ ?x ns:price ?p .
  ?x dc:title ?title .
  ?x ns:discount ?discount
}
(query
  (select :title [(raw "?p*(1-?discount)") :price])
  (where :x [:ns :price] :p \.
         :x [:dc :title] :title \.
         :x [:ns :discount] :discount))
PREFIX  dc:  <http://purl.org/dc/elements/1.1/>
PREFIX  ns:  <http://example.org/ns#>
SELECT  ?title (?p AS ?fullPrice) (?fullPrice*(1-?discount) AS ?customerPrice)
{ ?x ns:price ?p .
   ?x dc:title ?title .
   ?x ns:discount ?discount
}
(query
  (select :title [(raw "?p AS ?fullPrice) (?fullPrice*(1-?discount)") :customerPrice])
  (where :x [:ns :price] :p \.
         :x [:dc :title] :title \.
         :x [:ns :discount] :discount))

16.2 CONSTRUCT

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 }
(query
  (construct (URI. "http://example.org/person#Alice") [:vcard :FN] :name)
  (where :x [:foaf :name] :name))
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 }
WHERE
 {
    { ?x foaf:firstname ?gname } UNION  { ?x foaf:givenname   ?gname } .
    { ?x foaf:surname   ?fname } UNION  { ?x foaf:family_name ?fname } .
 }

Blank nodes are created with double brackets:

(query
  (construct :x [:vcard :N] [[:v]] \.
             [[:v]] [:vcard :givenName] :gname \.
             [[:v]] [:vcard :familyName] :fname)
  (where (union
           (group :x [:foaf :firstname] :gname)
           (group :x [:foaf :givenname] :gname)) \.
         (union
           (group :x [:foaf :surname] :fname)
           (group :x [:foaf :family_name] :fname)) \.))
CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/aGraph> { ?s ?p ?o } . }
(query
  (construct :s :p :o)
  (where (graph (URI. "http://example.org/aGraph") :s :p :o ) \.))
PREFIX  dc: <http://purl.org/dc/elements/1.1/>
PREFIX app: <http://example.org/ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

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 ) .
 }
(query
  (construct :s :p :o)
  (where
    (graph :g :s :p :o) \.
    :g [:dc :publisher] (URI. "http://www.w3.org/") \.
    :g [:dc :date] :date \.
    (filter (raw "app:customDate(?date)") > ["2005-02-28T00:00:00Z" "xsd:dateTime"]) \.))
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX site: <http://example.org/stats#>

CONSTRUCT { [] foaf:name ?name }
WHERE
{ [] foaf:name ?name ;
     site:hits ?hits .
}
ORDER BY desc(?hits)
LIMIT 2
(query
  (construct [[]] [:foaf :name] :name)
  (where [[]] [:foaf :name] :name
         \; [:site :hits] :hits \.)
  (order-by-desc :hits)
  (limit 2))
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
CONSTRUCT WHERE { ?x foaf:name ?name }
(query
  (construct)
  (where :x [:foaf :name] :name))
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

CONSTRUCT { ?x foaf:name ?name }
WHERE
{ ?x foaf:name ?name }
(query
  (construct :x [:foaf :name] :name)
  (where :x [:foaf :name] :name))

16.3 ASK

PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
ASK  { ?x foaf:name  "Alice" }
(query
  (ask :x [:foaf :name] "Alice"))
PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
ASK  { ?x foaf:name  "Alice" ;
          foaf:mbox  <mailto:alice@work.example> }
(query
  (ask :x [:foaf :name] "Alice"
       \; [:foaf :mbox] (URI. "mailto:alice@work.example")))

16.4 DESCRIBE (Informative)

DESCRIBE <http://example.org/>
(query
  (describe (URI. "http://example.org/")))
PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
DESCRIBE ?x
WHERE    { ?x foaf:mbox <mailto:alice@org> }
(query
  (describe :x)
  (where :x [:foaf :mbox] (URI. "mailto:alice@org")))
PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
DESCRIBE ?x
WHERE    { ?x foaf:name "Alice" }
(query
  (describe :x)
  (where :x [:foaf :name] "Alice"))
PREFIX foaf:   <http://xmlns.com/foaf/0.1/>
DESCRIBE ?x ?y <http://example.org/>
WHERE    {?x foaf:knows ?y}
(query
  (describe :x :y (URI. "http://example.org/"))
  (where :x [:foaf :knows] :y))
PREFIX ent:  <http://org.example.com/employees#>
DESCRIBE ?x WHERE { ?x ent:employeeId "1234" }
(query
  (describe :x)
  (where :x [:ent :employeeId] "1234"))

17 Expressions and Testing Values

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 ) }
(query
  (select :annot)
  (where :annot [:a :annotates] (URI. "http://www.w3.org/TR/rdf-sparql-query/") \.
         :annot [:dc :date] :date \.
         (filter :date > ["2005-01-01T00:00:00Z" "xsd:dateTime"])))

17.4 Function Definitions

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc:   <http://purl.org/dc/elements/1.1/>
SELECT ?givenName
 WHERE { ?x foaf:givenName  ?givenName .
         OPTIONAL { ?x dc:date ?date } .
         FILTER ( bound(?date) ) }
(query
  (select :givenName)
  (where :x [:foaf :givenName] :givenName \.
         (optional :x [:dc :date] :date) \.
         (filter (bound :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
  (select :name)
  (where :x [:foaf :givenName] :name \.
         (optional :x [:dc :date] :date) \.
         (filter (!bound :date))))
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
  (select :name1 :name2)
  (where :x [:foaf :name] :name1
         \; [:foaf :mbox] :mbox1 \.
         :y [:foaf :name] :name2
         \; [:foaf :mbox] :mbox2 \.
         (filter :mbox1 = :mbox2 && :name1 != :name2)))
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("2005-01-01T00:00:00Z") )
      }
(query ...) ; TODO dateTime
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 (sameTerm(?mbox1, ?mbox2) && !sameTerm(?name1, ?name2))
      }
  (query
  (select :name1 :name2)
  (where :x [:foaf :name] :name1
         \; [:foaf :mbox] :mbox1 \.
         :y [:foaf :name] :name2
         \; [:foaf :mbox] :mbox2 \.
         (filter (same-term :mbox1, :mbox2) && (!same-term :name1 :name2))))
PREFIX  :      <http://example.org/WMterms#>

SELECT ?aLabel1 ?bLabel
WHERE { ?a  :label        ?aLabel .
        ?a  :weight       ?aWeight .
        ?a  :displacement ?aDisp .

        ?b  :label        ?bLabel .
        ?b  :weight       ?bWeight .
        ?b  :displacement ?bDisp .

        FILTER ( sameTerm(?aWeight, ?bWeight) && !sameTerm(?aDisp, ?bDisp)) }
(query
  (base (URI. "http://example.org/WMterms#"))
  (select :aLabel1, :bLabel)
  (where :a [:label] :aLabel \.
         :a [:weight] :aWeight \.
         :a [:displacement] :aDisp \.
         :b [:label] :bLabel \.
         :b [:weight] :bWeight \.
         :b [:displacement] :bDisp \.
         (filter (same-term :aWeight :bWeight) && (!same-term :aDisp :bDisp))))
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
 WHERE { ?x foaf:name  ?name ;
            foaf:mbox  ?mbox .
         FILTER isIRI(?mbox) }
(query
  (select :name :mbox)
  (where :x [:foaf :name] :name
         \; [:foaf :mbox] :mbox \.
         (filter (is-iri :mbox))))
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
  (select :given :family)
  (where :annot [:a :annotates] (URI. "http://www.w3.org/TR/rdf-sparql-query/") \.
         :annot [:dc :creator] :c \.
         (optional :c [:foaf :given] :given
                    \; [:foaf :family] :family) \.
         (filter (is-blank :c))))
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE { ?x foaf:name  ?name ;
        foaf:mbox  ?mbox .
        FILTER isLiteral(?mbox) }
(query
  (select :name :mbox)
  (where :x [:foaf :name] :name
         \; [:foaf :mbox] :mbox \.
         (filter (is-literal :mbox))))
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$") }

I didn't want to do without clojure.core/str, so I named the str function str2:

(query
  (select :name :mbox)
  (where :x [:foaf :name] :name
         \; [:foaf :mbox] :mbox \.
         (filter (regex (str2 :mbox) "@work\\.example$"))))
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
 WHERE { ?x foaf:name  ?name ;
            foaf:mbox  ?mbox .
         FILTER ( lang(?name) = "es" ) }
(query
  (select :name :mbox)
  (where :x [:foaf :name] :name
          \; [:foaf :mbox] :mbox \.
          (filter (lang :name) = "es")))
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
  (select :name :shoeSize)
  (where :x [:foaf :name] :name
         \; [:eg :shoeSize] :shoeSize \.
         (filter (datatype :shoeSize) = [:xsd :integer])))
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
  (select :title)
  (where :x [:dc :title] ["That Seventies Show" :en]
         \; [:dc :title] :title \.
         (filter (lang-matches (lang :title) "FR"))))
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?title
 WHERE { ?x dc:title  ?title .
         FILTER langMatches( lang(?title), "*" ) }
(query
  (select :title)
  (where :x [:dc :title] :title \.
         (filter (lang-matches (lang :title) "*"))))
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
 WHERE { ?x foaf:name  ?name
         FILTER regex(?name, "^ali", "i") }
(query
  (select :name)
  (where :x [:foaf :name] :name
         (filter (regex :name "^ali" "i"))))

17.6 Extensible Value Testing

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)) }
(query ...) ; TBD
PREFIX aGeo: <http://example.org/geo#>

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

        ?b aGeo:placeName ?neighbor .
        ?b aGeo:locationX ?bxLoc .
        ?b aGeo:locationY ?byLoc .

        FILTER ( aGeo:distance(?axLoc, ?ayLoc, ?bxLoc, ?byLoc) < 10 ) .
      }
(query ...) ; TBD

18 Definition of SPARQL

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT (SUM(?val) AS ?sum) (COUNT(?a) AS ?count)
WHERE {
  ?a rdf:value ?val .
} GROUP BY ?a
(query
  (select [(sum :val) :sum] [(count :a) :count])
  (where :a [:rdf :value] :val \.)
  (group-by :a))

Thats all, folks!