example Code SPARQL

Example code SPARQL

following below :


PREFIX dcterms: http://purl.org/dc/terms/>
PREFIX owl: http://www.w3.org/2002/07/owl#>
PREFIX rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: http://www.w3.org/2001/XMLSchema#>
PREFIX geo: http://aims.fao.org/aos/geopolitical.owl#>


SELECT * WHERE { ?s rdf:type geo:self_governing . }


PREFIX dcterms: <http://purl.org/dc/terms/&gt;
PREFIX owl: <http://www.w3.org/2002/07/owl#&gt;
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#&gt;
PREFIX geo: <http://aims.fao.org/aos/geopolitical.owl#&gt;

SELECT * WHERE { ?s rdf:type geo:self_governing . ?s geo:codeISO3 ?code }

PREFIX geo: <http://aims.fao.org/aos/geopolitical.owl#&gt;
PREFIX dcterms: <http://purl.org/dc/terms/&gt;
PREFIX owl: <http://www.w3.org/2002/07/owl#&gt;
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#&gt;

SELECT ?x WHERE { ?x rdf:type geo:non_self_governing . }

PREFIX abc: <http://aims.fao.org/aos/geopolitical.owl#&gt;
PREFIX dcterms: <http://purl.org/dc/terms/&gt;
PREFIX owl: <http://www.w3.org/2002/07/owl#&gt;
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#&gt;

SELECT ?x ?code WHERE { ?x rdf:type abc:non_self_governing . ?x abc:codeGAUL ?code }

PREFIX abc: <http://aims.fao.org/aos/geopolitical.owl#&gt;
PREFIX dcterms: <http://purl.org/dc/terms/&gt;
PREFIX owl: <http://www.w3.org/2002/07/owl#&gt;
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#&gt;

SELECT * WHERE { ?s rdf:type abc:non_self_governing . ?s abc:isAdministeredBy ?is }

 

Advertisements

About SPARQL not SQL

query language SPARQL (pronounced “sparkle”)

SPARQL in an SQL -like query language for RDF data, spesified by the RDF Data Acess working group of W3C. it differs from SQL in the following aspects:

  • SPARQL does not contain spesific Data Definition Language (DLL) provisions  because the schemas are represented in both RDFS and OWL as standard RDF graphs, thus requiring no specific language to deal with them.
  • SPARQL is not a Data Modification Language (DML), that is, one cannot insert, delete and update RDF graphs using SPARQL. the major reason for this is that there is still no consensus on the optimal DML design for RDF.

SPARQL support four types of queries:

  1. SELECT queries – return n-tuples of results just like the SELECT queries in SQL
  2. DESCRIBE queries – return an RDF graph. the resulting graph describe the resource, which match the query contraints. Usually, a description of  a resource is considered an RDF-molocule, forming the immediate neighborhood of an URI.
  3. ASK queries- provide positive or negative answer indicating whether or not the query pattern can be satisfied.
  4. CONSTRUCT queries – return an RDF graph constructed by means of the substitution of the variables in the graph template and combining the triples into a single RDF graph by set union.

named graph, is an RDf graph with assigned name in the form of a URI reference. in an extended RDF model, one can deal with multiple named graphs  and describe  the graphs, making statement about them, putting their URIs in subject position. while the original definition of named graphs leaves plenty of room for interpretation, a more concrete definition is provided in the specification of SPARQL.

a SPARQL query is composedof five parts zero or more prefix declarations, a query result clause, zero or more FROM or FROM NAMED clauses, a WHERE clause, and zero or more query modifiers. the optional PREFIX declarations introduce shorcuts for  long IRIs as normally done when working with XML namespace. such prefixes can be used in the WHERE clause.

a SPARQL query can take four forms: SELECT, ASK, CONSTRUCT and DESCRIBE.

The WHERE clause is the core of a SPARQL query. It is specified in terms of a set of triple
patterns. As extensively explained in the following sections, these triple patterns are used
to select the triples composing the result.