SPARQL Examples: Difference between revisions

From info216
No edit summary
No edit summary
(25 intermediate revisions by 3 users not shown)
Line 1: Line 1:
== SPARQL Examples from Lecture 03==
This page will be updated with SPARQL examples as the course progresses.


The data are available in this Blazegraph triple store:
[http://sandbox.i2s.uib.no] .


===List properties===
== SPARQL Examples from Session 3: SPARQL==
 
 
 
===Prefixes used===
 
The examples below will assume that these are in place (some examples aren't yet visible).
 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX bibo: <http://purl.org/ontology/bibo/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX ss: <http://semanticscholar.org/>
PREFIX kg: <http://i2s.uib.no/kg4news/>
PREFIX sp: <http://i2s.uib.no/kg4news/science-parse/>
PREFIX th: <http://i2s.uib.no/kg4news/theme/>
PREFIX xml: <http://www.w3.org/XML/1998/namespace>
PREFIX ex: <http://example.org/>
 
===Select all triplets in graph===
 
SELECT ?s ?p ?o
WHERE {
    ?s ?p ?o .
}
 
or
 
SELECT *
WHERE {
    ?s ?p ?o .
}
 
===Select the interestes of Cade===
 
SELECT ?cadeInterest
WHERE {
    ex:Cade ex:interest ?cadeInterest .
}
===Select the country and city where Emma lives===
 
SELECT ?emmaCity ?emmaCountry
WHERE {
    ex:Emma ex:address ?address .
    ?address ex:city ?emmaCity .
    ?address ex:country ?emmaCountry .
}
 
All address info to Emma.
 
SELECT ?person ?city ?country ?postalcode ?street WHERE{
        ex:Emma ex:address ?address.
        ?person ex:address ?address.
        ?address ex:city ?city.
        ?address ex:country ?country.
        ?address ex:postalCode ?postalcode.
        ?address ex:street ?street.
}
 
Selecting all address info, to everyone.
 
SELECT ?name ?city ?country ?postalcode ?street WHERE{
        ?person foaf:name ?name.
        ?person ex:address ?address.
        ?address ex:city ?city.
        ?address ex:country ?country.
        ?address ex:postalCode ?postalcode.
        ?address ex:street ?street.
}
 
===Select the people who are over 26 years old===
 
SELECT ?person ?age
WHERE {
    ?person ex:age ?age .
    FILTER(?age > 26) .   
}
===Select people who graduated with Bachelor===


SELECT ?person ?degree
WHERE {
    ?person ex:degree ?degree .
    ?degree ex:degreeLevel "Bachelor" .
         
}
===Delete cades photography interest===


  SELECT DISTINCT ?p WHERE {
  DELETE DATA
  ?s ?p ?o .
{
  } LIMIT 100
    ex:Cade ex:interest ex:Photography .
  }  
===Delete and insert university of valencia===


DELETE { ?s ?p ex:University_of_Valencia }
INSERT { ?s ?p ex:Universidad_de_Valencia }
WHERE  { ?s ?p ex:University_of_Valencia }


===List types===
===Check if the deletion worked===


PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?s ?o2
WHERE  {
  ?s ex:degree ?o .
  ?o ex:degreeSource ?o2 .
      }


SELECT DISTINCT ?t WHERE {
===Insert Sergio===
  ?s rdf:type ?t .
} LIMIT 100


===List authors===
INSERT DATA {
  ex:Sergio a foaf:Person ;
    ex:address [ a ex:Address ;
            ex:city ex:Valenciay ;
            ex:country ex:Spain ;
            ex:postalCode "46021"^^xsd:string ;
            ex:state ex:California ;
            ex:street "4_Carrer_del_Serpis"^^xsd:string ] ;
    ex:degree [ ex:degreeField ex:Computer_science ;
            ex:degreeLevel "Master"^^xsd:string ;
            ex:degreeSource ex:University_of_Valencia ;
            ex:year "2008"^^xsd:gYear ] ;
    ex:expertise ex:Big_data,
        ex:Semantic_technologies,
        ex:Machine_learning;
    foaf:name "Sergio_Pastor"^^xsd:string .
}
     
===Describe Sergio===


PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
DESCRIBE ex:Sergio ?o
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
WHERE {
  ex:Sergio ?p ?o .
  ?o ?p2 ?o2 .
  }


SELECT DISTINCT ?p WHERE {
===Construct that any city is in the country in an address===
  ?s rdf:type foaf:Person .
  ?s ?p ?o .
} LIMIT 100


===Prefrixes used===
CONSTRUCT {?city ex:locatedIn ?country}
Where {
  ?s rdf:type ex:Address .
  ?s ex:city ?city .
  ?s ex:country ?country.
  }


The examples below will assume that these are in place.


PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
==The data are available in this Blazegraph triple store:==
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
[http://sandbox.i2s.uib.no http://sandbox.i2s.uib.no] , but you may need to be inside the UiB network (or on VPN.)
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX bibo: <http://purl.org/ontology/bibo/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX ss: <http://semanticscholar.org/>
PREFIX kg: <http://i2s.uib.no/kg4news/>
PREFIX sp: <http://i2s.uib.no/kg4news/science-parse/>
PREFIX th: <http://i2s.uib.no/kg4news/theme/>


SELECT DISTINCT ?p WHERE {
SELECT DISTINCT ?p WHERE {
  ?s rdf:type ss:Paper .
  ?s rdf:type ss:Paper .
  ?s ?p ?o .
  ?s ?p ?o .
} LIMIT 100
  } LIMIT 100


===Explain all types and properties===
===Explain all types and properties===


SELECT ?pt ?e WHERE {
SELECT ?pt ?e WHERE {
  ?pt rdfs:comment ?e .
  ?pt rdfs:comment ?e .
} LIMIT 100
} LIMIT 100


===List main papers===
===List main papers===


SELECT * WHERE {
SELECT * WHERE {
 
  ?paper rdf:type kg:MainPaper .
  ?paper dc:date ?year .
 
}
 
===List properties===
 
SELECT DISTINCT ?p WHERE {
  ?s ?p ?o .
} LIMIT 100
 
===List types===
 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
   
   
  ?paper rdf:type kg:MainPaper .
SELECT DISTINCT ?t WHERE {
  ?paper dc:date ?year .
  ?s rdf:type ?t .
} LIMIT 100
 
===List authors===
 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
   
   
}
SELECT DISTINCT ?p WHERE {
  ?s rdf:type foaf:Person .
  ?s ?p ?o .
} LIMIT 100


Add this to show datatypes!
Add this to show datatypes!
  BIND ( DATATYPE(?year) AS ?type )
  BIND ( DATATYPE(?year) AS ?type )


Add this to only show years with the right type.
Add this to only show years with the right type.
  FILTER ( DATATYPE(?year) = xsd:gYear )
  FILTER ( DATATYPE(?year) = xsd:gYear )


===Group and count main papers by year===
===Group and count main papers by year===


SELECT ?year (COUNT(?paper) AS ?count) WHERE {
SELECT ?year (COUNT(?paper) AS ?count) WHERE {
   
   
  ?paper rdf:type kg:MainPaper .
  ?paper rdf:type kg:MainPaper .
  ?paper dc:date ?year .
  ?paper dc:date ?year .
  FILTER ( DATATYPE(?year) = xsd:gYear )
  FILTER ( DATATYPE(?year) = xsd:gYear )
   
   
}
}
GROUP BY ?year
GROUP BY ?year


Add this to order the results
Add this to order the results
ORDER BY ?year
ORDER BY ?year


Add this to order and only show years with more than 5 papers.
Add this to order and only show years with more than 5 papers.
HAVING (?count > 5)
HAVING (?count > 5)
ORDER BY DESC(?count)
ORDER BY DESC(?count)


===Show papers  
===Show papers===


SELECT ?paper ?year WHERE {
SELECT ?paper ?year WHERE {
   
   
  ?paper rdf:type kg:MainPaper .
  ?paper rdf:type kg:MainPaper .
  ?paper dc:date ?year .
  ?paper dc:date ?year .
  FILTER ( DATATYPE(?year) = xsd:gYear )
  FILTER ( DATATYPE(?year) = xsd:gYear )
   
   
}
}


  OPTIONAL {
Change last lines to show papers without an xsd:gYear too.
      ?paper dc:date ?year .
  OPTIONAL {
      FILTER ( DATATYPE(?year) = xsd:gYear )
      ?paper dc:date ?year .
   }
      FILTER ( DATATYPE(?year) = xsd:gYear )
  }
 
===Alternative values for variables===
 
SELECT ?p ?n ?year WHERE {
  ?p rdf:type kg:MainPaper .
  ?p dc:contributor ?a .
  ?a foaf:name ?n .
  ?p dc:date ?year .
  FILTER ( CONTAINS( ?n, ?str ) )
  FILTER ( CONTAINS( STR(?year), ?yr) )
    
  VALUES ?str { "Andreas" "David" }
  VALUES ?yr { "2020" "2019" }
}
 
===Property paths (composite properties)===
 
This query:
SELECT ?p ?n WHERE {
  ?p rdf:type kg:MainPaper .
  ?p dc:contributor ?c .
  ?c foaf:name ?n .
}
 
Can be simplified by eliminating ?c:
SELECT ?p ?n WHERE {
  ?p rdf:type kg:MainPaper .
  ?p dc:contributor / foaf:name ?n .
}
 
Can be further simplified by first reversing rdf:type:
SELECT ?p ?n WHERE {
  kg:MainPaper ^rdf:type ?p .
  ?p dc:contributor / foaf:name ?n .
}
 
...and the eliminating ?p:
SELECT ?n WHERE {
  kg:MainPaper ^rdf:type / dc:contributor / foaf:name ?n .
}
 
===Retrieve titles of papers that mention SPARQL===
 
Get papers with topics labelled "SPARQL":
SELECT ?t WHERE {
  ?t ^dc:title / dc:subject / skos:prefLabel "SPARQL" .
}
 
Some labels also go via a theme:
SELECT ?t WHERE {
  ?t ^dc:title / dc:subject / th:theme / skos:prefLabel "SPARQL" .
}


*** VALUES
We can get both using a path with an optional element (the '?'):
SELECT ?t WHERE {
  ?t ^dc:title / dc:subject / th:theme? / skos:prefLabel "SPARQL" .
}


SELECT ?p ?n ?year WHERE {
===Using an external SPARQL endpoint===
  ?p rdf:type kg:MainPaper .
  ?p dc:contributor ?a .
  ?a foaf:name ?n .
  ?p dc:date ?year .
  FILTER ( CONTAINS( ?n, ?str ) )
  FILTER ( CONTAINS( STR(?year), ?yr) )
  VALUES ?str { "Andreas" "David" }
  VALUES ?yr { "2020" "2019" }
}


*** PROPERTY PATH
We limit to a single label to avoid time-outs and rate limitations:
SELECT ?a ?n ?r WHERE {
  ?a rdf:type ss:Topic .
  ?a skos:prefLabel ?n .
  FILTER ( ?n = "SPARQL" )
  BIND ( STRLANG( ?n, "en" ) AS ?n2 )
    SERVICE <https://dbpedia.org/sparql> {
    ?r rdfs:label ?n2 .
    }
} LIMIT 1


SELECT ?p ?n WHERE {
===Insert 4-digit years for all main papers===
  ?p rdf:type kg:MainPaper .
  ?p dc:contributor / foaf:name ?n .
}


SELECT ?p ?n WHERE {
Main papers that do not have an xsd:gYear:
   kg:MainPaper ^rdf:type ?p .
SELECT * WHERE {
  ?p dc:contributor / foaf:name ?n .
    
}
  ?p rdf:type kg:MainPaper .
  ?p dc:date ?d .
  FILTER ( DATATYPE(?d) = xsd:gYear )
 
}


SELECT ?n WHERE {
Show the datatypes:
   kg:MainPaper ^rdf:type / dc:contributor / foaf:name ?n .
SELECT * WHERE {
}
    
  ?p rdf:type kg:MainPaper .
  ?p dc:date ?d .
  FILTER ( DATATYPE(?d) = xsd:dateTime )
  BIND ( year( ?d ) AS ?dt )
 
}


Insert 4-digit years:
INSERT { ?paper dc:date ?year }
WHERE {
 
  ?paper rdf:type kg:MainPaper .
  ?paper dc:date ?date .
  FILTER( DATATYPE(?date) != xsd:gYear )
  BIND ( YEAR(?date) AS ?year )
 
}


SELECT ?t WHERE {
(Actually, these years are xsd:integer- s, not quite xsd:gYear-s.)
  ?t ^dc:title / dc:subject / skos:prefLabel "SPARQL" .
}


SELECT ?t WHERE {
<!--
  ?t ^dc:title / dc:subject / th:theme / skos:prefLabel "SPARQL" .
}


SELECT ?t WHERE {
== SPARQL Examples from Session 7: RDFS==
  ?t ^dc:title / dc:subject / th:theme? / skos:prefLabel "SPARQL" .
}


--
===Turn on inference!===


SELECT ?a ?n ?r WHERE {
Make sure inference is on in your triple store, or that you compute closures if you run this in Python with rdflib and OWL-RL.
  ?a rdf:type ss:Topic .
  ?a skos:prefLabel ?n .
  FILTER ( ?n = "SPARQL" )
  BIND ( STRLANG( ?n, "en" ) AS ?n2 )
    SERVICE <https://dbpedia.org/sparql> {
    ?r rdfs:label ?n2 .
    }
} LIMIT 1


--
In '''Blazegraph''', create a new "Namespace" with the "Inference" box checked.
Remember to "Use" the new namespace.


SELECT * WHERE {
In '''Python''', install the [https://owl-rl.readthedocs.io/en/latest/ OWL-RL package] (''pip install owlrl'').
Explicitly compute RDFS closure like this:
import owlrl.RDFSClosure
   
   
  ?p rdf:type kg:MainPaper .
...
  ?p dc:date ?d .
  FILTER ( DATATYPE(?d) = xsd:gYear )
   
   
}
rdfs = owlrl.RDFSClosure.RDFS_Semantics(g, False, False, False)
rdfs.closure()
rdfs.flush_stored_triples()


--
===rdfs:subClassOf entailment===


SELECT * WHERE {
Update:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX kg: <http://i2s.uib.no/kg4news/>
   
   
  ?p rdf:type kg:MainPaper .
INSERT DATA {
  ?p dc:date ?d .
    kg:TimBernersLee rdf:type kg:Author .
  FILTER ( DATATYPE(?d) = xsd:dateTime )
    kg:Author rdfs:subClassOf foaf:Person .  
  BIND ( year( ?d ) AS ?dt )
}
 
Query:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX kg: <http://i2s.uib.no/kg4news/>
   
   
}
ASK {
    kg:TimBernersLee rdf:type foaf:Person .
}
 
===rdfs:domain entailment===
 
Update:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dcterm: <http://purl.org/dc/terms/>
PREFIX kg: <http://i2s.uib.no/kg4news/>
INSERT DATA {
    kg:TimBernersLee rdf:type kg:Author .
    kg:TheSemanticWeb dcterm:contributor kg:TimBernersLee .
    dcterm:contributor rdfs:domain kg:Paper .
}
 
Query:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dcterm: <http://purl.org/dc/terms/>
PREFIX kg: <http://i2s.uib.no/kg4news/>
ASK {
    kg:TheSemanticWeb rdf:type kg:Paper .
}
 
==OWL Ontologies==
 
The following files contain an ontology for the knowledge graph used in this page:
* Small: [[:File:small-kg4news-ontology.txt]]
* Full:  [[:File:kg4news-ontology.txt]]
 
Rename them from '.txt.' to '.ttl' after you download them.
 
You can
* view the ontologies online using [http://www.visualdataweb.de/webvowl/ WebVOWL] or
* download the [https://protege.stanford.edu/products.php#desktop-protege Protegé-OWL] ontology editor.
<!--
-->

Revision as of 12:53, 10 March 2022

This page will be updated with SPARQL examples as the course progresses.


SPARQL Examples from Session 3: SPARQL

Prefixes used

The examples below will assume that these are in place (some examples aren't yet visible).

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX bibo: <http://purl.org/ontology/bibo/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX ss: <http://semanticscholar.org/>
PREFIX kg: <http://i2s.uib.no/kg4news/>
PREFIX sp: <http://i2s.uib.no/kg4news/science-parse/>
PREFIX th: <http://i2s.uib.no/kg4news/theme/>
PREFIX xml: <http://www.w3.org/XML/1998/namespace> 
PREFIX ex: <http://example.org/> 

Select all triplets in graph

SELECT ?s ?p ?o
WHERE {
   ?s ?p ?o .
} 

or

SELECT *
WHERE {
   ?s ?p ?o .
} 

Select the interestes of Cade

SELECT ?cadeInterest
WHERE {
   ex:Cade ex:interest ?cadeInterest .
} 

Select the country and city where Emma lives

SELECT ?emmaCity ?emmaCountry
WHERE {
   ex:Emma ex:address ?address .
   ?address ex:city ?emmaCity .
   ?address ex:country ?emmaCountry .
} 

All address info to Emma.

SELECT ?person ?city ?country ?postalcode ?street WHERE{
       ex:Emma ex:address ?address. 
       ?person ex:address ?address.
       ?address ex:city ?city.
       ?address ex:country ?country.
       ?address ex:postalCode ?postalcode.
       ?address ex:street ?street.
}

Selecting all address info, to everyone.

SELECT ?name ?city ?country ?postalcode ?street WHERE{ 
       ?person foaf:name ?name. 
       ?person ex:address ?address.
       ?address ex:city ?city.
       ?address ex:country ?country.
       ?address ex:postalCode ?postalcode.
       ?address ex:street ?street.
}

Select the people who are over 26 years old

SELECT ?person ?age
WHERE {
   ?person ex:age ?age .
    FILTER(?age > 26) .     
} 

Select people who graduated with Bachelor

SELECT ?person ?degree
WHERE {
   ?person ex:degree ?degree .
   ?degree ex:degreeLevel "Bachelor" .
         
} 

Delete cades photography interest

DELETE DATA
{
   ex:Cade ex:interest ex:Photography .
} 

Delete and insert university of valencia

DELETE { ?s ?p ex:University_of_Valencia }
INSERT { ?s ?p ex:Universidad_de_Valencia }
WHERE  { ?s ?p ex:University_of_Valencia } 

Check if the deletion worked

SELECT ?s ?o2
WHERE  { 
 ?s ex:degree ?o .
 ?o ex:degreeSource ?o2 .
      	}

Insert Sergio

INSERT DATA {
 ex:Sergio a foaf:Person ;
   ex:address [ a ex:Address ;
           ex:city ex:Valenciay ;
           ex:country ex:Spain ;
           ex:postalCode "46021"^^xsd:string ;
           ex:state ex:California ;
           ex:street "4_Carrer_del_Serpis"^^xsd:string ] ;
   ex:degree [ ex:degreeField ex:Computer_science ;
           ex:degreeLevel "Master"^^xsd:string ;
           ex:degreeSource ex:University_of_Valencia ;
           ex:year "2008"^^xsd:gYear ] ;
   ex:expertise ex:Big_data,
       ex:Semantic_technologies,
       ex:Machine_learning;
   foaf:name "Sergio_Pastor"^^xsd:string .
}
     

Describe Sergio

DESCRIBE ex:Sergio ?o
WHERE {
 ex:Sergio ?p ?o .
 ?o ?p2 ?o2 .
 }

Construct that any city is in the country in an address

CONSTRUCT {?city ex:locatedIn ?country}
Where {
 ?s rdf:type ex:Address .
 ?s ex:city ?city .
 ?s ex:country ?country.
 }


The data are available in this Blazegraph triple store:

http://sandbox.i2s.uib.no , but you may need to be inside the UiB network (or on VPN.)

SELECT DISTINCT ?p WHERE {
  ?s rdf:type ss:Paper .
  ?s ?p ?o .
 } LIMIT 100

Explain all types and properties

SELECT ?pt ?e WHERE {
  ?pt rdfs:comment ?e .
} LIMIT 100

List main papers

SELECT * WHERE {
 
  ?paper rdf:type kg:MainPaper .
  ?paper dc:date ?year .
 
}

List properties

SELECT DISTINCT ?p WHERE {
  ?s ?p ?o .
} LIMIT 100

List types

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

SELECT DISTINCT ?t WHERE {
  ?s rdf:type ?t .
} LIMIT 100

List authors

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

SELECT DISTINCT ?p WHERE {
  ?s rdf:type foaf:Person .
  ?s ?p ?o .
} LIMIT 100

Add this to show datatypes!

  BIND ( DATATYPE(?year) AS ?type )

Add this to only show years with the right type.

  FILTER ( DATATYPE(?year) = xsd:gYear )

Group and count main papers by year

SELECT ?year (COUNT(?paper) AS ?count) WHERE {

  ?paper rdf:type kg:MainPaper .
  ?paper dc:date ?year .
  FILTER ( DATATYPE(?year) = xsd:gYear )

}
GROUP BY ?year

Add this to order the results

ORDER BY ?year

Add this to order and only show years with more than 5 papers.

HAVING (?count > 5)
ORDER BY DESC(?count)

Show papers

SELECT ?paper ?year WHERE {

  ?paper rdf:type kg:MainPaper .
  ?paper dc:date ?year .
  FILTER ( DATATYPE(?year) = xsd:gYear )

}

Change last lines to show papers without an xsd:gYear too.

  OPTIONAL {
      ?paper dc:date ?year .
      FILTER ( DATATYPE(?year) = xsd:gYear )
  }

Alternative values for variables

SELECT ?p ?n ?year WHERE {
  ?p rdf:type kg:MainPaper .
  ?p dc:contributor ?a .
  ?a foaf:name ?n .
  ?p dc:date ?year .
  FILTER ( CONTAINS( ?n, ?str ) )
  FILTER ( CONTAINS( STR(?year), ?yr) )
 
  VALUES ?str { "Andreas" "David" }
  VALUES ?yr { "2020" "2019" }
}

Property paths (composite properties)

This query:

SELECT ?p ?n WHERE {
  ?p rdf:type kg:MainPaper .
  ?p dc:contributor ?c .
  ?c foaf:name ?n .
}

Can be simplified by eliminating ?c:

SELECT ?p ?n WHERE {
  ?p rdf:type kg:MainPaper .
  ?p dc:contributor / foaf:name ?n .
}

Can be further simplified by first reversing rdf:type:

SELECT ?p ?n WHERE {
  kg:MainPaper ^rdf:type ?p .
  ?p dc:contributor / foaf:name ?n .
}

...and the eliminating ?p:

SELECT ?n WHERE {
  kg:MainPaper ^rdf:type / dc:contributor / foaf:name ?n .
}

Retrieve titles of papers that mention SPARQL

Get papers with topics labelled "SPARQL":

SELECT ?t WHERE {
  ?t ^dc:title / dc:subject / skos:prefLabel "SPARQL" .
}

Some labels also go via a theme:

SELECT ?t WHERE {
  ?t ^dc:title / dc:subject / th:theme / skos:prefLabel "SPARQL" .
}

We can get both using a path with an optional element (the '?'):

SELECT ?t WHERE {
  ?t ^dc:title / dc:subject / th:theme? / skos:prefLabel "SPARQL" .
}

Using an external SPARQL endpoint

We limit to a single label to avoid time-outs and rate limitations:

SELECT ?a ?n ?r WHERE {
  ?a rdf:type ss:Topic .
  ?a skos:prefLabel ?n .
  FILTER ( ?n = "SPARQL" )
  BIND ( STRLANG( ?n, "en" ) AS ?n2 )
    SERVICE <https://dbpedia.org/sparql> {
    ?r rdfs:label ?n2 .
    }
} LIMIT 1

Insert 4-digit years for all main papers

Main papers that do not have an xsd:gYear:

SELECT * WHERE {
 
  ?p rdf:type kg:MainPaper .
  ?p dc:date ?d .
  FILTER ( DATATYPE(?d) = xsd:gYear )
 
}

Show the datatypes:

SELECT * WHERE {
 
  ?p rdf:type kg:MainPaper .
  ?p dc:date ?d .
  FILTER ( DATATYPE(?d) = xsd:dateTime )
  BIND ( year( ?d ) AS ?dt )
 
}

Insert 4-digit years:

INSERT { ?paper dc:date ?year } 
WHERE {
  
 ?paper rdf:type kg:MainPaper .
 ?paper dc:date ?date .
  FILTER( DATATYPE(?date) != xsd:gYear )
  BIND ( YEAR(?date) AS ?year ) 
  
}

(Actually, these years are xsd:integer- s, not quite xsd:gYear-s.)