Tutorial IX:
SPARQL 1.1 Supported Features

OpenLink Software


Functional Forms

NOT EXISTS

SELECT COUNT(*)
WHERE
 {
  ?s ?p "Novosibirsk" FILTER NOT EXISTS { ?s ?p "Новосибирск" }
 }

## See dbpedia live results.

## See LOD live results.

MINUS

SELECT COUNT(*)
WHERE
 {
  ?s ?p "Novosibirsk" MINUS { ?s ?p "Новосибирск" }
 }

## See dbpedia live results.

## See LOD live results.

bound

PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?article ?title
WHERE
 {
  ?article dc:title ?title ;
           dc:date  ?date .
  FILTER (
           bound ( ?date )
           && ?date >= "19450101"^^xsd:date
           && ?date <= "19451231"^^xsd:date
         )
 }
LIMIT 100

## See LOD live results.

IN

SELECT ?s1 ?s2 COUNT (1)
WHERE
 {
  ?s1 ?p ?o .
  FILTER (?s1 IN (<http://dbpedia.org/resource/Climate_change>,
   <http://dbpedia.org/resource/Disaster_risk_reduction>,
   <http://dbpedia.org/resource/Tanzania>,
   <http://dbpedia.org/resource/Vulnerability> ))
  ?s2 ?p ?o .
  FILTER (?s2 IN (<http://dbpedia.org/resource/Climate_change>,
   <http://dbpedia.org/resource/Disaster_risk_reduction>,
   <http://dbpedia.org/resource/Tanzania>,
   <http://dbpedia.org/resource/Poverty>,
   <http://dbpedia.org/resource/Construction>,
   <http://dbpedia.org/resource/Vulnerability> ))
  FILTER (?s1 != ?s2)
  FILTER (str(?s1) < str (?s2))
 }
LIMIT 20

## See dbpedia live results.

## See LOD live results.

NOT IN

SELECT ?s1
WHERE
 {
  ?s1 ?p ?o .
  FILTER (?s1 NOT IN (<http://dbpedia.org/resource/Climate_change>,
   <http://dbpedia.org/resource/Disaster_risk_reduction>,
   <http://dbpedia.org/resource/Tanzania>,
   <http://dbpedia.org/resource/Vulnerability> ))
 }
LIMIT 20

## See dbpedia live results.

## See LOD live results.

logical-and

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

SELECT ?name1 ?name2
WHERE
 {
  ?x foaf:name ?name1 .
  ?x foaf:mbox ?mbox1 .
  ?y foaf:name ?name2 .
  ?y foaf:mbox ?mbox2 .
  FILTER (?mbox1 = ?mbox2 && ?name1 != ?name2)
 }
LIMIT 10

## See LOD live results.

sameTerm

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

SELECT ?name1 ?name2
WHERE
 {
  ?x foaf:name ?name1 .
  ?x foaf:mbox ?mbox1 .
  ?y foaf:name ?name2 .
  ?y foaf:mbox ?mbox2 .
  FILTER (sameTerm(?mbox1, ?mbox2) && !sameTerm(?name1, ?name2))
 }
LIMIT 10

## See LOD live results.

RDFterm-equal

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

SELECT ?name1 ?name2
WHERE
 {
  ?x foaf:name ?name1 .
  ?x foaf:mbox ?mbox1 .
  ?y foaf:name ?name2 .
  ?y foaf:mbox ?mbox2 .
  FILTER (?mbox1 = ?mbox2 && ?name1 != ?name2)
 }
LIMIT 10

## See LOD live results.



Functions on RDF Terms

isIRI()

SELECT ?auth COUNT ( DISTINCT ?d ) as ?cnt
WHERE
 {
  ?d dc:creator ?auth .
  ?d ?p ?o .
  FILTER ( bif:contains ( ?o, 'WEB AND SEMANTIC' ) && isIRI ( ?auth ) )
 }
LIMIT 100

## See LOD live results.

isURI()

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

SELECT ?name ?mbox
WHERE
 {
  ?x foaf:name ?name .
  ?x foaf:mbox ?mbox .
  FILTER isURI(?mbox)
 }
LIMIT 100

## See LOD live results.

isBlank()

SELECT ?s ?lbl COUNT ( * )
WHERE
 {
  ?s ?p2 ?o2 .
  ?o2 <http://www.w3.org/2000/01/rdf-schema#label> ?lbl .
  ?s foaf:nick ?o .
  FILTER ( bif:contains ( ?o, '"Wellington"' ) && !isBlank ( ?s ) )
 }
GROUP BY ?s ?lbl
ORDER BY DESC 3
LIMIT 100

## See dbpedia live results.

## See LOD live results.

isLiteral()

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

## See LOD live results.

isREF()

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

SELECT ?name ?mbox
WHERE
 {
  ?x foaf:name ?name .
  ?x foaf:mbox ?mbox .
  FILTER isREF(?mbox)
 }
LIMIT 100

## See LOD live results.

str

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

SELECT ?name ?mbox
WHERE
 {
  ?x foaf:name ?name .
  ?x foaf:mbox ?mbox .
  FILTER regex(str(?mbox), "@w3.org")
 }
LIMIT 100

## See LOD live results.

lang

SELECT *
WHERE
 {
  ?m rdfs:label ?n .
  ?m grs:point ?p .
  FILTER ( lang ( ?n ) = "en" ) .
 }
LIMIT 100

## See dbpedia live results.

## See LOD live results.

datatype

SELECT DISTINCT ?s
                ( bif:round ( ?lat ) ) AS ?lat
 {
  GRAPH ?g
    {
      ?s geo:geometry ?geo .
      ?s geo:lat ?lat .
    }
  FILTER (
           datatype ( ?lat ) IN ( xsd:decimal )
         ) .
 }
LIMIT 100

## See LOD live results.

IRI

DEFINE output:valmode "LONG"
SELECT ?s ?p ?o
WHERE
 {
  GRAPH `iri('http://www.openlinksw.com/schemas/virtrdf#')` { ?s ?p ?o }
 }
LIMIT 100

## See dbpedia live results.

## See LOD live results.



Converting BINDINGS, Groups, Aggregates and SELECT Expressions

GROUP BY

SELECT ?c COUNT (*)
WHERE
 {
  ?s a ?c .
  ?s ?p ?o .
  ?o bif:contains "Shakespeare"
 }
GROUP BY ?c
ORDER BY DESC 2
LIMIT 100

## See dbpedia live results.

## See LOD live results.

SUM

DEFINE sql:signal-void-variables 1
PREFIX tpch: <http://www.openlinksw.com/schemas/tpch#>

SELECT ?cust sum(?extendedprice2 * (1 - ?discount2)) max (?bigdiscount)
WHERE
 {
  {
    SELECT ?line (?extendedprice * ?discount) as ?bigdiscount
    WHERE
      {
        ?line a tpch:lineitem ;
        tpch:lineextendedprice ?extendedprice ;
        tpch:linediscount ?discount .
      }
    ORDER BY DESC (?extendedprice * ?discount)
    LIMIT 1000
  }
  ?line tpch:has_order ?order .
  ?order tpch:has_customer ?cust .
  ?cust tpch:customer_of ?order2 .
  ?order2 tpch:order_of ?line2 .
  ?line2 tpch:lineextendedprice ?extendedprice2 ;
  tpch:linediscount ?discount2 .
 }
ORDER BY (SUM(?extendedprice2 * (1 - ?discount2)) / MAX (?bigdiscount))
LIMIT 100

HAVING

PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia-owl-uni: <http://dbpedia.org/ontology/University/>
PREFIX dbpedia-owl-inst: <http://dbpedia.org/ontology/EducationalInstitution/>

SELECT ?town COUNT(?uni)
  ?pgrad ?ugrad
  MAX(?population)
  ( ((?pgrad+?ugrad)/ MAX(?population))*100 ) AS ?percentage
WHERE
 {
  ?uni dbpedia-owl-inst:country dbpedia:United_Kingdom ;
                       dbpedia-owl-uni:postgrad ?pgrad ;
                      dbpedia-owl-uni:undergrad ?ugrad ;
                           dbpedia-owl-inst:city ?town .
  OPTIONAL
    {
     ?town dbpedia-owl:populationTotal ?population .
     FILTER (?population > 0 )
    }
 }
GROUP BY ?town ?pgrad ?ugrad
HAVING ( ( ( (?pgrad+?ugrad)/ MAX(?population) )*100 ) > 0 )
ORDER BY DESC 6

RDF
SPARQL
SQL
OpenLink
SIOC
WG
SPARUL
ARQ
XQuery
XPath
URI
Vocabulary
IFP
RIF
RDFS
OWL
RDBMS
ODBC
JDBC
GET
POST
QL