SPARQL Examples: Difference between revisions

From info216
No edit summary
No edit summary
Line 47: Line 47:
  PREFIX sp: <http://i2s.uib.no/kg4news/science-parse/>
  PREFIX sp: <http://i2s.uib.no/kg4news/science-parse/>
  PREFIX th: <http://i2s.uib.no/kg4news/theme/>
  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.
  }
   
   
  SELECT DISTINCT ?p WHERE {
  SELECT DISTINCT ?p WHERE {

Revision as of 15:03, 11 February 2022

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