Tutorial V:
Covers SPARQL Extensions for:
Aggregates, Updates, Deletes,
Sub-queries, Path Expressions,
Inference Rules and Geography

OpenLink Software

Live Instances

All the queries in this tutorial can be verified against the following live instances:

For each of these instances you have /sparql (basic SPARQL Endpoint) and /isparql (SPARQL Query By Example UI and Endpoint).

Scalar Subquery and Existence

## Find a Person Entities named "Alice"
## that knows at least on person named "John":

SELECT ?x
WHERE
  {
    ?x  foaf:name  "Alice"  .
    FILTER
      (
        bif:exists
          ( (
             SELECT *
             WHERE
               {
                 ?x  foaf:knows  ?y      .
                 ?y  foaf:name   "John"
               }
          ) )
      )
  }

Nested DISTINCT subquery with Scalars and Order By

## "Nested DISTINCT subquery
## with Scalars and Order By":

SELECT DISTINCT ?n
  ((
     SELECT COUNT (*)
     WHERE
       {
         ?p   foaf:interest  ?i  .
         ?ps  foaf:interest  ?i
       }
  ))
  ((
     SELECT COUNT (*)
     WHERE
       {
         ?p  foaf:interest  ?i
       }
  ))
WHERE
  {
    ?ps  foaf:nick  "plaid_skirt"@en  .
    {
      SELECT DISTINCT ?p ?psi
      WHERE
        {
          ?p    foaf:interest  ?i .
          ?psi  foaf:interest  ?i
        }
     } .
     FILTER
       (
         ?ps  =  ?psi
       )
       ?p  foaf:nick  ?n
  }
ORDER BY DESC 2
LIMIT 50

Distinctive Shared Interests

## "Find pairs of people interested
## in the same specialty where the two people
## do not know each other":

SELECT ?i
       ?cnt
       ?n1
       ?n2
       ?p1
       ?p2
WHERE
  {
    {
      SELECT ?i
             COUNT (*) AS ?cnt
      WHERE
        {
          ?p  foaf:interest  ?i
        }
      GROUP BY ?i
    }
    FILTER
      (
        ?cnt  >  1
        &&
        ?cnt  <  10
      ) .
    ?p1  foaf:interest  ?i  .
    ?p2  foaf:interest  ?i  .
    FILTER
      (
        ?p1  !=  ?p2
        &&
        !bif:exists
          ((
             SELECT (1)
             WHERE
               {
                 ?p1  foaf:knows  ?p2
               }
          )) &&
        !bif:exists
          ((
             SELECT (1)
             WHERE
               {
                 ?p2  foaf:knows  ?p1
               }
          ))
      ) .
    ?p1  foaf:nick  ?n1  .
    ?p2  foaf:nick  ?n2
  }
ORDER BY ?cnt
LIMIT 10

Path Expressions

## "Find all names and optionally mailboxes
## of friends of all Alices who know a John":

SELECT ?f+>foaf:name
       ?f*>foaf:mbox
WHERE
  {
    ?x  foaf:name   "Alice"  .
    ?x  foaf:knows  ?f       .
    FILTER
      (
        ?f+>foaf:name  =  "John"
      )
  }

The same query could be written:

SELECT ?fname
       ?mbox
WHERE
  {
     ?x   foaf:knows  ?f       .
     OPTIONAL
       {
         ?f  foaf:mbox   ?mbox
       }  .
     ?f   foaf:name   ?fname   .
     ?x   foaf:name   "Alice"  .
     ?x   foaf:knows  ?f2      .
     ?f2  foaf:name   "John"
  }

Full Text Queries & Text Scores Filtering

## "Text Scores
## incorporated into SPARQL":

SELECT *
WHERE
  {
    ?s ?p ?o .
    ?o bif:contains 'NEW AND YORK'
    OPTION (score ?sc) .
  }
ORDER BY DESC (?sc)
LIMIT 10

Entity Ranking

## "Entity Ranking":

PREFIX yago: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>

SELECT DISTINCT ?s2 AS ?c1
       ( <LONG::IRI_RANK> (?s2) ) AS ?Rank
WHERE
  {
    ?s1 ?s1textp ?o1 .
    ?o1 bif:contains 'Shakespeare' .
    ?s1 a yago:Writer110794014 .
    ?s2 prop:writer ?s1
  }
ORDER BY DESC ( <LONG::IRI_RANK> (?s2) )

Entity Ranking and Score

## Searching over labels, with text match
## scores and additional ranks for each
## iri / resource:

SELECT ?s ?page ?label
      ?textScore AS ?Text_Score_Rank
      ( <LONG::IRI_RANK> (?s) ) AS ?Entity_Rank
WHERE
  {
    ?s foaf:page ?page .
    ?s rdfs:label ?label .
    FILTER( lang( ?label ) = "en" ) .
    ?label bif:contains 'adobe and flash'
    OPTION (score ?textScore ) .
   }

XCONTAINS

## "xcontains usage":

SELECT *
FROM <http://mygraph.com>
WHERE
  {
    ?s ?p ?o .
    ?o bif:xcontains "//Hello[text-contains (., 'world')]"
  }

Operator IN

## "Operatior IN usage":

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/Capacity_building>,
                <http://dbpedia.org/resource/Poverty>,
                <http://dbpedia.org/resource/Construction>,
                <http://dbpedia.org/resource/Vulnerability>,
                <http://dbpedia.org/resource/Mount_Kilimanjaro>,
                <http://dbpedia.org/resource/Social_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/Capacity_building>,
               <http://dbpedia.org/resource/Poverty>,
               <http://dbpedia.org/resource/Construction>,
               <http://dbpedia.org/resource/Vulnerability>,
               <http://dbpedia.org/resource/Mount_Kilimanjaro>,
               <http://dbpedia.org/resource/Social_vulnerability>
             )
           )
    FILTER ( ?s1 != ?s2 )
    FILTER ( str( ?s1 ) < str ( ?s2 ) )
  }
LIMIT 20

Negation: DISTINCT

SELECT DISTINCT ?x ?o
WHERE
  {
    ?x a foaf:Agent .
    ?x ?p ?o .
    FILTER (
            !bif:exists
              (
                (
                  SELECT (1)
                  WHERE
                    {
                      ?x a foaf:Person
                    }
                )
              )
           )
  }
LIMIT 10

Negation: DESCRIBE

DESCRIBE ?x
WHERE
  {
    ?x a foaf:Agent .
    FILTER (
            !bif:exists
              (
                (
                  SELECT (1)
                  WHERE
                    {
                      ?x a foaf:Person
                    }
                )
              )
           )
  }
LIMIT 200

Aggregates: MAX

## "Find which town or city in
## the UK has the largest proportion of students":

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

Aggregates: SUM

## "Aggregate Distance Values Over Years":

PREFIX dst: <http://linkedgeodata.org/vocabulary#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT (bif:year( bif:stringdate(?sdate)) AS ?syear)
       (bif:sum( bif:number(?dist)) AS ?distance)
FROM <urn:dates:distances>
WHERE
  {
    ?row dc:date ?sdate .
    ?row dst:distance ?dist
  }
GROUP BY (bif:year(bif:stringdate(?sdate)))
ORDER BY ASC(bif:year(bif:stringdate(?sdate)))

SQL and TEXT Search

## "Find best lang matching City,
## containing "London" in its name":

SELECT DISTINCT ?cityUri ?cityName
  (
    sql:BEST_LANGMATCH ( ?cityName,
                         'en, en-gb;q=0.8, fr;q=0.7, *;q=0.1',
                         ''
                       )
  ) as ?bestCityName
WHERE
 {
  ?cityUri ?predicate ?value.
  ?cityUri a <http://dbpedia.org/ontology/City>.
  ?value bif:contains "London".
  OPTIONAL
   {
    ?cityUri rdfs:label ?cityName
   }
 }

Extending SPARQL via SQL for Full Text search: Variant I

##"Find all albums using parts of the Album Name":

SELECT ?s ?o ?an ( bif:search_excerpt ( bif:vector ( 'In', 'Your' ) , ?o ) )
WHERE
 {
  ?s rdf:type mo:Record .
  ?s foaf:maker ?a .
  ?a foaf:name ?an .
  ?s dc:title ?o .
  FILTER ( bif:contains ( ?o, '"in your"' ) )
 }

Extending SPARQL via SQL for Full Text search: Variant II

## "Find all movies from DBpedia
## where Title contains the words:
## 'Broken' and 'Flowers'":

SELECT ?s ?an ?dn ?o( bif:search_excerpt ( bif:vector ( 'Broken', 'Flowers' ) , ?o ) )
WHERE
 {
  ?s rdf:type dbpedia-owl:Film .
  ?s dbpprop:name ?o .
  FILTER ( bif:contains ( ?o, '"broken flowers"' ) )
  OPTIONAL { ?s dbpprop:starring ?starring . }
  OPTIONAL { ?s dbpprop:director ?director . }
  OPTIONAL { ?starring dbpprop:name ?an . }
  OPTIONAL { ?director dbpprop:name ?dn . }
 }

Date manipulation for xsd types within SPARQL

## This example shows usage of dateTime column
truncation to date only and performs a group by
on this column

## Insert some triples in a graph:
INSERT INTO GRAPH <http://BookStore.com>
 {
  <http://www.dajobe.org/foaf.rdf#i> <http://purl.org/dc/elements/1.1/title> "SPARQL and RDF" .
  <http://www.dajobe.org/foaf.rdf#i> <http://purl.org/dc/elements/1.1/date> <1999-01-01T00:00:00>.
  <http://www.w3.org/People/Berners-Lee/card#i> <http://purl.org/dc/elements/1.1/title> "Design notes" .
  <http://www.w3.org/People/Berners-Lee/card#i> <http://purl.org/dc/elements/1.1/date> <2001-01-01T00:00:00>.
  <http://www.w3.org/People/Connolly/#me> <http://purl.org/dc/elements/1.1/title> "Fundamentals of Compiler Design" .
  <http://www.w3.org/People/Connolly/#me> <http://purl.org/dc/elements/1.1/date> <2002-01-01T00:00:00>.
  <http://www.ivan-herman.net/foaf.rdf#me> <http://purl.org/dc/elements/1.1/title> "RDF Store" .
  <http://www.ivan-herman.net/foaf.rdf#me> <http://purl.org/dc/elements/1.1/date> <2001-03-05T00:00:00>.
  <http://bblfish.net/people/henry/card#me> <http://purl.org/dc/elements/1.1/title> "Design RDF notes" .
  <http://bblfish.net/people/henry/card#me> <http://purl.org/dc/elements/1.1/date> <2001-01-01T00:00:00>.
  <http://hometown.aol.com/chbussler/foaf/chbussler.foaf#me> <http://purl.org/dc/elements/1.1/title> "RDF Fundamentals" .
  <http://hometown.aol.com/chbussler/foaf/chbussler.foaf#me> <http://purl.org/dc/elements/1.1/date> <2002-01-01T00:00:00>.
 };

## Find Count of Group by Dates
SPARQL
SELECT (xsd:date(bif:subseq(str(?a_dt), 0, 10))), count(*)
FROM <http://BookStore.com>
WHERE
 {
  ?s <http://purl.org/dc/elements/1.1/date> ?a_dt
 }
GROUP BY ( xsd:date( bif:subseq( str( ?a_dt ), 0, 10 ) ) );

Date Range within SPARQL

## This example shows usage of date range
SELECT ?s ?date
FROM <http://dbpedia.org>
WHERE
 {
  ?s ?p ?date . FILTER ( ?date >= "19450101"^^xsd:date && ?date <= "19451231"^^xsd:date )
 }
LIMIT 100

Date with range Example II

## This example shows usage of date with bif:contains

SELECT DISTINCT ?s ?date
FROM <http://dbpedia.org>
WHERE
 {
  ?s ?p ?date . FILTER( ?date >= xsd:date("1945-01-01") && ?date < xsd:date("1946-01-01") && (str(?p) != str(rdfs:label)) )  }
LIMIT 30

Calling SQL from SPARQL

## "Calling SQL from SPARQL via use of sql: namespace prefix":

create procedure DB.DBA.ComposeInfo (
  in pname varchar,
  in pnick varchar := '',
  in pbox varchar := '' )
  {
    declare ss varchar;
    ss := concat(pname, ' ', pnick, ' ', pbox);
    ss := rtrim (ss, ' ');
    return ss;
  };

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

SELECT ( sql:ComposeInfo ( ?name, ?nick, ?box ) )
FROM <http://www.w3.org/People/Berners-Lee/card>
WHERE
  {
    ?s rdf:type foaf:Person .
    OPTIONAL { ?s foaf:name ?name } .
    OPTIONAL { ?s foaf:nick ?nick } .
    OPTIONAL { ?s foaf:box ?box } .
    FILTER ( ?nick like '%TimBL%' ) .
  }

Calling SQL from SPARQL

## "bif: namespace prefix":

SELECT *
FROM <http://www.w3.org/people#>
WHERE
  {
    ?s ?p ?o .
    ?o bif:contains '"Timo*"'
  }

SPARQL DESCRIBE

## "Pragma for Controlling SPARQL DESCRIBE behaviour
## e.g. Concise Bound Graphs":

DEFINE sql:describe-mode "CBD"
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

DESCRIBE ?friend
WHERE
  {
    ?s foaf:knows ?friend .
    ?friend foaf:nick ?nick .
    FILTER ( ?s =
        <http://www.advogato.org/person/rmorgan/foaf.rdf#me> )
  }

SPARUL INSERT

## "Pragma for Controlling SPARQL INSERT behaviour":

DEFINE input:default-graph-uri <http://mygraph.com>

INSERT INTO <http://mygraph.com>
  {
    <http://myopenlink.net/dataspace/Kingsley#this>
    <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
    <http://rdfs.org/sioc/ns#User>
  }

SPARUL INSERT via CONSTRUCT

## "Use of SPARUL CONSTRUCT to add triples to a Named Graph:
## 1) Execute a CONSTRUCT query.
## 2) View the generated triples to ensure they are correct.
## 3) Replace CONSTRUCT with INSERT INTO."

-- The CONSTRUCT query:
CONSTRUCT
  {
    ?s <http://www.w3.org/2002/07/owl#equivalentClass>
      `iri (bif:replace(?o,'http://schema.rdfs.org/', 'http://schema.org/'))`
  }
FROM WHERE
  {
    ?s <http://www.w3.org/2002/07/owl#equivalentClass> ?o
  }

-- Replaced CONSTRUCT with INSERT INTO:
SPARQL INSERT INTO <http://www.openlinksw.com/schemas/rdfs>
  {
    ?s <http://www.w3.org/2002/07/owl#equivalentClass>
      `iri (bif:replace(?o,'http://schema.rdfs.org/', 'http://schema.org/'))`
  }
FROM <http://www.openlinksw.com/schemas/rdfs>
WHERE
  {
    ?s <http://www.w3.org/2002/07/owl#equivalentClass> ?o
  }

SPARUL LOAD

## "Pragma for Controlling SPARQL LOAD behaviour":

SPARQL
load bif:concat (
                  "http://", bif:registry_get("URIQADefaultHost"),
                  "/DAV/n3_collection/kidehen.n3"
                )
INTO GRAPH <http://mygraph.com>

SPARUL DELETE

## "Pragma for Controlling SPARQL DELETE behaviour":

PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

DELETE FROM GRAPH <http://BookStore.com> { ?book ?p ?v }
WHERE
  {
    GRAPH <http://BookStore.com>
      {
        ?book dc:date ?date
        FILTER (
                 xsd:dateTime( ?date ) <
                 xsd:dateTime( "2000-01-01T00:00:00" )
               ).
        ?book ?p ?v.
      }
  }

SPARUL UPDATE

## "Pragma for Controlling SPARQL UPDATE behaviour"
## convert all properties to string:

INSERT INTO GRAPH
  {
    ?sa <http://ucb.com/nbeabase/resource/sampleId> `str (?oa)`
  }
WHERE
  {
    ?sa <http://ucb.com/nbeabase/resource/chemAbsNo> ?oa . FILTER regex(?oa, '-','i')
  }

SPARUL MODIFY

## "MODIFY GRAPH usage as form of UPDATE operation:"

MODIFY GRAPH
DELETE { ?s ?p ?o}
INSERT
  {
    `iri (bif:concat (str (?s), "#this"))` ?p ?o
  }
FROM <http://schema.org/docs/schemaorg>
WHERE
  {
    ?s ?p ?o .
    ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
    FILTER ( !isblank (?s) )
  }

SPARQL 1.1 feature: NOT EXISTS

## "NOT EXISTS usage:"

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

SPARQL 1.1 feature: MINUS

## "MINUS usage:"

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

SPARQL Subquery for enabling literal values based joins: Variant I

## "Use subqueries to enable literal values based joins:"

SELECT DISTINCT ?r
WHERE
  {
    graph ?g
      {
        ?r nie:url ?url .
      } .
    ?g nao:maintainedBy ?app .
    FILTER (?app = (SELECT ?a WHERE { ?a nao:identifier "nepomukindexer" }))
  }

SPARQL Subquery for enabling literal values based joins: Variant II

## "Use subqueries to enable literal values based joins:"

SELECT DISTINCT ?r
WHERE
  {
    graph ?g
      {
        ?r nie:url ?url .
      } .
    ?g nao:maintainedBy `(SELECT ?a WHERE { ?a nao:identifier "nepomukindexer" })` .
  }

Geo Spatial: ROUND

## "Places of worship, within 5 km of Paris,
## that have cafes in close proximity(0.2 km)":

PREFIX lgv: <http://linkedgeodata.org/vocabulary#>

SELECT DISTINCT ?cafe ?lat ?long ?cafename ?churchname
      ( bif:round( bif:st_distance ( ?churchgeo, ?cafegeo ) ) )
WHERE
  {
    ?church a lgv:place_of_worship .
    ?church geo:geometry ?churchgeo .
    ?church lgv:name ?churchname .
    ?cafe a lgv:cafe .
    ?cafe lgv:name ?cafename .
    ?cafe geo:geometry ?cafegeo .
    ?cafe geo:lat ?lat .
    ?cafe geo:long ?long .
    FILTER ( bif:st_intersects ( ?churchgeo, bif:st_point ( 2.3498, 48.853 ), 5 )
          && bif:st_intersects ( ?cafegeo, ?churchgeo, 0.2 ) )
  }
LIMIT 10

Geo Spatial: DESCRIBE

## "Geo Spatial "DESCRIBE usage:
## describe places of worship, within 5 km".
## of Paris, with cafes in close proximity(0.2 km)":

PREFIX lgv: <http://linkedgeodata.org/vocabulary#>

DESCRIBE ?cafe ?church
WHERE
  {
    ?church a lgv:place_of_worship .
    ?church geo:geometry ?churchgeo .
    ?church lgv:name ?churchname .
    ?cafe a lgv:cafe .
    ?cafe lgv:name ?cafename .
    ?cafe geo:geometry ?cafegeo .
    ?cafe geo:lat ?lat .
    ?cafe geo:long ?long .
    FILTER ( bif:st_intersects ( ?churchgeo, bif:st_point ( 2.3498, 48.853 ), 5 )
          && bif:st_intersects ( ?cafegeo, ?churchgeo, 0.2 ) )
  }
LIMIT 10

Geo Spatial: CONSTRUCT

## "Geo Spatial "CONSTRUCT a custom
## Linked Data Mesh (graph): about places of worship,
## within 5 km of Paris, that have cafes in".
## close proximity(0.2 km)":
## Note: we have distinct pin colors that
## identify for places of worship distinct from cafes:

PREFIX lgv: <http://linkedgeodata.org/vocabulary#>
PREFIX rtb: <http://www.openlinksw.com/schemas/oat/rdftabs#>

CONSTRUCT
  {
    ?cafe geo:geometry ?cafegeo ;
    rtb:useMarker '01' ;
    lgv:name ?cafename .
    ?church geo:geometry ?churchgeo ;
    rtb:useMarker '02' ;
    lgv:name ?churchname
  }
WHERE
  {
    ?church a lgv:place_of_worship .
    ?church geo:geometry ?churchgeo .
    ?church lgv:name ?churchname .
    ?cafe a lgv:cafe .
    ?cafe geo:geometry ?cafegeo .
    ?cafe lgv:name ?cafename .
    FILTER ( bif:st_intersects ( ?churchgeo, bif:st_point ( 2.3498, 48.853 ), 5 )
          && bif:st_intersects ( ?cafegeo, ?churchgeo, 0.2 ) )
  }

Geo Spatial: ASK

## "Asks for places of worship, within 5 km of Paris,
## that have cafes in close proximity(0.2 km)":

PREFIX lgv: <http://linkedgeodata.org/vocabulary#>

ASK
WHERE
  {
    ?church a lgv:place_of_worship .
    ?church geo:geometry ?churchgeo .
    ?church lgv:name ?churchname .
    ?cafe a lgv:cafe .
    ?cafe lgv:name ?cafename .
    ?cafe geo:geometry ?cafegeo .
    ?cafe geo:lat ?lat .
    ?cafe geo:long ?long .
    FILTER ( bif:st_intersects ( ?churchgeo, bif:st_point ( 2.3498, 48.853 ), 5 )
          && bif:st_intersects ( ?cafegeo, ?churchgeo, 0.2 ) )
  }

Geo Spatial: UNION

## Motorways across England & Scotland from DBpedia:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpprop: <http://dbpedia.org/property/>
PREFIX yago: <http://dbpedia.org/class/yago/>

SELECT ?road ?services ?lat ?long
WHERE
  {
    {
      ?services dbpprop:road ?road .
      ?road a yago:MotorwaysInEngland .
      ?services dbpprop:lat ?lat .
      ?services dbpprop:long ?long .
    }
    UNION
    {
      ?services dbpprop:road ?road .
      ?road a yago:MotorwaysInScotland .
      ?services dbpprop:lat ?lat .
      ?services dbpprop:long ?long .
    }
  }
LIMIT 20

Geo Spatial: COUNT

## "COUNT: Find big cities
## associated with a lot of GeoSpatial items":

SELECT ?s ( sql:num_or_null (?o) ) COUNT (*)
WHERE
  {
    ?s <http://dbpedia.org/ontology/populationTotal> ?o .
    FILTER ( sql:num_or_null (?o) < 6000000 ) .
    ?s geo:geometry ?geo .
    FILTER ( bif:st_intersects ( ?pt, ?geo,2 ) ) .
    ?xx geo:geometry ?pt
  }
GROUP BY ?s ( sql:num_or_null (?o) )
ORDER BY DESC 3
LIMIT 20

Geo Spatial: FILTER

## "FILTER: Find places of
## worship, within 5 km of Paris, that have
## cafes in close proximity(0.2 km)":

PREFIX lgv: <http://linkedgeodata.org/vocabulary#>

SELECT ?churchname ?cafename
       ( bif:st_distance ( ?churchgeo, ?cafegeo ) )
WHERE
  {
    ?church a lgv:place_of_worship .
    ?church geo:geometry ?churchgeo .
    ?church lgv:name ?churchname .
    ?cafe a lgv:cafe .
    ?cafe lgv:name ?cafename .
    ?cafe geo:geometry ?cafegeo .
    FILTER ( bif:st_intersects ( ?churchgeo, bif:st_point ( 2.3498, 48.853 ), 5 )
          && bif:st_intersects ( ?cafegeo, ?churchgeo, 0.2 ) )
  }
LIMIT 10

Distance Variant I

## "Find things within 20km of New York City":

SELECT DISTINCT ?resource ?label ?location
WHERE
  {
    <http://dbpedia.org/resource/New_York_City>
        geo:geometry ?sourcegeo                   .
    ?resource geo:geometry ?location              ;
                rdfs:label ?label .
    FILTER( bif:st_intersects( ?location, ?sourcegeo, 20 ) ) .
    FILTER( lang( ?label ) = "en" )
  }

Distance Variant II

## "Find Distance between New York City
## and London, England":

SELECT ( bif:st_distance( ?nyl,?ln ) )
      AS ?distanceBetweenNewYorkCityAndLondon
WHERE
  {
    <http://dbpedia.org/resource/New_York_City>
        geo:geometry ?nyl .
    <http://dbpedia.org/resource/London>
        geo:geometry ?ln  .
  }

Querying Time and Space Variant I

## "Find "All Educational Institutions
## within 10km of Oxford, UK; ordered by
## date of establishment":

SELECT DISTINCT ?thing AS ?uri
      ?thingLabel AS ?name
      ?date AS ?established
      ?matchgeo AS ?location
WHERE
  {
    <http://dbpedia.org/resource/Oxford> geo:geometry ?sourcegeo .
    ?resource geo:geometry ?matchgeo .
    FILTER( bif:st_intersects( ?matchgeo, ?sourcegeo, 5 ) ) .
    ?thing ?somelink ?resource .
    ?thing <http://dbpedia.org/ontology/established> ?date .
    ?thing rdfs:label ?thingLabel .
    FILTER( lang( ?thingLabel ) = "en" )
  }
ORDER BY ASC( ?date )

Querying Time and Space Variant II

## "Find Historical cross section of events related
## to Edinburgh and the surrounding area (within 30km)
## during the 19th century":

SELECT DISTINCT ?thing ?thingLabel
      ?dateMeaningLabel ?date ?matchgeo
WHERE
  {
    {
      SELECT DISTINCT ?thing ?matchgeo
      WHERE
        {
          <http://dbpedia.org/resource/Edinburgh>
              geo:geometry ?sourcegeo .
            ?resource geo:geometry ?matchgeo .
            FILTER( bif:st_intersects (
                ?matchgeo, ?sourcegeo, 30 ) ) .
            ?thing ?somelink ?resource
        }
    }
    {
      ?property rdf:type owl:DatatypeProperty ;
                          rdfs:range xsd:date
    } .
    ?thing ?dateMeaning ?date .
    FILTER( ?dateMeaning IN ( ?property ) ) .
    FILTER( ?date >= xsd:gYear("1800")
         && ?date <= xsd:gYear("1900") )
    ?dateMeaning rdfs:label ?dateMeaningLabel .
  FILTER( lang(?dateMeaningLabel) = "en" ) .
  ?thing rdfs:label ?thingLabel .
  FILTER( lang(?thingLabel) = "en" )
  }
ORDER BY ASC ( ?date )

Transitivity

## "Find all the people known by Tim Berners-Lee
## to a depth between 1 and 4 applications of
## the subquery":

SELECT ?o ?dist
       (
         (
           SELECT COUNT (*)
           WHERE
             {
               ?o foaf:knows ?xx
             }
         )
       )
WHERE
  {
    {
      SELECT ?s ?o
      WHERE
        {
          ?s foaf:knows ?o
        }
    }
    OPTION ( TRANSITIVE,
             t_distinct,
             t_in(?s),
             t_out(?o),
             t_min (1),
             t_max (4),
             t_step ('step_no') as ?dist ) .
    FILTER ( ?s = <http://www.w3.org/People/Berners-Lee/card#i> )
  }
ORDER BY ?dist DESC 3
LIMIT 50

Transitivity: Variant II

## "Find all the people known by Tim Berners-Lee
## to a depth between 2 and 4 applications of
## the subquery":

SELECT ?o ?dist
       (
         (
           SELECT COUNT (*)
           WHERE
             {
               ?o foaf:knows ?xx
             }
         )
       )
WHERE
  {
    {
      SELECT ?s ?o
      WHERE
        {
          ?s foaf:knows ?o
        }
    }
    OPTION ( TRANSITIVE,
             t_distinct,
             t_in(?s),
             t_out(?o),
             t_min (2),
             t_max (4) ,
             t_step ('step_no') as ?dist ) .
    FILTER ( ?s = <http://www.w3.org/People/Berners-Lee/card#i> )
  }
ORDER BY ?dist DESC 3
LIMIT 50

Transitivity with graph

## "Find how two people know each other
## and what graphs are involved in the connection":

SELECT ?link ?g ?step ?path
WHERE
  {
    {
      SELECT ?s ?o ?g
      WHERE
        {
          graph ?g {?s foaf:knows ?o }
        }
    }
    OPTION ( TRANSITIVE,
             t_distinct,
             t_in(?s),
             t_out(?o),
             t_no_cycles,
             t_shortest_only,
             t_step (?s) as ?link,
             t_step ('path_id') as ?path,
             t_step ('step_no') as ?step,
             t_direction 3 ) .
    FILTER ( ?s = <http://www.w3.org/People/Berners-Lee/card#i>
          && ?o = <http://myopenlink.net/dataspace/person/kidehen#this> )
  }
LIMIT 20

Transitivity with sameAs

## "Find all the IRI's that
## are the same as <New York>":

SELECT ?syn
WHERE
  {
    {
      SELECT ?x ?syn
      WHERE
        {
          {
            ?x owl:sameAs ?syn
          }
          UNION
          {
            ?syn owl:sameAs ?x
          }
        }
    }
    OPTION ( transitive, t_in (?x), t_out (?syn), t_distinct, t_min (0) )
    FILTER ( ?x = <http://dbpedia.org/resource/New_York> ) .
  }

Transitivity with sameAs and graph

## "Find all graphs that contain
## owl:sameAs for "New York":

SELECT ?g ?x count (*) as ?count
WHERE
  {
    {
      SELECT ?x ?alias ?g
      WHERE
        {
          {
            GRAPH ?g
              {
                ?x owl:sameAs ?alias
              }
          }
          UNION
          {
            GRAPH ?g
              {
                ?alias owl:sameAs ?x
            }
          }
        }
      }
    OPTION ( TRANSITIVE, t_in (?x), t_out (?alias), t_distinct, t_min (1) ) .
    FILTER ( ?x = <http://dbpedia.org/resource/New_York> ) .
  }

Transitive Closure via Graph Path Expressions: TBox

## Subsumption Demo Using Transitivity Clause:

SELECT ?y
FROM <http://dbpedia.org/resource/classes/yago#>
WHERE
  {
    {
      SELECT *
      WHERE
        {
          ?x rdfs:subClassOf ?y .
        }
    }
    OPTION ( TRANSITIVE, t_distinct, t_in (?x), t_out (?y) ) .
    FILTER ( ?x = <http://dbpedia.org/class/yago/AlphaReceptor105609111> )
  }

Transitive Closure via Graph Path Expressions: ABox

## Subsumption Demo Using Transitivity Clause:

SELECT ?y
FROM <http://dbpedia.org/resource/classes/yago#>
WHERE
  {
    {
      SELECT *
      WHERE
        {
          ?x rdfs:subClassOf ?y .
        }
    }
    OPTION ( TRANSITIVE, t_distinct, t_in (?y), t_out (?x) ) .
    FILTER ( ?y = <http://dbpedia.org/class/yago/Receptor105608868> )
  }



Inference Rules

Inference Rule: "skos-trans"

## Create an Inference Rule skos-trans

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

INSERT INTO GRAPH <urn:rules.skos>
  {
    skos:broader rdfs:subPropertyOf skos:broaderTransitive .
    skos:narrower rdfs:subPropertyOf skos:narrowerTransitive
  };

rdfs_rule_set ('skos-trans', 'urn:rules.skos');

Inference Rule SKOS Example 1: Transitivity with Inference Rule Context, Sub-Queries and FILTER

## Find entities that are subcategories of Protestant
## Churches, no deeper than 3 levels within the concept
## scheme hierarchy filtered by a specific subcategory
## Demonstrates use of Inference Rule Context,
## sub-queries, and FILTER to obtain entities
## associated with category:Protestant_churches
## combined with the use of the transitivity option
## that sets a 3-step traversal down the hierarchy.

DEFINE input:inference "skos-trans"
PREFIX p: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX category: <http://dbpedia.org/resource/Category:>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX geo: <http://www.georss.org/georss/>

SELECT DISTINCT ?c AS ?skos_broader
       ?trans AS ?skos_narrower
       ?dist AS ?skos_level
       ?m ?n ?p AS ?geo_point
WHERE
  {
    {
      SELECT ?c ?m ?n ?p ?trans ?dist
      WHERE
        {
          ?m rdfs:label ?n .
          ?m skos:subject ?c .
          ?c skos:broaderTransitive
              category:Protestant_churches .
          ?c skos:broaderTransitive ?trans
            OPTION ( TRANSITIVE,
                     t_distinct,
                     t_in (?c),
                     t_out (?trans),
                     t_max (3),
                     t_step ( 'step_no' ) as ?dist
                   ) .
          ?m p:abstract ?d .
          ?m geo:point ?p
          FILTER ( lang(?n) = "en" )
          FILTER ( lang(?d) = "en" )
        }
    }
    FILTER ( ?trans =
      <http://dbpedia.org/resource/Category:Churches_in_London> )
  }
ORDER BY ASC (?dist)

Inference Rule SKOS Example 2: Transitivity with Inference Rule Context, DISTINCT and FILTER

## "Inference Rule example using
## transitive properties from SKOS vocabulary":

SPARQL
DEFINE input:inference "skos-trans"
PREFIX p: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX category: <http://dbpedia.org/resource/Category:>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX geo: <http://www.georss.org/georss/>

SELECT DISTINCT ?m ?n ?p ?d
WHERE
  {
    ?m rdfs:label ?n .
    ?m skos:subject ?c .
    ?c skos:broaderTransitive category:Churches_in_Paris
        OPTION (TRANSITIVE) .
    ?m p:abstract ?d .
    ?m geo:point ?p
    FILTER ( lang(?n) = "fr" )
    FILTER ( lang(?d) = "fr" )
  }

Inference Rule: Yago Class hierarchy
"http://dbpedia.org/resource/inference/rules/yago#"

## "Loading script of the
## Yago Class hierarchy as inference rules":

## Load Class Hierarchy into a Named Graph
SELECT ttlp_mt
  (
    file_to_string_output ( 'yago-class-hierarchy_en.nt'),
    '',
    'http://dbpedia.org/resource/classes/yago#'
  );

## Create an Inference Rule that references
## the Yago Class Hierarchy Named Graph

rdfs_rule_set
  (
    'http://dbpedia.org/resource/inference/rules/yago#',
    'http://dbpedia.org/resource/classes/yago#'
  );

DBpedia Inference rule Example

## Retrieve all individuals instances
## of Fiction Class which should include all Novels":

DEFINE input:inference "http://dbpedia.org/resource/inference/rules/yago#"
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia: <http://dbpedia.org/property/>
PREFIX yago: <http://dbpedia.org/class/yago/>
SELECT ?s ?n
FROM <http://dbpedia.org>
WHERE
  {
    ?s a <http://dbpedia.org/class/yago/Fiction106367107> .
    ?s dbpedia:name ?n .
  }

DBpedia Inference rule Example with Literal Value

## Find all Fiction Books associated with a
## property "dbpedia:name" that has literal
## value: "The Lord of the Rings":

DEFINE input:inference "http://dbpedia.org/resource/inference/rules/yago#"
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia: <http://dbpedia.org/property/>
PREFIX yago: <http://dbpedia.org/class/yago/>

SELECT ?s
FROM <http://dbpedia.org>
WHERE
 {
  ?s a <http://dbpedia.org/class/yago/Fiction106367107> .
  ?s dbpedia:name "The Lord of the Rings"@en .
 }

DBpedia Inference rule Example with Virtuoso's Full Text Index extension via bif:contains

## Variant with Virtuoso's Full Text Index
## extension via the bif:contains
## function/magic predicate

DEFINE input:inference "http://dbpedia.org/resource/inference/rules/yago#"
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia: <http://dbpedia.org/property/>
PREFIX yago: <http://dbpedia.org/class/yago/>

SELECT ?s ?n
FROM <http://dbpedia.org>
WHERE
 {
  ?s a <http://dbpedia.org/class/yago/Fiction106367107> .
  ?s dbpedia:name ?n .
  ?n bif:contains 'Lord and Rings'
 }

Inference Rule: Relationship Vocab "urn:owl.tests"

## "Relationship Vocabulary":

## Create Instance Data for Relationship Ontology
## Verify Ontology Data is in Quad Store
## Ontology: <http://vocab.org/relationship/> (Relationship Ontology)
## Use pragma to put latest in Quad store.

DEFINE get:soft "replace"
SELECT *
FROM <http://vocab.org/relationship/>
WHERE
  {
    ?s ?p ?o
  }
## Clean up instance data graph

CLEAR GRAPH <urn:owl.tests>
## Create Instance Data for Relationship Ontology
PREFIX rel: <http://purl.org/vocab/relationship/>

INSERT into GRAPH <urn:owl.tests>
  {
    <http://dbpedia.org/resource/Prince_William_of_Wales> rel:siblingOf <http://dbpedia.org/resource/Prince_Harry_of_Wales> .
    <http://dbpedia.org/resource/Elizabeth_Bowes-Lyon> rel:ancestorOf <http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom> .
    <http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom> rel:ancestorOf
    <http://dbpedia.org/resource/Charles%2C_Prince_of_Wales> .
    <http://dbpedia.org/resource/Charles%2C_Prince_of_Wales> rel:ancestorOf <http://dbpedia.org/resource/Prince_William_of_Wales> .
  };

## Verify Data

SELECT *
FROM <urn:owl.tests>
WHERE
  {
    ?s ?p ?o
  }

## SQL mode
## Create Inference Rule that references the
## Relationship Ontology Named Graph

rdfs_rule_set ('urn:owl.tests', 'http://vocab.org/relationship/') ;

## Verify Rule's existence

SELECT *
FROM sys_rdf_schema;


owl:TransitiveProperty Reasoning

## Test owl:TransitiveProperty Reasoning
## Start with a specific URI
## Goal: See inferred Triples
## In this case, relationship between:
    <http://dbpedia.org/resource/Elizabeth_Bowes-Lyon>
## and her descendants:
    Queen Elizabeth, Prince Charles, Prince William, and Prince Harry)

DEFINE input:inference 'urn:owl.tests'
PREFIX rel: <http://purl.org/vocab/relationship/>

SELECT *
FROM <urn:owl.tests>
WHERE
  {
    <http://dbpedia.org/resource/Elizabeth_Bowes-Lyon>
    rel:ancestorOf ?o
  }

owl:SymmetricalProperty Reasoning

## Test owl:SymmetricalProperty Reasoning
## Should show same result irrespective of
## rel:siblingOf URI in Subject or Object slots of Triple

DEFINE input:inference 'urn:owl.tests'
PREFIX rel: <http://purl.org/vocab/relationship/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT *
FROM <urn:owl.tests>
WHERE
  {
    <http://dbpedia.org/resource/Prince_William_of_Wales>
    rel:siblingOf ?o
  }

owl:SymmetricalProperty Reasoning Variant II

## Test owl:SymmetricalProperty Reasoning
## Should show same result irrespective of
## rel:siblingOf URI in Subject or Object slots of Triple

DEFINE input:inference 'urn:owl.tests'
PREFIX rel: <http://purl.org/vocab/relationship/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT *
FROM <urn:owl.tests>
WHERE
  {
    ?s rel:siblingOf
    <http://dbpedia.org/resource/Prince_William_of_Wales>
    OPTION (T_DISTINCT)
  }

owl:inverseOf Reasoning

## Test owl:inverseOf Reasoning
## Should show triples exposing the inverseOf relation.
## In this case rel:ancestorOf instance data
## triples exist, so the system must infer
## rel:descendant Of triples

DEFINE input:inference 'urn:owl.tests'
PREFIX rel: <http://purl.org/vocab/relationship/>

SELECT *
FROM <urn:owl.tests>
WHERE
  {
    <http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom>
    rel:descendantOf ?o
  }

owl:inverseOf Reasoning Variant II

## Test owl:inverseOf Reasoning
## Should show triples exposing the inverseOf relation.
## In this case rel:ancestorOf instance data
## triples exist, so the system must infer
## rel:descendant Of triples

DEFINE input:inference 'urn:owl.tests'
PREFIX rel: <http://purl.org/vocab/relationship/>

SELECT *
FROM <urn:owl.tests>
WHERE
  {
    <http://dbpedia.org/resource/Prince_William_of_Wales>
    rel:descendantOf ?o OPTION (T_DISTINCT)
  }

Inference Rule: Relationship Vocab and SKOS "urn:owl.test2.rules"

## "Relationship Vocabulary and SKOS":

## Graph Cleanup

CLEAR GRAPH <urn:owl.test2.tbox>
CLEAR GRAPH <http://turnguard.com/virtuoso/test10.rdf>

## Load Instance Data into Quad Store
## PL Procedure

## SQL realm

DB.DBA.RDF_LOAD_RDFXML
  (
    http_get('http://www.w3.org/2009/08/skos-reference/skos-owl1-dl.rdf'),
    'no',
    'urn:owl.test2.tbox'
  );

DB.DBA.RDF_LOAD_RDFXML
  (
    http_get ('http://www.w3.org/2002/07/owl.rdf'),
    'no',
    'urn:owl.test2.tbox'
    );

DB.DBA.RDF_LOAD_RDFXML
  (
    http_get ('http://turnguard.com/virtuoso/test10.rdf'),
    'no',
    'http://turnguard.com/virtuoso/test10.rdf'
  );

SELECT * from <http://www.w3.org/2004/02/skos/core>
WHERE
  {
    {
      <http://www.w3.org/2004/02/skos/core#related> ?p ?o
    }
    UNION
    {
      ?s ?p <http://www.w3.org/2004/02/skos/core#related>
    }
  }

## Create Rules

## SQL Realm

rdfs_rule_set ('urn:owl.test2.rules', 'urn:owl.test2.tbox');

Example based on Relationship Vocab and SKOS

## Transitivity Query with SKOS concept hierarchy

DEFINE input:inference "urn:owl.test2.rules"
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT *
FROM <http://turnguard.com/virtuoso/test10.rdf>
WHERE
  {
    <http://www.turnguard.com/ElectroPop> skos:broaderTransitive ?o
    OPTION (T_DISTINCT).
  }

Inference Rule: "foaf-trans"

## "Negation using "COUNT"":
## with inference: "Find celebrities which"
## are not fans of their own fans:

SPARQL
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX sioc: <http://rdfs.org/sioc/ns#>

INSERT INTO GRAPH <urn:rules.skos>
  {
    foaf:knows rdfs:subPropertyOf skos:follows .
  };

rdfs_rule_set ('foaf-trans', 'urn:rules.skos');

Negation: COUNT with inference

## "Negation using "COUNT"
## with inference: "Find celebrities which"
## are not fans of their own fans":

SPARQL
DEFINE input:inference "foaf-trans"
PREFIX sioc: <http://rdfs.org/sioc/ns#>

SELECT ?celeb COUNT (*)
WHERE
  {
    ?claimant sioc:follows ?celeb .
    FILTER
      (
        !bif:exists
          (
            (
              SELECT (1)
              WHERE
                {
                  ?celeb sioc:follows ?claimant
                }
            )
          )
      )
  }
GROUP BY ?celeb
ORDER BY DESC 2
LIMIT 10

Negation: COUNT without inference

## "Negation with "COUNT"
## without inference":

SELECT ?celeb COUNT (*)
WHERE
  {
    ?claimant sioc:follows ?celeb .
    FILTER
      (
        !bif:exists
          (
            (
              SELECT (1)
              WHERE
                {
                  ?celeb sioc:follows ?claimant
                }
            )
          )
      )
  }
GROUP BY ?celeb
ORDER BY DESC 2
LIMIT 10

Co-reference Union Expansion Inference Rule Example

## "Conditional use of owl:sameAs inference rule":

DEFINE input:same-as "yes"

PREFIX geonames: <http://www.geonames.org/ontology#>
SELECT ?s ?z
WHERE
  {
    ?s geonames:parentFeature ?z
  }

SPARQL-BI Condition Example bif:either

## "Condition usage of
bif:either ( in cond any,
             in arg1 any,
             in arg2 any )
built-in SQL function":

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT ?team
      ( ( bif:either( ?ew = 'W', -1, 1)) * (?d + (((?m * 60) + ?s) / 3600.0)) as ?v )
  {
    ?team a dbo:HockeyTeam .
    ?team rdfs:label 'New Jersey Devils'@en .
    ?team dbp:city ?cityname .
    ?city rdfs:label ?cityname .
    ?city dbp:longd ?d ;
          dbp:longm ?m ;
          dbp:longs ?s ;
        dbp:longew ?ew .
  }

SPARQL-BI Condition Example bif:locate

## "Usage of
bif:locate ( in string_exp1 varchar,
             in string_exp2 varchar,
             [in start integer] )
built-in SQL function":

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT ?team
      ( ( bif:locate( 'W', ?ew )) * (?d + (((?m * 60) + ?s) / 3600.0)) as ?v )
  {
    ?team a dbo:HockeyTeam .
    ?team rdfs:label 'New Jersey Devils'@en .
    ?team dbp:city ?cityname .
    ?city rdfs:label ?cityname .
    ?city dbp:longd ?d ;
          dbp:longm ?m ;
          dbp:longs ?s ;
        dbp:longew ?ew .
  }

FILTER with "!BOUND()" expression Example

## "Usage of FILTER .. !BOUND()" expression
## It should be used with an OPTIONAL.
## Without OPTIONAL all variables from triple
## patterns will be bound in every result by definition."

SELECT DISTINCT ?s
WHERE
  {
    ?s <http://dbpedia.org/ontology/position> ?pos.
    ?s <http://purl.org/dc/terms/subject>
      <http://dbpedia.org/resource/Category:Premier_League_clubsgt>.
    ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#typegt>
      <http://dbpedia.org/class/yago/PremierLeagueClubs>.
    OPTIONAL {?s <http://dbpedia.org/property/leaguegt> ?o.}
    FILTER (!BOUND (?o))
  }
LIMIT 10

Inverse Functional Properties and Same As

## "Usage of IFP inference as a transparent
## addition of a subquery into the join sequence."

SPARQL
SELECT ?f
WHERE
 {
  ?k foaf:name "Kjetil Kjernsmo" .
  {
    SELECT ?org ?syn
    WHERE
      {
        ?org ?p ?key .
        ?syn ?p ?key .
        FILTER
          ( bif:rdf_is_sub
            ( "b3sifp", ?p, , 3 )
          &&
          ?syn != ?org
          )
      }
  }
  OPTION
    (
      TRANSITIVE ,
      T_IN (?org),
      T_OUT (?syn),
      T_MIN (0),
      T_MAX (1)
    )
  FILTER ( ?org = ?k ) .
  ?syn foaf:knows ?f .
 }

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