Search code examples
sparqlwikidata-query-service

Pulling an optional statement into a Wikidata query result


I'm trying to make a SPARQL to show Boston schools with their own website and their Wikipedia page:

PREFIX schema: <http://schema.org/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT ?item ?itemLabel ?wikipediaTitle ?website
WHERE {
  ?item wdt:P5353 wd:Q2215021 .
  OPTIONAL {
    ?item wdt:P1441 ?wikipediaTitle .
  }
  OPTIONAL {
    ?item wdt:P856 ?website .
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

The school's own website is optional and an identifier .. and it is correctly shown in the results where it is present, and blank where not.

The school's Wikipedia link is optional and a statement .. and is always blank in the results when it shouldn't be.

I keep trying variations, but it's always blank, and I don't know what I'm doing wrong.


Solution

  • If you mean sitelinks at the bottom of a Wikidata page, then the former OPTIONAL should be of this kind:

    OPTIONAL {
      ?wikipediaTitle schema:about ?item .
      ?wikipediaTitle schema:isPartOf <https://en.wikipedia.org/>
      }
    

    https://mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format#Sitelinks