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: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> 


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:

SELECT DISTINCT ?p
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:

SELECT DISTINCT ?s
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?:

SELECT DISTINCT ?f
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:

SELECT DISTINCT ?f
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.

1 comment:

  1. Hi,
    I'm trying to query infobox parameters so that I can get all the information in a specific infobox as a single table. Tried this one but didn't work. Can you help on this?
    PREFIX dbo:

    SELECT ?name ?released ?type ?recorded WHERE {
    ?album dbo:name ?name .
    ?album dbo:released ?released .
    ?album dbo:type ?type .
    ?album dbo:recorded ?recorded .
    }
    ORDER BY ?name
    LIMIT 100

    ReplyDelete