1
votes

Please forgive me if I am using the wrong terminology to describe my problem.

I want to extract information on the world's island regions via WIKIDATA SPARQL query, including coordinates, the country they belong to, the archipelago they belong to and their GeoNamesIDs. Of course, this information is not provided for each and every island, so if I include it in my query, I am limiting my result list to items that already contain these properties:

SELECT ?item ?itemLabel ?coords ?GeoNamesID

  WHERE {
  ?item wdt:P31 wd:Q23442.
  ?item wdt:P625 ?coords.
  ?item wdt:P1566 ?GeoNamesID.
  ?item wdt:P17 ?country.
  ?item wdt:P706 ?terrain.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

How can I make some of these properties "optional" to display the values if they exist but still include items that do not have them at all?

I could not find any similar issue in the long list of Wikidata SPARQL examples and would appreciate your help.

1
Awesome, I didn't know it would be that easy. :-) Will post my new query as a reply below for others who might have the same issue. - OnceUponATime

1 Answers

1
votes

Here is my updated query including several optional properties:

SELECT ?item ?itemLabel ?coords ?GeoNamesID ?country ?continent ?terrain ?date ?named ?archipelago

  WHERE {
  ?item wdt:P31 wd:Q23442.
  ?item wdt:P625 ?coords.
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  
  OPTIONAL {?item wdt:P1566 ?GeoNamesID.}
  OPTIONAL {?item wdt:P17 ?country.}
  OPTIONAL {?item wdt:P30 ?continent.}          
  OPTIONAL {?item wdt:P706 ?terrain.}
  OPTIONAL {?item wdt:P571 ?date.}
  OPTIONAL {?item wdt:P138 ?named.}
  OPTIONAL {?item wdt:P361 ?archipelago.}
}

I should note that I got a "time out" when first querying all optional properties. I had to retry. But it worked at once with a single optional item:

    SELECT ?item ?itemLabel ?coords ?GeoNamesID
    
      WHERE {
      ?item wdt:P31 wd:Q23442.
      ?item wdt:P625 ?coords.
      
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
      
      OPTIONAL {?item wdt:P1566 ?GeoNamesID.}
}