SPARQL Query Tests

This document consists of the examples from the SPARQL Query specification. The Makefile has a target examples.html which generated this document. Example run:

make examples.html
xsltproc -o examples.html examples-extract.xsl Overview.html

data 2.1 Writing a Simple Query

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

query 2.1 Writing a Simple Query

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "title",;
     rs:solution [
     rs:binding  [ rs:variable "title";
        rs:value "SPARQL Tutorial" ];
];
.

query 2.1.5 Examples of Query Syntax

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

query 2.1.5 Examples of Query Syntax

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

query 2.1.5 Examples of Query Syntax

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

query 2.1.5 Examples of Query Syntax

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

data 2.1.6 Data descriptions used in this document

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

query 2.4 Pattern Solutions

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "?book", "?title",;
     rs:solution [
     rs:binding  [ rs:variable "?book";
        rs:value <http://example.org/book/book1> ];
     rs:binding  [ rs:variable "?book";
        rs:value "SPARQL" ];
];
.

data 2.5 Basic Graph Patterns

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

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

query 2.5 Basic Graph Patterns

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "mbox",;
     rs:solution [
     rs:binding  [ rs:variable "mbox";
        rs:value <mailto:jlow@example.com> ];
];
.

data 2.6 Multiple Matches

@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> .

query 2.6 Multiple Matches

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "name", "mbox",;
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Johnny Lee Outlaw" ];
     rs:binding  [ rs:variable "name";
        rs:value <mailto:jlow@example.com> ];
];
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Peter Goodguy" ];
     rs:binding  [ rs:variable "name";
        rs:value <mailto:peter@example.org> ];
];
.

data 2.7.2 Blank Nodes and Query Results

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

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

query 2.7.2 Blank Nodes and Query Results

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "x", "name",;
     rs:solution [
     rs:binding  [ rs:variable "x";
        rs:value _:c ];
     rs:binding  [ rs:variable "x";
        rs:value "Alice" ];
];
     rs:solution [
     rs:binding  [ rs:variable "x";
        rs:value _:d ];
     rs:binding  [ rs:variable "x";
        rs:value "Bob" ];
];
.

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "x", "name",;
     rs:solution [
     rs:binding  [ rs:variable "x";
        rs:value _:r ];
     rs:binding  [ rs:variable "x";
        rs:value "Alice" ];
];
     rs:solution [
     rs:binding  [ rs:variable "x";
        rs:value _:s ];
     rs:binding  [ rs:variable "x";
        rs:value "Bob" ];
];
.

data 2.9 Querying Reification 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" .

query 2.9 Querying Reification Vocabulary

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

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "book", "title",;
.

query 2.9 Querying Reification 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#> 

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "book", "title",;
     rs:solution [
     rs:binding  [ rs:variable "book";
        rs:value <http://example.org/book/book1> ];
     rs:binding  [ rs:variable "book";
        rs:value "SPARQL Tutorial" ];
];
.

data 3.1 Matching 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 .

query 3.1.1 Matching Integers

SELECT ?v WHERE { ?v ?p 42 }

query 3.1.2 Matching Arbitrary Datatypes

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

query 3.1.3 Matching Language Tags

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

query 3.1.3 Matching Language Tags

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

data 3.2 Value Constraints

@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 .

query 3.2 Value Constraints

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 . }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "title", "price",;
     rs:solution [
     rs:binding  [ rs:variable "title";
        rs:value "The Semantic Web" ];
     rs:binding  [ rs:variable "title";
        rs:value 23 ];
];
.

query 4.1 Group Graph Patterns

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

query 4.1 Group Graph Patterns

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

data 5.1 Optional Pattern Matching

@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" .

query 5.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 }
       }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "name", "mbox",;
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Alice" ];
     rs:binding  [ rs:variable "name";
        rs:value <mailto:alice@example.com> ];
];
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Alice" ];
     rs:binding  [ rs:variable "name";
        rs:value <mailto:alice@work.example> ];
];
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Bob" ];
     rs:binding  [ rs:variable "name";
        rs:value   ];
];
.

data 5.2 Constraints in Optional Pattern Matching

@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 .

query 5.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) }
        }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "title", "price",;
     rs:solution [
     rs:binding  [ rs:variable "title";
        rs:value "SPARQL Tutorial" ];
     rs:binding  [ rs:variable "title";
        rs:value  ];
];
     rs:solution [
     rs:binding  [ rs:variable "title";
        rs:value "The Semantic Web" ];
     rs:binding  [ rs:variable "title";
        rs:value 23 ];
];
.

data 5.3 Multiple 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> .

query 5.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 }
       }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "name", "mbox", "hpage",;
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Alice" ];
     rs:binding  [ rs:variable "name";
        rs:value   ];
     rs:binding  [ rs:variable "name";
        rs:value <http://work.example.org/alice/> ];
];
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Bob" ];
     rs:binding  [ rs:variable "name";
        rs:value <mailto:bob@example.com> ];
     rs:binding  [ rs:variable "name";
        rs:value   ];
];
.

data 5.5 Nested Optional Graph Patterns

@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" .

query 5.5 Nested Optional Graph Patterns

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "foafName", "mbox", "gname", "fname",;
     rs:solution [
     rs:binding  [ rs:variable "foafName";
        rs:value "Alice" ];
     rs:binding  [ rs:variable "foafName";
        rs:value <mailto:alice@work.example> ];
     rs:binding  [ rs:variable "foafName";
        rs:value "Alice" ];
     rs:binding  [ rs:variable "foafName";
        rs:value "Hacker" ];
];
     rs:solution [
     rs:binding  [ rs:variable "foafName";
        rs:value "Bob" ];
     rs:binding  [ rs:variable "foafName";
        rs:value <mailto:bob@work.example> ];
     rs:binding  [ rs:variable "foafName";
        rs:value   ];
     rs:binding  [ rs:variable "foafName";
        rs:value   ];
];
     rs:solution [
     rs:binding  [ rs:variable "foafName";
        rs:value "Ella" ];
     rs:binding  [ rs:variable "foafName";
        rs:value  ];
     rs:binding  [ rs:variable "foafName";
        rs:value "Eleanor" ];
     rs:binding  [ rs:variable "foafName";
        rs:value  ];
];
.

data 6.1 Joining Patterns with UNION

@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" .

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

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

query 6.1 Joining Patterns with UNION

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 } }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "title",;
     rs:solution [
     rs:binding  [ rs:variable "title";
        rs:value  "SPARQL Protocol Tutorial"  ];
];
     rs:solution [
     rs:binding  [ rs:variable "title";
        rs:value "SPARQL" ];
];
     rs:solution [
     rs:binding  [ rs:variable "title";
        rs:value "SPARQL (updated)" ];
];
     rs:solution [
     rs:binding  [ rs:variable "title";
        rs:value "SPARQL Query Language Tutorial" ];
];
.

query 6.1 Joining Patterns with UNION

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 } }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "x", "y",;
     rs:solution [
     rs:binding  [ rs:variable "x";
        rs:value  ];
     rs:binding  [ rs:variable "x";
        rs:value "SPARQL (updated)" ];
];
     rs:solution [
     rs:binding  [ rs:variable "x";
        rs:value  ];
     rs:binding  [ rs:variable "x";
        rs:value "SPARQL Protocol Tutorial" ];
];
     rs:solution [
     rs:binding  [ rs:variable "x";
        rs:value "SPARQL" ];
     rs:binding  [ rs:variable "x";
        rs:value  ];
];
     rs:solution [
     rs:binding  [ rs:variable "x";
        rs:value "SPARQL Query Language Tutorial" ];
     rs:binding  [ rs:variable "x";
        rs:value  ];
];
.

query 6.1 Joining Patterns with UNION

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 }
         UNION
         { ?book dc11:title ?title .  ?book dc11:creator ?author }
       }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "author", "title",;
     rs:solution [
     rs:binding  [ rs:variable "author";
        rs:value  "Alice" ];
     rs:binding  [ rs:variable "author";
        rs:value  "SPARQL Protocol Tutorial" ];
];
     rs:solution [
     rs:binding  [ rs:variable "author";
        rs:value "Bob" ];
     rs:binding  [ rs:variable "author";
        rs:value "SPARQL Query Language Tutorial" ];
];
.

data 7.1 Examples of RDF Datasets

# 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" .

data 7.1 Examples of RDF Datasets

# 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> .

data 7.1 Examples of RDF Datasets

# 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> .

data 7.1 Examples of RDF Datasets

# 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> .

data 7.1 Examples of RDF Datasets

# 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> .

data 7.1 Examples of RDF Datasets

# 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> .

data 8 Querying the Dataset

# 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> .

<http://example.org/foaf/bobFoaf>
     rdf:type      foaf:PersonalProfileDocument .

data 8 Querying the Dataset

# 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" .
<http://example.org/foaf/bobFoaf>
     rdf:type      foaf:PersonalProfileDocument .

query 8.1 Accessing Graph Names

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

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "src", "bobNick",;
     rs:solution [
     rs:binding  [ rs:variable "src";
        rs:value <http://example.org/foaf/aliceFoaf> ];
     rs:binding  [ rs:variable "src";
        rs:value "Bobby" ];
];
     rs:solution [
     rs:binding  [ rs:variable "src";
        rs:value <http://example.org/foaf/bobFoaf> ];
     rs:binding  [ rs:variable "src";
        rs:value "Robert" ];
];
.

query 8.2 Restricting by Graph IRI

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

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "nick",;
     rs:solution [
     rs:binding  [ rs:variable "nick";
        rs:value "Robert" ];
];
.

query 8.3 Restricting by Bound Variables

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
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 
  }
}    

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "mbox", "nick", "ppd",;
     rs:solution [
     rs:binding  [ rs:variable "mbox";
        rs:value <mailto:bob@work.example> ];
     rs:binding  [ rs:variable "mbox";
        rs:value "Robert" ];
     rs:binding  [ rs:variable "mbox";
        rs:value <http://example.org/foaf/bobFoaf> ];
];
.

data 8.4 Named and Default Graphs

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

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 .

data 8.4 Named and Default Graphs

# 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> .

data 8.4 Named and Default Graphs

# 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> .

query 8.4 Named and Default Graphs

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 }
  }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "name", "mbox", "date",;
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Bob" ];
     rs:binding  [ rs:variable "name";
        rs:value <mailto:bob@oldcorp.example.org> ];
     rs:binding  [ rs:variable "name";
        rs:value "2004-12-06"^^xsd:date ];
];
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Bob" ];
     rs:binding  [ rs:variable "name";
        rs:value <mailto:bob@newcorp.example.org> ];
     rs:binding  [ rs:variable "name";
        rs:value "2005-01-10"^^xsd:date ];
];
.

data 9.1 Specifying the 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> .

query 9.1 Specifying the Default Graph

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT  ?name
FROM    <http://example.org/foaf/aliceFoaf>
WHERE   { ?x foaf:name ?name }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "name",;
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Alice" ];
];
.

data 9.2 Specifying Named Graphs

# 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> .

data 9.2 Specifying Named Graphs

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

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

query 9.2 Specifying Named Graphs

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

SELECT ?src ?name

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

WHERE
{ GRAPH ?src { ?x foaf:name ?name } }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "src", "name",;
     rs:solution [
     rs:binding  [ rs:variable "src";
        rs:value <http://example.org/bob> ];
     rs:binding  [ rs:variable "src";
        rs:value "Bob" ];
];
     rs:solution [
     rs:binding  [ rs:variable "src";
        rs:value <http://example.org/alice> ];
     rs:binding  [ rs:variable "src";
        rs:value "Alice" ];
];
.

data 9.3 Combining FROM and FROM NAMED

# 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" .

data 9.3 Combining FROM and FROM NAMED

# 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> .

data 9.3 Combining FROM and FROM NAMED

# 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> .

query 9.3 Combining FROM and FROM NAMED

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 }
}

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "who", "g", "mbox",;
     rs:solution [
     rs:binding  [ rs:variable "who";
        rs:value "Bob Hacker" ];
     rs:binding  [ rs:variable "who";
        rs:value <http://example.org/bob> ];
     rs:binding  [ rs:variable "who";
        rs:value <mailto:bob@oldcorp.example.org> ];
];
     rs:solution [
     rs:binding  [ rs:variable "who";
        rs:value "Alice Hacker" ];
     rs:binding  [ rs:variable "who";
        rs:value <http://example.org/alice> ];
     rs:binding  [ rs:variable "who";
        rs:value <mailto:alice@work.example.org> ];
];
.

data 10.1.1 Projection

@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> .

query 10.1.1 Projection

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "name",;
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Bob" ];
];
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Alice" ];
];
.

data 10.1.2 DISTINCT

@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> .

query 10.1.2 DISTINCT

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "name",;
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Alice" ];
];
.

query 10.1.3 ORDER BY

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

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

query 10.1.3 ORDER BY

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)

query 10.1.3 ORDER BY

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

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

query 10.1.4 LIMIT

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

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

query 10.1.5 OFFSET

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

SELECT  ?name
WHERE   { ?x foaf:name ?name }
ORDER BY ?name
LIMIT   5
OFFSET  10

data 10.2 Selecting 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" .

query 10.2 Selecting Variables

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 } 
  }

XML results

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
  <head>
    <variable name="nameX"/>
    <variable name="nameY"/>
    <variable name="nickY"/>
  </head>
  <results>
    <result>
      <binding name="nameX">
        <literal>Alice</literal>
      </binding>
      <binding name="nameY">
        <literal>Bob</literal>
      </binding>
      <binding name="nickY">
        <unbound/>
      </binding>
    </result>
    <result>
      <binding name="nameX">
        <literal>Alice</literal>
      </binding>
      <binding name="nameY">
        <literal>Clare</literal>
      </binding>
      <binding name="nickY">
        <literal>CT</literal>
      </binding>
    </result>
  </results>
</sparql>

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "nameX", "nameY", "nickY",;
     rs:solution [
     rs:binding  [ rs:variable "nameX";
        rs:value "Alice" ];
     rs:binding  [ rs:variable "nameX";
        rs:value "Bob" ];
     rs:binding  [ rs:variable "nameX";
        rs:value  ];
];
     rs:solution [
     rs:binding  [ rs:variable "nameX";
        rs:value "Alice" ];
     rs:binding  [ rs:variable "nameX";
        rs:value "Clare" ];
     rs:binding  [ rs:variable "nameX";
        rs:value "CT" ];
];
.

data 10.3 Constructing an Output Graph

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

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

query 10.3 Constructing an Output Graph

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 }

graph results

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

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

data 10.3.1 Templates with Blank Nodes

@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" .

query 10.3.1 Templates with Blank Nodes

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 } .
 }  

graph results

@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" .

query 10.3.2 Accessing Graphs in the RDF Dataset

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

query 10.3.2 Accessing Graphs in the RDF Dataset

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 ) .
 }

data 10.3.3 Solution Modifiers and CONSTRUCT

@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 .

query 10.3.3 Solution Modifiers and CONSTRUCT

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 ?hits
LIMIT 2

graph results

@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:x foaf:name "Eve" .
_:y foaf:name "Bob" .

query 10.4.1 Explicit IRIs

DESCRIBE <http://example.org/>

query 10.4.2 Identifying Resources

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

query 10.4.2 Identifying Resources

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

query 10.4.2 Identifying Resources

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

query 10.4.3 Descriptions of Resources

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

graph results

@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:N
         [ vcard:Family       "Smith" ;
           vcard:Given        "John"  ] .

foaf:mbox_sha1sum  rdf:type  owl:InverseFunctionalProperty .

data 10.5 Asking "yes or no" questions

@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> .

query 10.5 Asking "yes or no" questions

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

ask results

yes

XML results

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

query 10.5 Asking "yes or no" questions

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

ask results

no

data 11 Testing Values

@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> .

query 11 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 ) }

data 11.4.1 bound

@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 .

query 11.4.1 bound

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  ?name .
         OPTIONAL { ?x dc:date ?date } .
         FILTER ( bound(?date) ) }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "givenName",;
     rs:solution [
     rs:binding  [ rs:variable "givenName";
        rs:value "Bob" ];
];
.

query 11.4.1 bound

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)) }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "name",;
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Alice" ];
];
.

data 11.4.2 isIRI

@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" .

query 11.4.2 isIRI

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "name", "mbox",;
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Alice" ];
     rs:binding  [ rs:variable "name";
        rs:value <mailto:alice@work.example> ];
];
.

data 11.4.3 isBlank

@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".

query 11.4.3 isBlank

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)
       }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "given", "family",;
     rs:solution [
     rs:binding  [ rs:variable "given";
        rs:value "Bob" ];
     rs:binding  [ rs:variable "given";
        rs:value "Smith" ];
];
.

data 11.4.4 isLiteral

@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" .

query 11.4.4 isLiteral

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "name", "mbox",;
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Bob" ];
     rs:binding  [ rs:variable "name";
        rs:value "bob@work.example" ];
];
.

data 11.4.5 str

@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> .

query 11.4.5 str

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") }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "name", "mbox",;
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Alice" ];
     rs:binding  [ rs:variable "name";
        rs:value <mailto:alice@work.example> ];
];
.

data 11.4.6 lang

@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> .

query 11.4.6 lang

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "name", "mbox",;
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Roberto"@ES ];
     rs:binding  [ rs:variable "name";
        rs:value <mailto:bob@work.example> ];
];
.

data 11.4.7 datatype

@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 .

query 11.4.7 datatype

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "name", "shoeSize",;
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Bob" ];
     rs:binding  [ rs:variable "name";
        rs:value 42 ];
];
.

data 11.4.10 RDFterm-equal

@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> .

query 11.4.10 RDFterm-equal

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)
       }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "name1", "name2",;
     rs:solution [
     rs:binding  [ rs:variable "name1";
        rs:value "Alice" ];
     rs:binding  [ rs:variable "name1";
        rs:value "Ms A." ];
];
     rs:solution [
     rs:binding  [ rs:variable "name1";
        rs:value "Ms A." ];
     rs:binding  [ rs:variable "name1";
        rs:value "Alice" ];
];
.

data 11.4.10 RDFterm-equal

@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> .

query 11.4.10 RDFterm-equal

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") ) }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "annotates",;
     rs:solution [
     rs:binding  [ rs:variable "annotates";
        rs:value <http://www.w3.org/TR/rdf-sparql-query/> ];
];
.

data 11.4.11 langMatches

@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" .

query 11.4.11 langMatches

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" ) }

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "title",;
     rs:solution [
     rs:binding  [ rs:variable "title";
        rs:value "Cette Série des Années Soixante-dix"@fr ];
];
     rs:solution [
     rs:binding  [ rs:variable "title";
        rs:value "Cette Série des Années Septante"@fr-BE ];
];
.

query 11.4.11 langMatches

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "title",;
     rs:solution [
     rs:binding  [ rs:variable "title";
        rs:value "That Seventies Show"@en ];
];
     rs:solution [
     rs:binding  [ rs:variable "title";
        rs:value "Cette Série des Années Soixante-dix"@fr ];
];
     rs:solution [
     rs:binding  [ rs:variable "title";
        rs:value "Cette Série des Années Septante"@fr-BE ];
];
.

data 11.4.12 regex

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

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

query 11.4.12 regex

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

turtle results

@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
	  rs:resultVariable "name",;
     rs:solution [
     rs:binding  [ rs:variable "name";
        rs:value "Alice" ];
];
.

query 11.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 11.6 Extensible Value Testing

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 ) . 
      }