Saturday, 23 April 2011

Simple SPARQL Query Examples Part 1: SELECT

Some understanding of the RDF triple format will help you understand these basic examples of SPARQL queries.

Again, when using Snorql, the following namespaces are included with all SPARQL queries:

PREFIX owl: <>
PREFIX xsd: <>
PREFIX rdfs: <>
PREFIX rdf: <>
PREFIX foaf: <>
PREFIX dc: <>
PREFIX dbpedia2: <>
PREFIX dbpedia: <>
PREFIX skos: <> 

fig 1

I've centered the following examples around the Corydoras genus of armored catfish.

The scientific taxonomy for these fish is shown in the screenshot from Wikipedia's infobox on the Corydoras Aneus species page ( fig 1).

This will help you understand the correlation between the variables I've chosen in my example.

Example 1

Which predicates (properties) are used to refer to :Corydoras:

WHERE { ?s ?p :Corydoras  }

This query translates to:
Show the predicates where some subject  predicates the Corydoras resource (object).

The most notable of the result set is: dbpedia:ontology/genus, which says there's at least one subject that is of genus Corydora.

This example demonstrates that SPARQL query triples and matching result graphs are always in the form subject, predicate, object.

Example 2

Find all the species that have genus :Corydoras:

WHERE { ?s dbpedia-owl:genus :Corydoras }

The subjects that have as their genus Corydoras are all the Corydoras species

Example 3

Which family (or families) does the :Corydoras genus belong to?:

WHERE { ?s dbpedia-owl:genus :Corydoras.
?s dbpedia-owl:family ?f }

This query is asking to match all objects where some subject has as its genus Corydoras AND where that subject has as its family the variable we're looking for.

Or, more succinctly, if something is in the Corydoras genus, what is its family?

The result set: Callichthyidae and Corydoradinae.

The shorthand version of this query:

WHERE { ?s dbpedia-owl:genus :Corydoras;
dbpedia-owl:family ?f }

Example 4

List all species of the Characidae family along with their genus and binomial nomenclature. Order the result set by genus name:

SELECT DISTINCT ?species ?binomial ?genus
WHERE {?species dbpedia-owl:family :Characidae.
?species dbpedia-owl:genus ?genus.
?species dbpedia2:binomial ?binomial }
ORDER BY ?genus

Again, the shorthand version is less redundant:

SELECT DISTINCT ?species ?binomial ?genus
WHERE { ?species dbpedia-owl:family :Characidae;
dbpedia-owl:genus ?genus;
dbpedia2:binomial ?binomial }
ORDER BY ?genus

See the result set.

Example 5

Use the COUNT function to count the number of species in the preceding result set:

SELECT DISTINCT (COUNT(?species) AS ?num_species)
WHERE { ?species dbpedia-owl:family :Characidae;
dbpedia-owl:genus ?genus;
dbpedia2:binomial ?binomial }

The result is 77.