Difference between revisions of "SPARQL Examples"

From Info216
Line 25: Line 25:
 
  PREFIX ex: <http://example.org/>  
 
  PREFIX ex: <http://example.org/>  
  
===select all triplets in graph===
+
===Select all triplets in graph===
  
 
  SELECT ?s ?p ?o
 
  SELECT ?s ?p ?o
Line 31: Line 31:
 
     ?s ?p ?o .
 
     ?s ?p ?o .
 
  }  
 
  }  
===select the interestes of Cade===
+
===Select the interestes of Cade===
  
 
  SELECT ?cadeInterest
 
  SELECT ?cadeInterest
Line 37: Line 37:
 
     ex:Cade ex:interest ?cadeInterest .
 
     ex:Cade ex:interest ?cadeInterest .
 
  }  
 
  }  
===select the country and city where Emma lives===
+
===Select the country and city where Emma lives===
  
 
  SELECT ?emmaCity ?emmaCountry
 
  SELECT ?emmaCity ?emmaCountry
Line 45: Line 45:
 
     ?address ex:country ?emmaCountry .
 
     ?address ex:country ?emmaCountry .
 
  }  
 
  }  
===select the people who are over 26 years old===
+
===Select the people who are over 26 years old===
  
 
  SELECT ?person ?age
 
  SELECT ?person ?age
Line 52: Line 52:
 
     FILTER(?age > 26) .     
 
     FILTER(?age > 26) .     
 
  }  
 
  }  
===select people who graduated with Bachelor===
+
===Select people who graduated with Bachelor===
  
 
  SELECT ?person ?degree
 
  SELECT ?person ?degree
Line 60: Line 60:
 
            
 
            
 
  }  
 
  }  
===delete cades photography interest===
+
===Delete cades photography interest===
  
 
  DELETE DATA
 
  DELETE DATA
Line 66: Line 66:
 
     ex:Cade ex:interest ex:Photography .
 
     ex:Cade ex:interest ex:Photography .
 
  }  
 
  }  
===delete and insert university of valencia===
+
===Delete and insert university of valencia===
  
 
  DELETE { ?s ?p ex:University_of_Valencia }
 
  DELETE { ?s ?p ex:University_of_Valencia }
Line 72: Line 72:
 
  WHERE  { ?s ?p ex:University_of_Valencia }  
 
  WHERE  { ?s ?p ex:University_of_Valencia }  
  
===check if the deletion worked===
+
===Check if the deletion worked===
  
 
  SELECT ?s ?o2
 
  SELECT ?s ?o2
Line 80: Line 80:
 
       }
 
       }
  
===insert Sergio===
+
===Insert Sergio===
  
 
  INSERT DATA {
 
  INSERT DATA {
Line 100: Line 100:
 
}
 
}
 
        
 
        
===describe Sergio===
+
===Describe Sergio===
  
 
  DESCRIBE ex:Sergio ?o
 
  DESCRIBE ex:Sergio ?o
Line 108: Line 108:
 
   }
 
   }
  
===construct that any city is in the country in an address===  
+
===Construct that any city is in the country in an address===  
  
 
  CONSTRUCT {?city ex:locatedIn ?country}
 
  CONSTRUCT {?city ex:locatedIn ?country}

Revision as of 08:14, 15 February 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 .
} 

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.
 }