SPARQL Examples
From info216
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.
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 ex: <http://example.org/> PREFIX xml: <http://www.w3.org/XML/1998/namespace> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
select all triplets in graph
SELECT ?s ?p ?o
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 .
}
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.
}
