OR in sparql query

2.6k views Asked by At

This sparql query on wikidata shows all places in Germany (Q183) with a name that ends in -ow or -itz.

I want to extend this to look for places in Germany and, say, Austria.

I tried modifying the 8th line to something like:

wdt:P17 (wd:Q183 || wd:Q40);

in order to look for places in Austria (Q40), but this is not a valid query.

What is a way to extend the query to include other countries?

1

There are 1 answers

0
Sirko On BEST ANSWER

Afaik there is no syntax as simple as that. You can, however, use UNION to the same effect like this:

SELECT ?item ?itemLabel ?coord
WHERE 
{
    ?item wdt:P31/wdt:P279* wd:Q486972;
              rdfs:label ?itemLabel;
              wdt:P625 ?coord;
    {?item wdt:P17 wd:Q183} 
    UNION 
    {?item wdt:P17 wd:Q40}
    FILTER (lang(?itemLabel) = "de") . 
    FILTER regex (?itemLabel, "(ow|itz)$").
}

or as an alternative create a new variable containing both countries using VALUES:

SELECT ?item ?itemLabel ?coord
WHERE 
{
  VALUES ?country { wd:Q40 wd:Q183 }
  ?item wdt:P31/wdt:P279* wd:Q486972;
        wdt:P17 ?country; 
        rdfs:label ?itemLabel;
        wdt:P625 ?coord;
  FILTER (lang(?itemLabel) = "de") . 
  FILTER regex (?itemLabel, "(ow|itz)$").
}