0

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
}
kaleidoscop
  • 113
  • 5

1 Answers1

0

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.
}
Pascalco
  • 1,133
  • 1
  • 6
  • 20
  • Thank you. Btw, would you know how to take descendants up to order n, where n is some variable? (the above would be n=2) – kaleidoscop Dec 05 '20 at 16:52
  • @kaleidoscop As mentioned in a comment, there's also `?` postfix operator for "0 or more occurrences", so you can also use this path: `wdt:P171/(wdt:P171?)` (that's the property *optionally* followed by the same property). – Joshua Taylor Dec 06 '20 at 21:56
  • @kaleidoscop As for matching "up to order n", there's not convenient way in standard SPARQL, but when property paths were still in development, one of the proposed features included `p{n,m}` for "n to m" repetitions of p, and `p{,n}` for "0 to n". See [3 Path Language](https://www.w3.org/TR/sparql11-property-paths/#path-language) in that draft for more details. Some endpoints implemented that feature and still include it, so you might be lucky enough to be using one of them already. – Joshua Taylor Dec 06 '20 at 21:59