Search code examples
sparqlwikidata

simple SPARQL with OR operator


I am trying to learn SPARQL to use with WikiData and I can't figure out how to perform an 'OR' statement, for instance find all taxons which are a subclasses of mammals OR a subclass of a subclass of mammals. I don't see how to use the VALUES method and if I use operator P171* with filter it takes too long. The following code provides an 'AND' statement I would like the equivalent 'OR' statement.

SELECT ?taxon 
WHERE
{
  ?taxon wdt:P171 wd:Q7377.  #taxon is a subclass of mammals
  ?taxonn wdt:P171/wdt:P171 wd:Q7377.  #taxon is a subclass of a subclass of mammals
}

Solution

  • There are multiple ways to perform an 'OR' statement in SPARQL. A good overview is provided in the follwing answer: https://stackoverflow.com/a/30502737/12780418.

    For your query the easiest way is to use |, i.e.

    SELECT ?taxon WHERE {
       ?taxon wdt:P171|wdt:P171/wdt:P171 wd:Q7377.
    }